Skip to content

Commit 21945e6

Browse files
committed
use memcpy instead of manual copying
1 parent f5031a6 commit 21945e6

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

src/native/entropy_cpu_stubs.c

+4-18
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,13 @@
1515
#define random_t unsigned long long
1616
#define _rdseed_step _rdseed64_step
1717
#define _rdrand_step _rdrand64_step
18-
#define fill_bytes(buf, off, data) { \
19-
(_bp_uint8_off(buf, off))[0] = (uint8_t)((data) >> 56); \
20-
(_bp_uint8_off(buf, off))[1] = (uint8_t)((data) >> 48); \
21-
(_bp_uint8_off(buf, off))[2] = (uint8_t)((data) >> 40); \
22-
(_bp_uint8_off(buf, off))[3] = (uint8_t)((data) >> 32); \
23-
(_bp_uint8_off(buf, off))[4] = (uint8_t)((data) >> 24); \
24-
(_bp_uint8_off(buf, off))[5] = (uint8_t)((data) >> 16); \
25-
(_bp_uint8_off(buf, off))[6] = (uint8_t)((data) >> 8); \
26-
(_bp_uint8_off(buf, off))[7] = (uint8_t)((data)); \
27-
}
18+
#define fill_bytes(buf, off, data) memcpy(_bp_uint8_off(buf, off), data, 8)
2819

2920
#elif defined (__i386__)
3021
#define random_t unsigned int
3122
#define _rdseed_step _rdseed32_step
3223
#define _rdrand_step _rdrand32_step
33-
#define fill_bytes(buf, off, data) { \
34-
(_bp_uint8_off(buf, off))[0] = (uint8_t)((data) >> 24); \
35-
(_bp_uint8_off(buf, off))[1] = (uint8_t)((data) >> 16); \
36-
(_bp_uint8_off(buf, off))[2] = (uint8_t)((data) >> 8); \
37-
(_bp_uint8_off(buf, off))[3] = (uint8_t)((data)); \
38-
}
24+
#define fill_bytes(buf, off, data) memcpy(_bp_uint8_off(buf, off), data, 4)
3925

4026
#endif
4127
#endif /* __i386__ || __x86_64__ */
@@ -251,7 +237,7 @@ CAMLprim value mc_cpu_rdseed (value buf, value off) {
251237
int ok = 0;
252238
int i = RETRIES;
253239
do { ok = _rdseed_step (&r); _mm_pause (); } while ( !(ok | !--i) );
254-
fill_bytes(buf, off, r);
240+
fill_bytes(buf, off, &r);
255241
return Val_bool (ok);
256242
#else
257243
/* ARM: CPU-assisted randomness here. */
@@ -265,7 +251,7 @@ CAMLprim value mc_cpu_rdrand (value buf, value off) {
265251
int ok = 0;
266252
int i = RETRIES;
267253
do { ok = _rdrand_step (&r); } while ( !(ok | !--i) );
268-
fill_bytes(buf, off, r);
254+
fill_bytes(buf, off, &r);
269255
return Val_bool (ok);
270256
#else
271257
/* ARM: CPU-assisted randomness here. */

0 commit comments

Comments
 (0)