blob: 789ff2946a695bc77b9e5cc35fcd0c9ea1423e2e (
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
|
/*
* 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) {
return NAN;
} else {
return isneg ? -INFINITY : INFINITY;
}
} else if (exp == 0) {
if (n == 0)
return isneg ? -0.0 : 0.0;
exp = -126;
} else {
n += 0x1p23f;
exp -= 127;
}
n = ldexpf(n, exp - 23);
return isneg ? -n : n;
}
|