diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-02-03 16:29:02 -0500 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-02-03 16:29:02 -0500 |
commit | 8aa19ff8c6533f84a2053c6fa6863397b522d014 (patch) | |
tree | 4dca409942389e53fd29783266a6c667e4ad0b3c /floatprops.h | |
parent | 68df6e89b5bc2bfab76204684cb446e84f358101 (diff) | |
download | vecmathlib-8aa19ff8c6533f84a2053c6fa6863397b522d014.zip vecmathlib-8aa19ff8c6533f84a2053c6fa6863397b522d014.tar.gz |
Use memcpy instead of a union to re-interpret values
Diffstat (limited to 'floatprops.h')
-rw-r--r-- | floatprops.h | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/floatprops.h b/floatprops.h index 7870c31..4608b10 100644 --- a/floatprops.h +++ b/floatprops.h @@ -5,6 +5,7 @@ #include <cmath> #include <cstdint> +#include <cstring> #include <limits> @@ -65,14 +66,20 @@ namespace vecmathlib { static inline real_t as_float(int_t x) { // return *(real_t*)&x; - union { int_t i; real_t r; } ir; - return ir.i=x, ir.r; + // union { int_t i; real_t r; } ir; + // return ir.i=x, ir.r; + real_t res; + std::memcpy(&res, &x, sizeof res); + return res; } static inline int_t as_int(real_t x) { // return *(int_t*)&x; - union { real_t r; int_t i; } ri; - return ri.r=x, ri.i; + // union { real_t r; int_t i; } ri; + // return ri.r=x, ri.i; + int_t res; + std::memcpy(&res, &x, sizeof res); + return res; } // Convert values @@ -128,14 +135,20 @@ namespace vecmathlib { static inline real_t as_float(int_t x) { // return *(real_t*)&x; - union { int_t i; real_t r; } ir; - return ir.i=x, ir.r; + // union { int_t i; real_t r; } ir; + // return ir.i=x, ir.r; + real_t res; + std::memcpy(&res, &x, sizeof res); + return res; } static inline int_t as_int(real_t x) { // return *(int_t*)&x; - union { real_t r; int_t i; } ri; - return ri.r=x, ri.i; + // union { real_t r; int_t i; } ri; + // return ri.r=x, ri.i; + int_t res; + std::memcpy(&res, &x, sizeof res); + return res; } // Convert values |