summaryrefslogtreecommitdiff
path: root/src/os/object
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-12-05 14:41:08 +0100
committerDaniel Friesel <derf@finalrewind.org>2018-12-05 14:41:08 +0100
commit9df610bbcde89aed4fb71f1fa73d9246ee1fe370 (patch)
tree9f1c9762d4fc398cc502d89a5081c0d55f44e0ff /src/os/object
parentbb1c35235b71079b20f469421a895b038bcfa0db (diff)
XDR: Use unions instead of casts for float/double<->int conversion
Diffstat (limited to 'src/os/object')
-rw-r--r--src/os/object/xdrinput.cc16
-rw-r--r--src/os/object/xdrstream.cc14
2 files changed, 24 insertions, 6 deletions
diff --git a/src/os/object/xdrinput.cc b/src/os/object/xdrinput.cc
index 5fb17bf..612244e 100644
--- a/src/os/object/xdrinput.cc
+++ b/src/os/object/xdrinput.cc
@@ -37,14 +37,22 @@ int64_t XDRInput::get_int64()
float XDRInput::get_float()
{
- uint32_t val = get_uint32();
- return *(float*)&val;
+ union {
+ uint32_t i;
+ float f;
+ } v;
+ v.i = get_uint32();
+ return v.f;
}
double XDRInput::get_double()
{
- uint64_t val = get_uint64();
- return *(double*)&val;
+ union {
+ uint64_t i;
+ double d;
+ } v;
+ v.i = get_uint64();
+ return v.d;
}
uint32_t XDRInput::get_opaque_length()
diff --git a/src/os/object/xdrstream.cc b/src/os/object/xdrstream.cc
index 888153b..ad708be 100644
--- a/src/os/object/xdrstream.cc
+++ b/src/os/object/xdrstream.cc
@@ -70,13 +70,23 @@ XDRStream & XDRStream::operator<<(int64_t number)
XDRStream & XDRStream::operator<<(float number)
{
- *this << *(uint32_t*)&number;
+ union {
+ uint32_t i;
+ float f;
+ } v;
+ v.f = number;
+ *this << v.i;
return *this;
}
XDRStream & XDRStream::operator<<(double number)
{
- *this << *(uint64_t*)&number;
+ union {
+ uint64_t i;
+ double d;
+ } v;
+ v.d = number;
+ *this << v.i;
return *this;
}