summaryrefslogtreecommitdiffstats
path: root/floatprops.h
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@gmail.com>2013-02-03 16:29:02 -0500
committerErik Schnetter <schnetter@gmail.com>2013-02-03 16:29:02 -0500
commit8aa19ff8c6533f84a2053c6fa6863397b522d014 (patch)
tree4dca409942389e53fd29783266a6c667e4ad0b3c /floatprops.h
parent68df6e89b5bc2bfab76204684cb446e84f358101 (diff)
downloadvecmathlib-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.h29
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
OpenPOWER on IntegriCloud