Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify custom types for punning, use better names #128

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/integer64.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@
/** **/
/*****************************************************************************/

typedef struct Unsigned32x2TStruct {
unsigned int low;
unsigned int high;
} Unsigned32x2T;

typedef union {
Unsigned32x2T u32;
long long ll;
struct {
unsigned int low;
unsigned int high;
} U32x2Repr;
long long LongLongRepr;
} PunnedU32x2AndLongLong;

/*****************************************************************************/
Expand Down Expand Up @@ -1010,20 +1008,20 @@ SEXP runif_integer64(SEXP n_, SEXP min_, SEXP max_){
SEXP ret_;
PROTECT(ret_ = allocVector(REALSXP, n));
long long * ret = (long long *) REAL(ret_);
PunnedU32x2AndLongLong ii;
PunnedU32x2AndLongLong rand_draw;
GetRNGstate();
for (i=0; i<n; i++){
ii.u32.low = (unsigned int) floor(unif_rand()*4294967296);
ii.u32.high = (unsigned int) floor(unif_rand()*4294967296);
while(ii.ll == NA_INTEGER64) {
rand_draw.U32x2Repr.low = (unsigned int) floor(unif_rand()*4294967296);
rand_draw.U32x2Repr.high = (unsigned int) floor(unif_rand()*4294967296);
while(rand_draw.LongLongRepr == NA_INTEGER64) {
// # nocov start. Requires exceedingly rare (2^(-64) probability) occurrence.
// In principle can be found with the 'perfect' random seed, not worth burning compute to find out what that seed is.
// xx optimisation opportunity: if we know endianess, we only need to replace one of the two
ii.u32.low = (unsigned int) floor(unif_rand()*4294967296);
ii.u32.high = (unsigned int) floor(unif_rand()*4294967296);
rand_draw.U32x2Repr.low = (unsigned int) floor(unif_rand()*4294967296);
rand_draw.U32x2Repr.high = (unsigned int) floor(unif_rand()*4294967296);
// # nocov end
}
ret[i] = min + (ii.ll % d);
ret[i] = min + (rand_draw.LongLongRepr % d);
}
PutRNGstate();
UNPROTECT(1);
Expand Down
Loading