-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
161 lines (131 loc) · 4.92 KB
/
Copy pathscript.js
File metadata and controls
161 lines (131 loc) · 4.92 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
"use strict"
function range(min, max, step) {
if (step == null && max == null) {
max = min
min = 0
}
if (step == null)
step = 1
const l = []
for (let i = min; i != max; i += step)
l.push(i)
return l
}
function group(array, size) {
const l = []
for (let i = 0; i < array.length; i++)
l.push(array.slice(i,i+size))
}
function leftpad(s,n,c) {
while (s.length < n)
s = `${c}${s}`
return s
}
const set_float_versions = {
16: DataView.prototype.setFloat16,
32: DataView.prototype.setFloat32,
64: DataView.prototype.setFloat64
}
const get_float_versions = {
16: DataView.prototype.getFloat16,
32: DataView.prototype.getFloat32,
64: DataView.prototype.getFloat64
}
function init(bits) {
const sign_size = 1
const exponent_size = {16: 5, 32: 8, 64: 11}[bits]
const mantissa_size = bits-exponent_size-sign_size
const set_float = set_float_versions[bits]
const get_float = get_float_versions[bits]
const sign_element = document.querySelector(`#f${bits}-s0`)
const exponent_elements = range(exponent_size).map(i => document.querySelector(`#f${bits}-e${i}`))
const mantissa_elements = range(mantissa_size).map(i => document.querySelector(`#f${bits}-m${i}`))
const elements = [sign_element].concat(exponent_elements, mantissa_elements)
const sign_value_element = document.querySelector(`#f${bits}-s-val`)
const exponent_value_element = document.querySelector(`#f${bits}-e-val`)
const mantissa_value_element = document.querySelector(`#f${bits}-m-val`)
const sign_encoded_element = document.querySelector(`#f${bits}-s-enc`)
const exponent_encoded_element = document.querySelector(`#f${bits}-e-enc`)
const mantissa_encoded_element = document.querySelector(`#f${bits}-m-enc`)
const decimal_element = document.querySelector(`#f${bits}-dec`)
const binary_element = document.querySelector(`#f${bits}-bin`)
const hexadecimal_element = document.querySelector(`#f${bits}-hex`)
for (const e of elements)
e.onchange = recompute_from_checkboxes
decimal_element.onchange = recompute_from_dec
binary_element.onchange = recompute_from_bin
hexadecimal_element.onchange = recompute_from_hex
function big_to_float(big) {
const buf = new ArrayBuffer(8)
const dv = new DataView(buf)
dv.setBigUint64(0, big, true)
const f = get_float.call(dv, 0, true)
return f
}
function float_to_big(float) {
const buf = new ArrayBuffer(8)
const dv = new DataView(buf)
dv.setBigUint64(0, 0n, true)
set_float.call(dv, 0, float, true)
const big = dv.getBigUint64(0, true)
return big
}
function from_sem(sign, exponent, mantissa) {
sign = BigInt(sign)
exponent = BigInt(exponent)
mantissa = BigInt(mantissa)
const big = sign << BigInt(exponent_size+mantissa_size) | exponent << BigInt(mantissa_size) | mantissa
const float = big_to_float(big)
return {sign, exponent, mantissa, big, float}
}
function from_big(big) {
big = BigInt(big)
const sign = (big >> BigInt(exponent_size+mantissa_size)) & 1n
const exponent = (big >> BigInt(mantissa_size)) & ((1n << BigInt(exponent_size)) - 1n)
const mantissa = (big) & ((1n << BigInt(mantissa_size)) - 1n)
const float = big_to_float(big)
return {sign, exponent, mantissa, big, float}
}
function from_float(float) {
return from_big(float_to_big(float))
}
function recompute_from_checkboxes() {
let sign = BigInt(+sign_element.checked)
let exponent = exponent_elements.map((e,i) => BigInt(+e.checked) << BigInt(i)).reduce((a,b) => a|b, BigInt(0))
let mantissa = mantissa_elements.map((e,i) => BigInt(+e.checked) << BigInt(i)).reduce((a,b) => a|b, BigInt(0))
render(from_sem(sign, exponent, mantissa))
}
function recompute_from_dec() {
const t = decimal_element.value
const f = Number.parseFloat(t)
render(from_float(f))
}
function recompute_from_bin() {
const t = binary_element.value
const b = BigInt(`0b${t}`)
render(from_big(b))
}
function recompute_from_hex() {
const t = hexadecimal_element.value
const b = BigInt(`0x${t}`)
render(from_big(b))
}
function render({sign, exponent, mantissa, big, float}) {
sign_element.checked = sign
exponent_elements.forEach((e,i) => e.checked = (exponent >> BigInt(i)) & 1n)
mantissa_elements.forEach((e,i) => e.checked = (mantissa >> BigInt(i)) & 1n)
sign_encoded_element.innerText = sign
exponent_encoded_element.innerText = exponent
mantissa_encoded_element.innerText = mantissa
sign_value_element.innerText = sign == 0 ? "+1" : "-1"
exponent_value_element.innerText = exponent - ((1n << BigInt(exponent_size-1)) - 1n)
mantissa_value_element.innerText = 1+Number(mantissa)*Math.pow(2,-mantissa_size)
decimal_element.value = 1 / float === -Infinity ? "-0" : float
binary_element.value = leftpad(big.toString(2), bits, "0")
hexadecimal_element.value = leftpad(big.toString(16), bits/4, "0")
}
render(from_big(0))
}
init(16)
init(32)
init(64)