Discussion:
[gem5-dev] Change in gem5/gem5[master]: base: Add some functions to convert floats to bits and vice versa.
(too old to reply)
Gabe Black (Gerrit)
2018-11-20 03:30:49 UTC
Permalink
Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/14455


Change subject: base: Add some functions to convert floats to bits and vice
versa.
......................................................................

base: Add some functions to convert floats to bits and vice versa.

These make it easier to extract the binary representation of floats and
doubles, and given a binary representation convert it back again.

The versions with a size prefix are safer to use since they make it
clear what size inputs/outputs are expected. The versions without are
to make writing generic code easier in case the same code snippet,
templated function, etc., needs to be applied in both circumstances.

Change-Id: Ib1f35a7e88e00806a7c639c211c5699b4af5a472
---
M src/base/types.hh
1 file changed, 54 insertions(+), 0 deletions(-)



diff --git a/src/base/types.hh b/src/base/types.hh
index a53f4db..c02a606 100644
--- a/src/base/types.hh
+++ b/src/base/types.hh
@@ -168,6 +168,60 @@
typedef uint64_t RegVal;
typedef double FloatRegVal;

+static inline uint32_t
+floatToBits32(float val)
+{
+ union
+ {
+ float f;
+ uint32_t i;
+ } u;
+ u.f = val;
+ return u.i;
+}
+
+static inline uint64_t
+floatToBits64(double val)
+{
+ union
+ {
+ double f;
+ uint64_t i;
+ } u;
+ u.f = val;
+ return u.i;
+}
+
+static inline uint64_t floatToBits(double val) { return
floatToBits64(val); }
+static inline uint32_t floatToBits(float val) { return floatToBits32(val);
}
+
+static inline float
+bitsToFloat32(uint32_t val)
+{
+ union
+ {
+ float f;
+ uint32_t i;
+ } u;
+ u.i = val;
+ return u.f;
+}
+
+static inline double
+bitsToFloat64(uint64_t val)
+{
+ union
+ {
+ double f;
+ uint64_t i;
+ } u;
+ u.i = val;
+ return u.f;
+}
+
+static inline double bitsToFloat(uint64_t val) { return
bitsToFloat64(val); }
+static inline float bitsToFloat(uint32_t val) { return bitsToFloat32(val);
}
+
/**
* Thread index/ID type
*/
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/14455
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Ib1f35a7e88e00806a7c639c211c5699b4af5a472
Gerrit-Change-Number: 14455
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <***@google.com>
Gerrit-MessageType: newchange
Gabe Black (Gerrit)
2018-11-27 00:46:48 UTC
Permalink
Gabe Black has submitted this change and it was merged. (
https://gem5-review.googlesource.com/c/public/gem5/+/14455 )

Change subject: base: Add some functions to convert floats to bits and vice
versa.
......................................................................

base: Add some functions to convert floats to bits and vice versa.

These make it easier to extract the binary representation of floats and
doubles, and given a binary representation convert it back again.

The versions with a size prefix are safer to use since they make it
clear what size inputs/outputs are expected. The versions without are
to make writing generic code easier in case the same code snippet,
templated function, etc., needs to be applied in both circumstances.

Change-Id: Ib1f35a7e88e00806a7c639c211c5699b4af5a472
Reviewed-on: https://gem5-review.googlesource.com/c/14455
Reviewed-by: Giacomo Travaglini <***@arm.com>
Maintainer: Gabe Black <***@google.com>
---
M src/base/types.hh
1 file changed, 54 insertions(+), 0 deletions(-)

Approvals:
Giacomo Travaglini: Looks good to me, approved
Gabe Black: Looks good to me, approved



diff --git a/src/base/types.hh b/src/base/types.hh
index a53f4db..c02a606 100644
--- a/src/base/types.hh
+++ b/src/base/types.hh
@@ -168,6 +168,60 @@
typedef uint64_t RegVal;
typedef double FloatRegVal;

+static inline uint32_t
+floatToBits32(float val)
+{
+ union
+ {
+ float f;
+ uint32_t i;
+ } u;
+ u.f = val;
+ return u.i;
+}
+
+static inline uint64_t
+floatToBits64(double val)
+{
+ union
+ {
+ double f;
+ uint64_t i;
+ } u;
+ u.f = val;
+ return u.i;
+}
+
+static inline uint64_t floatToBits(double val) { return
floatToBits64(val); }
+static inline uint32_t floatToBits(float val) { return floatToBits32(val);
}
+
+static inline float
+bitsToFloat32(uint32_t val)
+{
+ union
+ {
+ float f;
+ uint32_t i;
+ } u;
+ u.i = val;
+ return u.f;
+}
+
+static inline double
+bitsToFloat64(uint64_t val)
+{
+ union
+ {
+ double f;
+ uint64_t i;
+ } u;
+ u.i = val;
+ return u.f;
+}
+
+static inline double bitsToFloat(uint64_t val) { return
bitsToFloat64(val); }
+static inline float bitsToFloat(uint32_t val) { return bitsToFloat32(val);
}
+
/**
* Thread index/ID type
*/
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/14455
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Ib1f35a7e88e00806a7c639c211c5699b4af5a472
Gerrit-Change-Number: 14455
Gerrit-PatchSet: 3
Gerrit-Owner: Gabe Black <***@google.com>
Gerrit-Reviewer: Andreas Sandberg <***@arm.com>
Gerrit-Reviewer: Gabe Black <***@google.com>
Gerrit-Reviewer: Giacomo Travaglini <***@arm.com>
Gerrit-Reviewer: Jason Lowe-Power <***@lowepower.com>
Gerrit-MessageType: merged
Continue reading on narkive:
Loading...