-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathHalfPrecisionFloatTest.cpp
115 lines (96 loc) · 2.21 KB
/
HalfPrecisionFloatTest.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "umHalf.h"
#include <iostream>
#include <assert.h>
#define VALIDATE(x) if (!(x)){std::cout << "Failed: " << #x << std::endl;assert((x));}
int main(int argc, char* argv[])
{
half h = 1.f, h2 = 2.f;
--h2;
++h2;
--h;
++h;
h2 -= 1.f;
float f = h2, f2 = h;
VALIDATE(1.f == f && f == f2);
h = h2;
h2 = 15.5f;
f = h2, f2 = h;
VALIDATE(15.5f == f && 1.f == f2);
h2 *= h;
f = h2, f2 = h;
VALIDATE(15.5f == f && 1.f == f2);
h2 /= h;
f = h2, f2 = h;
VALIDATE(15.5f == f && 1.f == f2);
h2 += h;
f = h2, f2 = h;
VALIDATE(16.5f == f && 1.f == f2);
h++;h++;h++;
h2 = -h2;
h2 += 17.5f;
h2 *= h;
f = h2, f2 = h;
VALIDATE(4.f == f && 4.f == f2);
VALIDATE(h == h2);
VALIDATE(h <= h2);
--h;
VALIDATE(h <= h2);
h -= 250.f;
VALIDATE(h < h2);
h += 500.f;
VALIDATE(h > h2);
VALIDATE(h >= h2);
f = h2, f2 = h;
VALIDATE(h * h2 == (half)(f * f2));
// addition
// ****************************************************************************
// identical exponents
for (float f = 0.f; f < 1000.f; ++f)
{
half one = f;
half two = f;
half three = one + two;
f2 = three;
VALIDATE(f*2.f == f2);
}
// different exponents
for (float f = 0.f, fp = 1000.f; f < 500.f; ++f, --fp)
{
half one = f;
half two = fp;
half three = one + two;
f2 = three;
VALIDATE(f+fp == f2);
}
// very small numbers - this is already beyond the accuracy of 16 bit floats.
for (float f = 0.003f; f < 1000.f; f += 0.0005f)
{
half one = f;
half two = f;
half three = one + two;
f2 = three;
float m = f*2.f;
VALIDATE(f2 > (m-0.05*m) && f2 < (m+0.05*m));
}
// subtraction
// ****************************************************************************
// identical exponents
for (float f = 0.f; f < 1000.f; ++f)
{
half one = f;
half two = f;
half three = one - two;
f2 = three;
VALIDATE(0.f == f2);
}
// different exponents
for (float f = 0.f, fp = 1000.f; f < 500.f; ++f, --fp)
{
half one = f;
half two = fp;
half three = one - two;
f2 = three;
VALIDATE(f-fp == f2);
}
return 0;
}