Skip to content

Commit ed7c725

Browse files
committed
return quick int <-> double conversion
1 parent 1078cbb commit ed7c725

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

cp-algo/util/simd.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ namespace cp_algo {
1717
return a < 0 ? -a : a;
1818
}
1919

20+
// https://stackoverflow.com/a/77376595
21+
// works for ints in (-2^51, 2^51)
22+
static constexpr dx4 magic = dx4() + (3ULL << 51);
23+
[[gnu::always_inline]] inline i64x4 lround(dx4 x) {
24+
return i64x4(x + magic) - i64x4(magic);
25+
}
26+
[[gnu::always_inline]] inline dx4 to_double(i64x4 x) {
27+
return dx4(x + i64x4(magic)) - magic;
28+
}
29+
2030
[[gnu::always_inline]] inline dx4 round(dx4 a) {
2131
return dx4{
2232
std::nearbyint(a[0]),

0 commit comments

Comments
 (0)