-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlatform.cpp
More file actions
86 lines (75 loc) · 1.41 KB
/
Platform.cpp
File metadata and controls
86 lines (75 loc) · 1.41 KB
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
//-----------------------------------------------------------------------------
// File: Framework\Platform.cpp
// Copyright (c) 2007 Advanced Micro Devices Inc. All rights reserved.
//-----------------------------------------------------------------------------
#include "Platform.h"
// 16-bit floats
half::half(const float x)
{
unsigned int i = *((unsigned int *) &x);
int e = ((i >> 23) & 0xFF) - 112;
int m = i & 0x007FFFFF;
sh = (i >> 16) & 0x8000;
if (e <= 0)
{
// Denormed half
m = ((m | 0x00800000) >> (1 - e)) + 0x1000;
sh |= (m >> 13);
}
else if (e == 255 - 112)
{
sh |= 0x7C00;
if (m != 0)
{
// NAN
m >>= 13;
sh |= m | (m == 0);
}
}
else
{
m += 0x1000;
if (m & 0x00800000)
{
// Mantissa overflow
m = 0;
e++;
}
if (e >= 31)
{
// Exponent overflow
sh |= 0x7C00;
}
else
{
sh |= (e << 10) | (m >> 13);
}
}
}
half::operator float () const
{
unsigned int s = (sh & 0x8000) << 16;
unsigned int e = (sh >> 10) & 0x1F;
unsigned int m = sh & 0x03FF;
if (e == 0)
{
// +/- 0
if (m == 0) return *((float *) &s);
// Denorm
while ((m & 0x0400) == 0)
{
m += m;
e--;
}
e++;
m &= ~0x0400;
}
else if (e == 31)
{
// INF / NAN
s |= 0x7F800000 | (m << 13);
return *((float *) &s);
}
s |= ((e + 112) << 23) | (m << 13);
return *((float *) &s);
}