-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
298 lines (255 loc) · 11.2 KB
/
index.html
File metadata and controls
298 lines (255 loc) · 11.2 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Float Viewer</title>
<meta name="description" content="An interactive tool to visualize and explore floating-point representation, IEEE 754 format, bit patterns and precision limits.">
<meta name="keywords" content="floating point, float, IEEE 754, binary representation, sign, exponent, mantissa, significand, visualization, interactive tool, precision, rounding, computer science, number representation">
<meta name="author" content="Michal W. Tarnowski">
<link rel="icon" href="favicon.ico" sizes="any">
<link rel="icon" href="favicon.svg" type="image/svg+xml" sizes="any">
<link rel="preload" href="fonts/RedditMono%5Bwght%5D.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="style.css">
<script src="float.js" defer></script>
</head>
<body>
<header>
<div class="header-left">
<svg class="site-logo" viewBox="0 0 32 32">
<path d="M2 26 H6 V30 H2 V26 Z M14 6 V2 H26 V6 H30 V24 H30 V26 H26 V30 H14 V26 H10 V6 H14 Z M14 6 V10 H18 V14 H22 V18 H26 V6 H14 Z M14 26 H26 V22 H22 V18 H18 V14 H14 V26 Z"/>
</svg>
<h1 class="site-title">Float Viewer</h1>
</div>
</header>
<main>
<div class="main-content">
<div id="bit-container" class="bit-container">
<!-- bit-cell elements here -->
</div>
<div class="hline"></div>
<div class="field-container">
<div class="field-group">
<label class="field-label" for="flt-input">Value:</label>
<div class="field-control">
<input class="field-input" type="text" id="flt-input">
</div>
</div>
<div class="field-group">
<label class="field-label" for="raw-input">Hex Raw:</label>
<div class="field-control">
<input class="field-input" type="text" id="raw-input" data-radix="16">
<button class="field-button" type="button" id="raw-dec-btn">−</button>
<button class="field-button" type="button" id="raw-inc-btn">+</button>
</div>
</div>
<div class="field-group">
<label class="field-label" for="sgn-input">Sign:</label>
<div class="field-control">
<input class="field-input" type="text" id="sgn-input" data-part="sgn" data-radix="10">
<button class="field-button" type="button" id="sgn-dec-btn">−</button>
<button class="field-button" type="button" id="sgn-inc-btn">+</button>
</div>
</div>
<div class="field-group">
<label class="field-label" for="exp-input">Exponent:</label>
<div class="field-control">
<input class="field-input" type="text" id="exp-input" data-part="exp" data-radix="10">
<button class="field-button" type="button" id="exp-dec-btn">−</button>
<button class="field-button" type="button" id="exp-inc-btn">+</button>
</div>
</div>
<div class="field-group">
<label class="field-label" for="man-input">Significand:</label>
<div class="field-control">
<input class="field-input" type="text" id="man-input" data-part="man" data-radix="10">
<button class="field-button" type="button" id="man-dec-btn">−</button>
<button class="field-button" type="button" id="man-inc-btn">+</button>
</div>
</div>
<div class="field-group">
<label class="field-label" for="ulp-input">ULP:</label>
<div class="field-control">
<input class="field-input" type="text" id="ulp-input" readonly>
</div>
</div>
</div>
<div class="hline"></div>
<div class="format-selector">
<form id="format-form" class="format-form">
<label for="format-select">Format:</label>
<select id="format-select" name="format" required>
<option value="" selected>-- Choose a format --</option>
<option value="fp16">Half (fp16)</option>
<option value="bf16">Bfloat16 (bf16)</option>
<option value="fp32">Single (fp32)</option>
<option value="fp64">Double (fp64)</option>
<option value="custom">Custom</option>
</select>
<div id="custom-fields" class="format-custom" style="display: none;">
<label for="exp-bits"><abbr title="Exponent Bits">E</abbr></label>
<input type="number" id="exp-bits" name="exponent" min="1" max="11" step="1" placeholder="8">
<label for="man-bits"><abbr title="Mantissa Bits">M</abbr></label>
<input type="number" id="man-bits" name="mantissa" min="1" max="52" step="1" placeholder="23">
</div>
<button type="submit">Apply</button>
</form>
</div>
</div>
</main>
<footer>
<p class="footnote">
<a href="https://github.com/mwtarnowski/float-viewer">Source</a> © <time datetime="2023">2023</time> Michal W. Tarnowski
</p>
</footer>
<script>
document.addEventListener('DOMContentLoaded', () => {
const $ = id => document.getElementById(id);
const toggleValid = (input, valid) => input.classList.toggle('invalid', !valid);
const bitContainer = $('bit-container');
const inputs = {
flt: $('flt-input'),
raw: $('raw-input'),
sgn: $('sgn-input'),
exp: $('exp-input'),
man: $('man-input'),
ulp: $('ulp-input'),
};
const buttons = {
raw: { dec: $('raw-dec-btn'), inc: $('raw-inc-btn') },
sgn: { dec: $('sgn-dec-btn'), inc: $('sgn-inc-btn') },
exp: { dec: $('exp-dec-btn'), inc: $('exp-inc-btn') },
man: { dec: $('man-dec-btn'), inc: $('man-inc-btn') },
};
const formatForm = $('format-form');
const formatSelect = $('format-select');
const customFields = $('custom-fields');
const expBitsInput = $('exp-bits');
const manBitsInput = $('man-bits');
const fp = new FloatingPoint(FP32);
let bitButtons = [];
function generateBitGrid(format) {
const totalBits = Number(format.totalBits);
const expIndex = Number(format.mantissaBits);
const sgnIndex = Number(format.mantissaBits + format.exponentBits);
bitButtons = [];
const grid = document.createDocumentFragment();
for (let index = totalBits - 1; index >= 0; index--) {
const bitCell = document.createElement('div');
bitCell.classList.add('bit-cell');
const bitIdx = document.createElement('div');
bitIdx.classList.add('bit-idx');
bitIdx.textContent = index.toString().padStart(2, '0');
bitCell.appendChild(bitIdx);
const bitBtn = document.createElement('button');
bitBtn.classList.add('bit-btn');
bitBtn.dataset.index = index;
bitBtn.dataset.part = index < expIndex ? 'man' : index < sgnIndex ? 'exp' : 'sgn';
bitBtn.dataset.value = '0';
bitBtn.textContent = '0';
bitButtons.push(bitBtn);
bitCell.appendChild(bitBtn);
grid.append(bitCell);
}
bitContainer.replaceChildren(grid);
}
let fieldUpdate = {};
function updateFields() {
for (const key in fieldUpdate) fieldUpdate[key]();
inputs.ulp.value = fp.getUlpString();
}
function updateBitGrid() {
bitButtons.forEach(btn => {
const index = Number(btn.dataset.index);
const value = fp.getBit(index);
btn.textContent = btn.dataset.value = value;
});
}
function updateAll() {
updateFields();
updateBitGrid();
}
const configs = {
flt: { parse: str => str, isval: isValidFloatString,
get: fp.getString.bind(fp), set: fp.setString.bind(fp) },
raw: { parse: parseBigUint, isval: fp.isValidRaw.bind(fp),
get: fp.getRaw.bind(fp), set: fp.setRaw.bind(fp), inc: fp.incRaw.bind(fp) },
sgn: { parse: parseBigUint, isval: fp.isValidSign.bind(fp),
get: fp.getSign.bind(fp), set: fp.setSign.bind(fp), inc: fp.incSign.bind(fp) },
exp: { parse: parseBigUint, isval: fp.isValidExponent.bind(fp),
get: fp.getExponent.bind(fp), set: fp.setExponent.bind(fp), inc: fp.incExponent.bind(fp) },
man: { parse: parseBigUint, isval: fp.isValidMantissa.bind(fp),
get: fp.getMantissa.bind(fp), set: fp.setMantissa.bind(fp), inc: fp.incMantissa.bind(fp) },
};
for (const key in configs) {
const input = inputs[key];
const radix = input.dataset.radix;
const { parse, isval, get, set, inc } = configs[key];
const check = () => { toggleValid(input, isval(parse(input.value, radix))); };
const update = () => { input.value = get().toString(radix); toggleValid(input, true); };
const commit = () => { set(parse(input.value, radix)) ? updateAll() : update(); };
input.addEventListener('input', check);
input.addEventListener('keydown', (e) => e.key === 'Enter' && commit());
input.addEventListener('blur', update);
if (inc) {
const step = (neg) => { inc(neg); updateAll(); };
input.addEventListener('keydown', (e) => {
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
e.preventDefault();
step(e.key === 'ArrowDown');
}
});
const btns = buttons[key];
btns?.dec.addEventListener('click', () => step(true));
btns?.inc.addEventListener('click', () => step(false));
}
fieldUpdate[key] = update;
}
bitContainer.addEventListener('click', (e) => {
const btn = e.target.closest('button.bit-btn');
if (!btn) return;
const index = Number(btn.dataset.index);
const value = 1 ^ Number(btn.dataset.value);
btn.textContent = btn.dataset.value = value;
fp.setBit(index, value);
updateFields();
});
function setFormat(format) {
fp.setFormat(format);
generateBitGrid(format);
updateAll();
}
const predefinedFormats = {
fp16: FP16, bf16: BF16, fp32: FP32, fp64: FP64,
};
function updateCustomFields() {
if (formatSelect.value === 'custom') {
customFields.style.display = 'flex';
expBitsInput.required = true;
manBitsInput.required = true;
} else {
customFields.style.display = 'none';
expBitsInput.required = false;
manBitsInput.required = false;
}
}
formatSelect.addEventListener('change', updateCustomFields);
formatForm.addEventListener('submit', (e) => {
e.preventDefault();
const selected = formatSelect.value;
const format = predefinedFormats[selected];
if (format) {
setFormat(format);
} else if (selected === 'custom') {
const expBits = parseInt(expBitsInput.value);
const manBits = parseInt(manBitsInput.value);
const format = new Format(expBits, manBits);
setFormat(format);
}
});
setFormat(FP32);
updateCustomFields();
});
</script>
</body>
</html>