[postgis-tickets] r17741 - Replace aliasing approach with blunt memcpy to avoid errors with some compilers

Paul Ramsey pramsey at cleverelephant.ca
Tue Aug 20 11:18:43 PDT 2019


Author: pramsey
Date: 2019-08-20 11:18:43 -0700 (Tue, 20 Aug 2019)
New Revision: 17741

Modified:
   branches/2.5/liblwgeom/lwgeom.c
Log:
Replace aliasing approach with blunt memcpy to avoid errors with some compilers
References #4319


Modified: branches/2.5/liblwgeom/lwgeom.c
===================================================================
--- branches/2.5/liblwgeom/lwgeom.c	2019-08-20 16:01:52 UTC (rev 17740)
+++ branches/2.5/liblwgeom/lwgeom.c	2019-08-20 18:18:43 UTC (rev 17741)
@@ -2494,28 +2494,21 @@
 	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)



More information about the postgis-tickets mailing list