简体   繁体   中英

fast convert double to uint64_t in thread safe way

I have the below function that I call in a parallel loop. I have declared the union inside the below function for thread safety. The issue is that doing so slow things a lot. My question is whether there is a way to turn the double into an uin64_t (the same way as union does) but in a faster way? Thank you.

uint64_t flip(double d) {
  union {double d; uint64_t u64;} u;
  u.d = d;
  return u.u64 ^ (-(u.u64 >> 63) | 0x8000000000000000ULL);
}

I am using gcc to compile the code.

You might try re-doing this as a macro to eliminate the call and return overhead for the function:

#define DOUBLE_TO_UINT64(d) ((*(uint64_t *)(&(d))) ^ (-( (*(uint64_t *)(&(d))) >> 63) | 0x8000000000000000ULL))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM