blob: 7995bd82328d0c7196ba7ddf49e69ab047488a54 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/*
* Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com>
* SPDX-License-Identifier: MIT
*/
#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;
}
|