-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfloat16.js
180 lines (152 loc) · 5.47 KB
/
float16.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function decodebfloat16(bitstring) {
var out = {
'bitstring' : bitstring,
'value' : undefined,
'pzero' : false,
'nzero' : false,
'pnan' : false,
'nnan' : false,
'pinfinity' : false,
'ninfinity' : false
};
var sgnbits = bitstring[0]
var expbits = bitstring.slice(1,9).join("")
var subbits = bitstring.slice(1,9).join("")
var frabits = bitstring.slice(9, 16).join("")
var sgn = parseInt(sgnbits);
var exp = parseInt(expbits, 2) - 127
var sub = (parseInt(subbits, 2) == 0)
var fra = (1.0-sub) + parseInt(frabits, 2) / 2**7
var value = 2**exp * fra;
if (sgn == 1) {
value = -value;
}
var pzero = bitstring.join("") == "0_00000000_0000000";
var nzero = bitstring.join("") == "1_00000000_0000000";
var pinf = bitstring.join("") == "0_11111111_0000000";
var ninf = bitstring.join("") == "1_11111111_0000000";
var pnan = (bitstring.slice(0, 9).join("") == "0_11111111") & !pinf;
var nnan = (bitstring.slice(0, 9).join("") == "1_11111111") & !ninf;
out.bitstring = bitstring;
out.value = value;
out.pzero = pzero;
out.nzero = nzero;
out.pinfinity = pinf;
out.ninfinity = ninf;
out.pnan = pnan;
out.nnan = nnan;
//console.log(
// "sign: " + sgn + ": " + sgnbits
// + " exp: " + exp + ": " + expbits
// + " sub: " + sub + ": " + subbits
// + " fra: " + fra + ": " + frabits
// + " val: " + value.toFixed(2) + ": " + bitstring
// );
return out
}
//console.log(decodefloat16([0, 1,0,0,1,0,1,0,0, 1,1,0,0,0,1,1]))
function decodefloat16(bitstring) {
var out = {
'bitstring' : bitstring,
'value' : undefined,
'pzero' : false,
'nzero' : false,
'pnan' : false,
'nnan' : false,
'pinfinity' : false,
'ninfinity' : false
};
var sgnbits = bitstring[0]
var expbits = bitstring.slice(1,6).join("")
var subbits = bitstring.slice(1,6).join("")
var frabits = bitstring.slice(6, 16).join("")
var sgn = parseInt(sgnbits, 2);
var exp = parseInt(expbits, 2) - 15
var sub = (parseInt(subbits, 2) == 0)
var fra = (1.0-sub) + parseInt(frabits, 2) / 2**10
var value = 2**exp * fra;
if (sgn == 1) {
value = -value;
}
var pzero = bitstring.join("") == "0_00000_0000000000";
var nzero = bitstring.join("") == "1_00000_0000000000";
var pinf = bitstring.join("") == "0_11111_1111111111";
var ninf = bitstring.join("") == "1_11111_1111111111";
var pnan = (bitstring.slice(0, 6).join("") == "0_11111") & !pinf;
var nnan = (bitstring.slice(0, 6).join("") == "1_11111") & !ninf;
out.bitstring = bitstring;
out.value = value;
out.pzero = pzero;
out.nzero = nzero;
out.pinfinity = pinf;
out.ninfinity = ninf;
out.pnan = pnan;
out.nnan = nnan;
//console.log(
// "sign: " + sgn + ": " + sgnbits
// + " exp: " + exp + ": " + expbits
// + " sub: " + sub + ": " + subbits
// + " fra: " + fra + ": " + frabits
// + " val: " + value.toFixed(2) + ": " + bitstring
// );
return out
}
//console.log(decodefloat16([0, 1,0,0,1,0, 1,0,0,1,1,0,0,0,1,1]))
function bFloatCompare(bfloat1, bfloat2) {
return bfloat1.value - bfloat2.value;
};
function generateBFloats() {
var arr = new Array(2**16);
for (var i = 0; i < arr.length; i++) {
var bitstring = i.toString(2).padStart(16, '0').split('');
var bitarray = bitstring.map(bit => parseInt(bit));
arr[i] = decodebfloat16(bitarray);
}
const positiveBfloat = arr.filter(bfloat => bfloat.value > 0).sort(bFloatCompare);
const negativeBfloat = arr.filter(bfloat => bfloat.value < 0).sort(bFloatCompare);
// Order the negative posits by closeness to zero, since that matches
// the order that we draw them
negativeBfloat.reverse();
const zeroBfloat = arr.filter(p => p.pzero || p.nzero);
const infinity = arr.filter(p => p.pinfinity || p.ninfinity);
console.assert(zeroBfloat && infinity);
console.assert(positiveBfloat.length === negativeBfloat.length);
// NANs mess this up
//console.assert(negativeBfloat.length + negativeBfloat.length + 2 === 2**16);
return {
pos: positiveBfloat,
neg: negativeBfloat,
zero: zeroBfloat,
inf: infinity
}
}
function floatCompare(float1, float2) {
return float1.value - float2.value;
};
function generateFloats() {
var arr = new Array(2**16);
for (var i = 0; i < arr.length; i++) {
var bitstring = i.toString(2).padStart(16, '0').split('');
var bitarray = bitstring.map(bit => parseInt(bit));
arr[i] = decodefloat16(bitarray);
}
const positiveFloat = arr.filter(float => float.value > 0).sort(floatCompare);
const negativeFloat = arr.filter(float => float.value < 0).sort(floatCompare);
// Order the negative posits by closeness to zero, since that matches
// the order that we draw them
negativeFloat.reverse();
const zeroFloat = arr.filter(p => p.pzero || p.nzero);
const infinity = arr.filter(p => p.pinfinity || p.ninfinity);
console.assert(zeroFloat && infinity);
console.assert(positiveFloat.length === negativeFloat.length);
// NANs mess this up
//console.assert(negativeFloat.length + negativeFloat.length + 2 === 2**16);
return {
pos: positiveFloat,
neg: negativeFloat,
zero: zeroFloat,
inf: infinity
}
}
generateFloats()
generateBFloats()