From deb64ea2e0a055755fd3590331035a8bdaac0308 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 20 Aug 2019 18:33:34 +0000 Subject: [PATCH] Replace aliasing with memcpy References #4319 git-svn-id: http://svn.osgeo.org/postgis/trunk@17744 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/lwgeom.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/liblwgeom/lwgeom.c b/liblwgeom/lwgeom.c index a655b616c..a5f5152d8 100644 --- a/liblwgeom/lwgeom.c +++ b/liblwgeom/lwgeom.c @@ -2444,28 +2444,21 @@ bits_for_precision(int32_t significant_digits) return bits_needed; } -static inline -double mask_double(double d, uint64_t mask) -{ - uint64_t* double_bits = (uint64_t*) (&d); - - (*double_bits) &= mask; - - return *((double*) double_bits); -} - static double trim_preserve_decimal_digits(double d, int32_t decimal_digits) { - if (d==0) + if (d == 0) return 0; int digits_left_of_decimal = (int) (1 + log10(fabs(d))); uint8_t bits_needed = bits_for_precision(decimal_digits + digits_left_of_decimal); - - uint64_t mask = 0xffffffffffffffff << (52 - bits_needed); + uint64_t dint = 0; + size_t dsz = sizeof(d) < sizeof(dint) ? sizeof(d) : sizeof(dint); - return mask_double(d, mask); + memcpy(&dint, &d, dsz); + dint &= mask; + memcpy(&d, &dint, dsz); + return d; } void lwgeom_trim_bits_in_place(LWGEOM* geom, int32_t prec_x, int32_t prec_y, int32_t prec_z, int32_t prec_m) -- 2.40.0