aboutsummaryrefslogtreecommitdiffstats
path: root/ieee754.c
diff options
context:
space:
mode:
Diffstat (limited to 'ieee754.c')
-rw-r--r--ieee754.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/ieee754.c b/ieee754.c
new file mode 100644
index 0000000..3b21351
--- /dev/null
+++ b/ieee754.c
@@ -0,0 +1,39 @@
+#include <assert.h>
+#include <math.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "ieee754.h"
+
+// ieee754f: convert f32 to float
+float ieee754f(unsigned long b)
+{
+ bool isneg;
+ int exp;
+ float n;
+
+ isneg = (b >> 31) & 0x1;
+ exp = (b >> 23) & 0xff;
+ n = b & 0x7fffff;
+
+ if (exp == 0xff) {
+ if (n) {
+ /* TODO: work out what a negative NaN means */
+ assert(!isneg);
+ return NAN;
+ } else {
+ return isneg ? -INFINITY : INFINITY;
+ }
+ } else if (exp == 0) {
+ if (n == 0)
+ return isneg ? -0 : 0;
+ exp = -126;
+ } else {
+ n += 0x1p23f;
+ exp -= 127;
+ }
+
+ n = ldexpf(n, exp - 23);
+
+ return isneg ? -n : n;
+}