[英]fast convert double to uint64_t in thread safe way
I have the below function that I call in a parallel loop.我有下面的 function 我在并行循环中调用。 I have declared the
union
inside the below function for thread safety.为了线程安全,我已经在下面的 function 中声明了
union
。 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?我的问题是是否有办法将
double
精度转换为uin64_t
(与union
相同)但以更快的方式? 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.我正在使用 gcc 来编译代码。
You might try re-doing this as a macro to eliminate the call
and return
overhead for the function:您可以尝试将其作为宏重新执行,以消除 function 的
call
和return
开销:
#define DOUBLE_TO_UINT64(d) ((*(uint64_t *)(&(d))) ^ (-( (*(uint64_t *)(&(d))) >> 63) | 0x8000000000000000ULL))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.