1
- /* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1
+ /* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2
+ * Copyright 2016 University of Szeged
2
3
*
3
4
* Licensed under the Apache License, Version 2.0 (the "License");
4
5
* you may not use this file except in compliance with the License.
27
28
/**
28
29
* State of pseudo-random number generator
29
30
*/
30
- static unsigned int libc_random_gen_state [4 ] = { 1455997910 , 1999515274 , 1234451287 , 1949149569 };
31
+ static uint32_t libc_random_gen_state [4 ] = { 1455997910 , 1999515274 , 1234451287 , 1949149569 };
31
32
32
33
/**
33
34
* Standard file descriptors
@@ -62,9 +63,9 @@ memset (void *s, /**< area to set values in */
62
63
size_t n ) /**< area size */
63
64
{
64
65
uint8_t * area_p = (uint8_t * ) s ;
65
- for ( size_t index = 0 ; index < n ; index ++ )
66
+ while ( n -- )
66
67
{
67
- area_p [ index ] = (uint8_t ) c ;
68
+ * area_p ++ = (uint8_t ) c ;
68
69
}
69
70
70
71
return s ;
@@ -74,24 +75,21 @@ memset (void *s, /**< area to set values in */
74
75
* memcmp
75
76
*
76
77
* @return 0, if areas are equal;
77
- * -1 , if first area's content is lexicographically less, than second area's content;
78
- * 1 , otherwise
78
+ * <0 , if first area's content is lexicographically less, than second area's content;
79
+ * >0 , otherwise
79
80
*/
80
81
int
81
82
memcmp (const void * s1 , /**< first area */
82
83
const void * s2 , /**< second area */
83
84
size_t n ) /**< area size */
84
85
{
85
86
const uint8_t * area1_p = (uint8_t * ) s1 , * area2_p = (uint8_t * ) s2 ;
86
- for ( size_t index = 0 ; index < n ; index ++ )
87
+ while ( n -- )
87
88
{
88
- if (area1_p [ index ] < area2_p [ index ])
89
+ int diff = ((int ) * area1_p ++ ) - ((int ) * area2_p ++ );
90
+ if (diff )
89
91
{
90
- return -1 ;
91
- }
92
- else if (area1_p [ index ] > area2_p [ index ])
93
- {
94
- return 1 ;
92
+ return diff ;
95
93
}
96
94
}
97
95
@@ -109,9 +107,9 @@ memcpy (void *s1, /**< destination */
109
107
uint8_t * area1_p = (uint8_t * ) s1 ;
110
108
const uint8_t * area2_p = (const uint8_t * ) s2 ;
111
109
112
- for ( size_t index = 0 ; index < n ; index ++ )
110
+ while ( n -- )
113
111
{
114
- area1_p [ index ] = area2_p [ index ] ;
112
+ * area1_p ++ = * area2_p ++ ;
115
113
}
116
114
117
115
return s1 ;
@@ -127,21 +125,27 @@ memmove (void *s1, /**< destination */
127
125
const void * s2 , /**< source */
128
126
size_t n ) /**< bytes number */
129
127
{
130
- uint8_t * dest_p = ( uint8_t * ) s1 ;
131
- const uint8_t * src_p = ( const uint8_t * ) s2 ;
128
+ uint8_t * dest_p ;
129
+ const uint8_t * src_p ;
132
130
133
- if (dest_p < src_p )
131
+ if (s1 < s2 )
134
132
{ /* from begin to end */
135
- for (size_t index = 0 ; index < n ; index ++ )
133
+ dest_p = (uint8_t * ) s1 ;
134
+ src_p = (const uint8_t * ) s2 ;
135
+
136
+ while (n -- )
136
137
{
137
- dest_p [ index ] = src_p [ index ] ;
138
+ * dest_p ++ = * src_p ++ ;
138
139
}
139
140
}
140
- else if (dest_p > src_p )
141
+ else if (s1 > s2 )
141
142
{ /* from end to begin */
142
- for (size_t index = 1 ; index <= n ; index ++ )
143
+ dest_p = ((uint8_t * ) s1 ) + n - 1 ;
144
+ src_p = ((const uint8_t * ) s2 ) + n - 1 ;
145
+
146
+ while (n -- )
143
147
{
144
- dest_p [ n - index ] = src_p [ n - index ] ;
148
+ * dest_p -- = * src_p -- ;
145
149
}
146
150
}
147
151
@@ -156,23 +160,19 @@ CALL_PRAGMA (GCC diagnostic pop)
156
160
/** Compare two strings. return an integer less than, equal to, or greater than zero
157
161
if s1 is found, respectively, to be less than, to match, or be greater than s2. */
158
162
int
159
- strcmp (const char * str1 , const char * str2 )
163
+ strcmp (const char * s1 , const char * s2 )
160
164
{
161
- int s1 ;
162
- int s2 ;
163
- do
165
+ while (1 )
164
166
{
165
- s1 = * str1 ++ ;
166
- s2 = * str2 ++ ;
167
+ int c1 = (unsigned char ) * s1 ++ ;
168
+ int c2 = (unsigned char ) * s2 ++ ;
169
+ int diff = c1 - c2 ;
167
170
168
- if (s1 == 0 )
171
+ if (! c1 || diff )
169
172
{
170
- break ;
173
+ return diff ;
171
174
}
172
175
}
173
- while (s1 == s2 );
174
-
175
- return (s1 < s2 ) ? -1 : (s1 > s2 ? 1 : 0 );
176
176
} /* strcmp */
177
177
178
178
/** Compare two strings. return an integer less than, equal to, or greater than zero
@@ -181,38 +181,15 @@ strcmp (const char *str1, const char *str2)
181
181
int
182
182
strncmp (const char * s1 , const char * s2 , size_t n )
183
183
{
184
- size_t i ;
185
-
186
- if (n == 0 )
184
+ while (n -- )
187
185
{
188
- return 0 ;
189
- }
186
+ int c1 = (unsigned char ) * s1 ++ ;
187
+ int c2 = (unsigned char ) * s2 ++ ;
188
+ int diff = c1 - c2 ;
190
189
191
- if (s1 == NULL )
192
- {
193
- if (s2 != NULL )
190
+ if (!c1 || diff )
194
191
{
195
- return -1 ;
196
- }
197
- else
198
- {
199
- return 0 ;
200
- }
201
- }
202
- if (s2 == NULL )
203
- {
204
- return 1 ;
205
- }
206
-
207
- for (i = 0 ; i < n ; i ++ )
208
- {
209
- if (s1 [i ] > s2 [i ])
210
- {
211
- return 1 ;
212
- }
213
- else if (s1 [i ] < s2 [i ])
214
- {
215
- return -1 ;
192
+ return diff ;
216
193
}
217
194
}
218
195
@@ -225,12 +202,12 @@ strncmp (const char *s1, const char *s2, size_t n)
225
202
char * __attr_used___ // FIXME
226
203
strncpy (char * dest , const char * src , size_t n )
227
204
{
228
- size_t i ;
229
-
230
- for (i = 0 ; i < n ; i ++ )
205
+ while (n -- )
231
206
{
232
- dest [i ] = src [i ];
233
- if (src [i ] == '\0' )
207
+ char c = * src ++ ;
208
+ * dest ++ = c ;
209
+
210
+ if (!c )
234
211
{
235
212
break ;
236
213
}
@@ -243,10 +220,10 @@ strncpy (char *dest, const char *src, size_t n)
243
220
size_t
244
221
strlen (const char * s )
245
222
{
246
- size_t i ;
247
- for ( i = 0 ; s [i ]; i ++ )
223
+ size_t i = 0 ;
224
+ while ( s [i ])
248
225
{
249
- ;
226
+ i ++ ;
250
227
}
251
228
252
229
return i ;
@@ -273,9 +250,7 @@ rand (void)
273
250
libc_random_gen_state [3 ] ^= libc_random_gen_state [3 ] >> 19 ;
274
251
libc_random_gen_state [3 ] ^= intermediate ;
275
252
276
- uint32_t ret = libc_random_gen_state [3 ] % (RAND_MAX + 1u );
277
-
278
- return (int32_t ) ret ;
253
+ return libc_random_gen_state [3 ] % (RAND_MAX + 1u );
279
254
} /* rand */
280
255
281
256
/**
@@ -284,5 +259,8 @@ rand (void)
284
259
void
285
260
srand (unsigned int seed ) /**< new seed */
286
261
{
262
+ libc_random_gen_state [0 ] =
263
+ libc_random_gen_state [1 ] =
264
+ libc_random_gen_state [2 ] =
287
265
libc_random_gen_state [3 ] = seed ;
288
266
} /* srand */
0 commit comments