Skip to content

Commit 3608f8d

Browse files
committed
Refactoring and fixing jerry-libc
Refactor memory and string handling routines * Potential code size improvements: * Most loops don't really need a new incrementing index variable running between 0..n-1 but can just loop `while (n--)`, and the bodies don't need array indexing but can just use the `*p++` idiom. * Compare routines are not required to return -1, 0, and +1, but any negative, zero, or positive result will do. * `strncmp` may follow the other routines and does not have to have defined behaviour if any of its args is NULL. * Fix: * `strncmp` did not work correctly if the strings were equal but `n` was greater than their length (did not stop at the terminating zero character). Refactor printf * Merging code duplications, removing dead initialization. Refactor rand and srand * Making sure that the type of the state variables is OK (`uint32_t` instead of `unsigned int`). * Getting type conversions OK. * Fixing `srand` to write all state variables and thus indeed generate the same random sequence. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 684ed72 commit 3608f8d

File tree

2 files changed

+59
-100
lines changed

2 files changed

+59
-100
lines changed

jerry-libc/jerry-libc-printf.c

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -210,23 +211,13 @@ libc_printf_write_d_i (FILE *stream, /**< stream pointer */
210211
switch (length)
211212
{
212213
case LIBC_PRINTF_ARG_LENGTH_TYPE_NONE:
214+
case LIBC_PRINTF_ARG_LENGTH_TYPE_HH: /* char is promoted to int */
215+
case LIBC_PRINTF_ARG_LENGTH_TYPE_H: /* short int is promoted to int */
213216
{
214217
value = (uintmax_t) va_arg (*args_list_p, int);
215218
break;
216219
}
217220

218-
case LIBC_PRINTF_ARG_LENGTH_TYPE_HH:
219-
{
220-
value = (uintmax_t) va_arg (*args_list_p, int); /* char is promoted to int */
221-
break;
222-
}
223-
224-
case LIBC_PRINTF_ARG_LENGTH_TYPE_H:
225-
{
226-
value = (uintmax_t) va_arg (*args_list_p, int); /* short int is promoted to int */
227-
break;
228-
}
229-
230221
case LIBC_PRINTF_ARG_LENGTH_TYPE_L:
231222
{
232223
value = (uintmax_t) va_arg (*args_list_p, long int);
@@ -324,23 +315,13 @@ libc_printf_write_u_o_x_X (FILE *stream, /**< stream pointer */
324315
switch (length)
325316
{
326317
case LIBC_PRINTF_ARG_LENGTH_TYPE_NONE:
318+
case LIBC_PRINTF_ARG_LENGTH_TYPE_HH: /* char is promoted to int */
319+
case LIBC_PRINTF_ARG_LENGTH_TYPE_H: /* short int is promoted to int */
327320
{
328321
value = (uintmax_t) va_arg (*args_list_p, unsigned int);
329322
break;
330323
}
331324

332-
case LIBC_PRINTF_ARG_LENGTH_TYPE_HH:
333-
{
334-
value = (uintmax_t) va_arg (*args_list_p, unsigned int); /* char is promoted to int */
335-
break;
336-
}
337-
338-
case LIBC_PRINTF_ARG_LENGTH_TYPE_H:
339-
{
340-
value = (uintmax_t) va_arg (*args_list_p, unsigned int); /* short int is promoted to int */
341-
break;
342-
}
343-
344325
case LIBC_PRINTF_ARG_LENGTH_TYPE_L:
345326
{
346327
value = (uintmax_t) va_arg (*args_list_p, unsigned long int);
@@ -398,7 +379,7 @@ libc_printf_write_u_o_x_X (FILE *stream, /**< stream pointer */
398379
}
399380
}
400381

401-
uint32_t radix = 10;
382+
uint32_t radix;
402383
const char *alphabet;
403384

404385
switch (specifier)

jerry-libc/jerry-libc.c

Lines changed: 52 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -27,7 +28,7 @@
2728
/**
2829
* State of pseudo-random number generator
2930
*/
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 };
3132

3233
/**
3334
* Standard file descriptors
@@ -62,9 +63,9 @@ memset (void *s, /**< area to set values in */
6263
size_t n) /**< area size */
6364
{
6465
uint8_t *area_p = (uint8_t *) s;
65-
for (size_t index = 0; index < n; index++)
66+
while (n--)
6667
{
67-
area_p[ index ] = (uint8_t) c;
68+
*area_p++ = (uint8_t) c;
6869
}
6970

7071
return s;
@@ -74,24 +75,21 @@ memset (void *s, /**< area to set values in */
7475
* memcmp
7576
*
7677
* @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
7980
*/
8081
int
8182
memcmp (const void *s1, /**< first area */
8283
const void *s2, /**< second area */
8384
size_t n) /**< area size */
8485
{
8586
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--)
8788
{
88-
if (area1_p[ index ] < area2_p[ index ])
89+
int diff = ((int) *area1_p++) - ((int) *area2_p++);
90+
if (diff)
8991
{
90-
return -1;
91-
}
92-
else if (area1_p[ index ] > area2_p[ index ])
93-
{
94-
return 1;
92+
return diff;
9593
}
9694
}
9795

@@ -109,9 +107,9 @@ memcpy (void *s1, /**< destination */
109107
uint8_t *area1_p = (uint8_t *) s1;
110108
const uint8_t *area2_p = (const uint8_t *) s2;
111109

112-
for (size_t index = 0; index < n; index++)
110+
while (n--)
113111
{
114-
area1_p[ index ] = area2_p[ index ];
112+
*area1_p++ = *area2_p++;
115113
}
116114

117115
return s1;
@@ -127,21 +125,27 @@ memmove (void *s1, /**< destination */
127125
const void *s2, /**< source */
128126
size_t n) /**< bytes number */
129127
{
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;
132130

133-
if (dest_p < src_p)
131+
if (s1 < s2)
134132
{ /* 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--)
136137
{
137-
dest_p[ index ] = src_p[ index ];
138+
*dest_p++ = *src_p++;
138139
}
139140
}
140-
else if (dest_p > src_p)
141+
else if (s1 > s2)
141142
{ /* 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--)
143147
{
144-
dest_p[ n - index ] = src_p[ n - index ];
148+
*dest_p-- = *src_p--;
145149
}
146150
}
147151

@@ -156,23 +160,19 @@ CALL_PRAGMA (GCC diagnostic pop)
156160
/** Compare two strings. return an integer less than, equal to, or greater than zero
157161
if s1 is found, respectively, to be less than, to match, or be greater than s2. */
158162
int
159-
strcmp (const char *str1, const char *str2)
163+
strcmp (const char *s1, const char *s2)
160164
{
161-
int s1;
162-
int s2;
163-
do
165+
while (1)
164166
{
165-
s1 = *str1++;
166-
s2 = *str2++;
167+
int c1 = (unsigned char) *s1++;
168+
int c2 = (unsigned char) *s2++;
169+
int diff = c1 - c2;
167170

168-
if (s1 == 0)
171+
if (!c1 || diff)
169172
{
170-
break;
173+
return diff;
171174
}
172175
}
173-
while (s1 == s2);
174-
175-
return (s1 < s2) ? -1 : (s1 > s2 ? 1 : 0);
176176
} /* strcmp */
177177

178178
/** 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)
181181
int
182182
strncmp (const char *s1, const char *s2, size_t n)
183183
{
184-
size_t i;
185-
186-
if (n == 0)
184+
while (n--)
187185
{
188-
return 0;
189-
}
186+
int c1 = (unsigned char) *s1++;
187+
int c2 = (unsigned char) *s2++;
188+
int diff = c1 - c2;
190189

191-
if (s1 == NULL)
192-
{
193-
if (s2 != NULL)
190+
if (!c1 || diff)
194191
{
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;
216193
}
217194
}
218195

@@ -225,12 +202,12 @@ strncmp (const char *s1, const char *s2, size_t n)
225202
char * __attr_used___ // FIXME
226203
strncpy (char *dest, const char *src, size_t n)
227204
{
228-
size_t i;
229-
230-
for (i = 0; i < n; i++)
205+
while (n--)
231206
{
232-
dest[i] = src[i];
233-
if (src[i] == '\0')
207+
char c = *src++;
208+
*dest++ = c;
209+
210+
if (!c)
234211
{
235212
break;
236213
}
@@ -243,10 +220,10 @@ strncpy (char *dest, const char *src, size_t n)
243220
size_t
244221
strlen (const char *s)
245222
{
246-
size_t i;
247-
for (i = 0; s[i]; i++)
223+
size_t i = 0;
224+
while (s[i])
248225
{
249-
;
226+
i++;
250227
}
251228

252229
return i;
@@ -273,9 +250,7 @@ rand (void)
273250
libc_random_gen_state[3] ^= libc_random_gen_state[3] >> 19;
274251
libc_random_gen_state[3] ^= intermediate;
275252

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);
279254
} /* rand */
280255

281256
/**
@@ -284,5 +259,8 @@ rand (void)
284259
void
285260
srand (unsigned int seed) /**< new seed */
286261
{
262+
libc_random_gen_state[0] =
263+
libc_random_gen_state[1] =
264+
libc_random_gen_state[2] =
287265
libc_random_gen_state[3] = seed;
288266
} /* srand */

0 commit comments

Comments
 (0)