Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Limkilde Svendsen
relibc
Commits
13a10ce7
Commit
13a10ce7
authored
5 years ago
by
Alex Lyon
Browse files
Options
Downloads
Patches
Plain Diff
stdlib: implement rand_r() using XorShiftRng
parent
60746168
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/header/stdlib/mod.rs
+15
-3
15 additions, 3 deletions
src/header/stdlib/mod.rs
tests/expected/stdlib/rand.stdout
+4
-0
4 additions, 0 deletions
tests/expected/stdlib/rand.stdout
tests/stdlib/rand.c
+36
-0
36 additions, 0 deletions
tests/stdlib/rand.c
with
55 additions
and
3 deletions
src/header/stdlib/mod.rs
+
15
−
3
View file @
13a10ce7
...
...
@@ -628,9 +628,21 @@ pub unsafe extern "C" fn rand() -> c_int {
}
}
// #[no_mangle]
pub
extern
"C"
fn
rand_r
(
seed
:
*
mut
c_uint
)
->
c_int
{
unimplemented!
();
#[no_mangle]
pub
unsafe
extern
"C"
fn
rand_r
(
seed
:
*
mut
c_uint
)
->
c_int
{
if
seed
.is_null
()
{
errno
::
EINVAL
}
else
{
// set the type explicitly so this will fail if the array size for XorShiftRng changes
let
seed_arr
:
[
u8
;
16
]
=
mem
::
transmute
([
*
seed
;
16
/
mem
::
size_of
::
<
c_uint
>
()]);
let
mut
rng
=
XorShiftRng
::
from_seed
(
seed_arr
);
let
ret
=
rng
.gen_range
(
0
,
RAND_MAX
);
*
seed
=
ret
as
_
;
ret
}
}
// #[no_mangle]
...
...
This diff is collapsed.
Click to expand it.
tests/expected/stdlib/rand.stdout
+
4
−
0
View file @
13a10ce7
...
...
@@ -2,3 +2,7 @@
201425341
201425341
67141780
264204
271585843
264204
271585843
This diff is collapsed.
Click to expand it.
tests/stdlib/rand.c
+
36
−
0
View file @
13a10ce7
...
...
@@ -31,4 +31,40 @@ int main(void) {
puts
(
"srand(1) doesn't work"
);
exit
(
EXIT_FAILURE
);
}
// Ensure rand_r() fails with NULL input
if
(
rand_r
(
NULL
)
!=
EINVAL
)
{
puts
(
"rand_r(NULL) doesn't return EINVAL"
);
exit
(
EXIT_FAILURE
);
}
// Ensure rand_r() produces unique values
int
seed
=
259
;
int
rand_r_seed259_1
=
rand_r
((
unsigned
*
)
&
seed
);
printf
(
"%d
\n
"
,
rand_r_seed259_1
);
int
rand_r_seed259_2
=
rand_r
((
unsigned
*
)
&
seed
);
printf
(
"%d
\n
"
,
rand_r_seed259_2
);
if
(
rand_r_seed259_1
==
rand_r_seed259_2
)
{
puts
(
"rand_r() fails to produce unique values"
);
exit
(
EXIT_FAILURE
);
}
// Ensure rand_r() returns reproducible values
seed
=
259
;
int
rand_r_seed259_1_2
=
rand_r
((
unsigned
*
)
&
seed
);
printf
(
"%d
\n
"
,
rand_r_seed259_1_2
);
int
rand_r_seed259_2_2
=
rand_r
((
unsigned
*
)
&
seed
);
printf
(
"%d
\n
"
,
rand_r_seed259_2_2
);
if
(
rand_r_seed259_1
!=
rand_r_seed259_1_2
||
rand_r_seed259_2
!=
rand_r_seed259_2_2
)
{
puts
(
"rand_r() is not reproducible"
);
exit
(
EXIT_FAILURE
);
}
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment