gofront 1.8.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +6 -5
- package/src/backend/js/expressions.js +1157 -408
- package/src/backend/js/index.js +327 -153
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +559 -2552
- package/src/backend/js/statements.js +670 -158
- package/src/backend/js/stdlib/index.js +45 -93
- package/src/backend/js/templ.js +32 -45
- package/src/backend/js/type-info.js +298 -19
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +91 -170
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +490 -965
- package/src/backend/wasm/emit-builtins.js +884 -256
- package/src/backend/wasm/emit-exprs.js +185 -77
- package/src/backend/wasm/emit-maps.js +17 -367
- package/src/backend/wasm/emit-shared.js +4 -1
- package/src/backend/wasm/emit-stmts.js +524 -240
- package/src/backend/wasm/emit.js +176 -38
- package/src/backend/wasm/globals.js +48 -8
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +44 -530
- package/src/backend/wasm/method-expr.js +12 -1
- package/src/backend/wasm/module-emitter.js +299 -770
- package/src/backend/wasm/monomorph.js +139 -8
- package/src/backend/wasm/natives.js +600 -0
- package/src/backend/wasm/prescan.js +5 -153
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -32
- package/src/backend/wasm/types.js +97 -20
- package/src/backend/wasm/variadic.js +9 -4
- package/src/cli/cli-core.js +5 -1
- package/src/cli/test-runner.js +115 -342
- package/src/compiler.js +113 -130
- package/src/lexer/lexer.js +106 -11
- package/src/lexer/templ-lexer.js +61 -18
- package/src/lexer/tokens.js +1 -0
- package/src/link/stdlib.js +355 -132
- package/src/link/type-names.js +204 -0
- package/src/link/wasm.js +30 -6
- package/src/lower/bounds.js +153 -0
- package/src/lower/captures.js +92 -33
- package/src/lower/embedding.js +62 -5
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +929 -0
- package/src/lower/functions.js +225 -7
- package/src/lower/gen-types.js +293 -0
- package/src/lower/index.js +14 -2
- package/src/lower/json-codecs.js +334 -245
- package/src/lower/ownership.js +25 -7
- package/src/lower/reachability.js +69 -51
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +30 -5
- package/src/parser/expressions.js +111 -57
- package/src/parser/statements.js +108 -0
- package/src/parser/templ.js +44 -5
- package/src/parser/types.js +12 -0
- package/src/resolver.js +1 -2
- package/src/typechecker/assignability.js +214 -19
- package/src/typechecker/const-fold.js +70 -27
- package/src/typechecker/constants.js +156 -8
- package/src/typechecker/expressions.js +490 -108
- package/src/typechecker/index.js +346 -42
- package/src/typechecker/init-order.js +136 -40
- package/src/typechecker/operand-text.js +41 -0
- package/src/typechecker/resolve.js +490 -65
- package/src/typechecker/statements.js +288 -27
- package/src/typechecker/stdlib/excluded.js +9 -5
- package/src/typechecker/stdlib/index.js +25 -9
- package/src/typechecker/termination.js +10 -1
- package/src/typechecker/types.js +287 -575
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/context/context.go +115 -0
- package/stdlib/crypto/rand/rand.go +34 -0
- package/stdlib/crypto/sha256/sha256.go +249 -0
- package/stdlib/encoding/base64/base64.go +331 -0
- package/stdlib/encoding/binary/binary.go +332 -0
- package/stdlib/encoding/hex/hex.go +140 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/errors/errors.go +174 -0
- package/stdlib/fmt/errors.go +110 -0
- package/stdlib/fmt/format.go +732 -0
- package/stdlib/fmt/print.go +1232 -0
- package/stdlib/gom/gom.go +293 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +171 -38
- package/stdlib/internal/jsonrt/enc.go +186 -36
- package/stdlib/internal/jsonrt/scanner.go +17 -8
- package/stdlib/io/io.go +157 -1
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +16 -14
- package/stdlib/math/math.go +439 -0
- package/stdlib/math/rand/rand.go +297 -0
- package/stdlib/os/os.go +93 -0
- package/stdlib/path/filepath/filepath.go +47 -0
- package/stdlib/path/path.go +394 -0
- package/stdlib/regexp/regexp.go +231 -0
- package/stdlib/slices/slices.go +526 -0
- package/stdlib/slices/sort.go +209 -0
- package/stdlib/slices/zsort_cmp.go +480 -0
- package/stdlib/slices/zsort_ordered.go +482 -0
- package/stdlib/sort/sort.go +391 -452
- package/stdlib/sort/zsort_func.go +480 -0
- package/stdlib/sort/zsort_iface.go +480 -0
- package/stdlib/strconv/float.go +1699 -0
- package/stdlib/strconv/isprint.go +752 -0
- package/stdlib/strconv/quote.go +235 -50
- package/stdlib/strconv/strconv.go +616 -0
- package/stdlib/strings/builder.go +95 -0
- package/stdlib/strings/reader.go +163 -0
- package/stdlib/strings/replacer.go +102 -0
- package/stdlib/strings/strings.go +807 -0
- package/stdlib/sync/sync.go +268 -0
- package/stdlib/testing/async_js.go +61 -0
- package/stdlib/testing/benchmark.go +297 -0
- package/stdlib/testing/testing.go +523 -0
- package/stdlib/time/duration.go +233 -0
- package/stdlib/time/format.go +745 -0
- package/stdlib/time/parse.go +790 -0
- package/stdlib/time/sleep_js.go +63 -0
- package/stdlib/time/time.go +834 -0
- package/stdlib/unicode/unicode.go +141 -3
- package/stdlib/unicode/utf8/utf8.go +464 -0
- package/src/backend/js/fmt-runtime.js +0 -573
- package/src/backend/js/stdlib/base64.js +0 -12
- package/src/backend/js/stdlib/binary.js +0 -102
- package/src/backend/js/stdlib/bits.js +0 -119
- package/src/backend/js/stdlib/builder.js +0 -98
- package/src/backend/js/stdlib/bytes.js +0 -368
- package/src/backend/js/stdlib/errors.js +0 -45
- package/src/backend/js/stdlib/fmt.js +0 -517
- package/src/backend/js/stdlib/gom.js +0 -155
- package/src/backend/js/stdlib/hex.js +0 -17
- package/src/backend/js/stdlib/html.js +0 -20
- package/src/backend/js/stdlib/io.js +0 -137
- package/src/backend/js/stdlib/json.js +0 -1167
- package/src/backend/js/stdlib/maps.js +0 -37
- package/src/backend/js/stdlib/math.js +0 -85
- package/src/backend/js/stdlib/os.js +0 -13
- package/src/backend/js/stdlib/path.js +0 -48
- package/src/backend/js/stdlib/rand.js +0 -32
- package/src/backend/js/stdlib/regexp.js +0 -54
- package/src/backend/js/stdlib/sha256.js +0 -12
- package/src/backend/js/stdlib/shared.js +0 -17
- package/src/backend/js/stdlib/simd.js +0 -34
- package/src/backend/js/stdlib/slices.js +0 -107
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -79
- package/src/backend/js/stdlib/strings.js +0 -301
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -41
- package/src/backend/js/stdlib/time.js +0 -152
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/stdlib/bytes.js +0 -6716
- package/src/backend/wasm/stdlib/core.js +0 -10278
- package/src/backend/wasm/stdlib/io.js +0 -3578
- package/src/backend/wasm/stdlib/packages.js +0 -2402
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -2229
- package/src/typechecker/stdlib/core.js +0 -1079
- package/src/typechecker/stdlib/extended.js +0 -1210
- package/stdlib/bytes/iter.go +0 -192
- package/stdlib/bytes/search.go +0 -240
- package/stdlib/bytes/split.go +0 -218
- package/stdlib/errors/overlay.go +0 -12
- package/stdlib/maps/iter.go +0 -65
- package/stdlib/slices/extra.go +0 -73
- package/stdlib/slices/iter.go +0 -113
- package/stdlib/strings/extra.go +0 -35
- package/stdlib/strings/iter.go +0 -122
- package/stdlib/sync/extra.go +0 -105
- package/stdlib/unicode/utf8/bytes.go +0 -239
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Copyright 2017 The Go Authors. All rights reserved.
|
|
6
|
+
// Use of this source code is governed by a BSD-style
|
|
7
|
+
// license that can be found in the LICENSE file.
|
|
8
|
+
|
|
9
|
+
// Package bits implements bit counting and manipulation functions for the
|
|
10
|
+
// predeclared unsigned integer types.
|
|
11
|
+
//
|
|
12
|
+
// Go's lookup tables (len8tab, ntz8tab, pop8tab, rev8tab) are replaced by
|
|
13
|
+
// shift arithmetic with the same results. The Len, LeadingZeros,
|
|
14
|
+
// TrailingZeros and OnesCount functions carry host natives where the host
|
|
15
|
+
// has a single instruction for them.
|
|
16
|
+
package bits
|
|
17
|
+
|
|
18
|
+
// UintSize is the size of a uint in bits.
|
|
19
|
+
const UintSize = 64
|
|
20
|
+
|
|
21
|
+
// Go panics with a runtime.Error; GoFront's recovered run-time errors are
|
|
22
|
+
// their message, so these are the same strings.
|
|
23
|
+
const (
|
|
24
|
+
overflowError = "runtime error: integer overflow"
|
|
25
|
+
divideError = "runtime error: integer divide by zero"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
// len8 is the bit length of x, which is below 256.
|
|
29
|
+
func len8(x uint32) int {
|
|
30
|
+
n := 0
|
|
31
|
+
if x >= 1<<4 {
|
|
32
|
+
x >>= 4
|
|
33
|
+
n = 4
|
|
34
|
+
}
|
|
35
|
+
if x >= 1<<2 {
|
|
36
|
+
x >>= 2
|
|
37
|
+
n += 2
|
|
38
|
+
}
|
|
39
|
+
if x >= 1<<1 {
|
|
40
|
+
x >>= 1
|
|
41
|
+
n++
|
|
42
|
+
}
|
|
43
|
+
if x >= 1 {
|
|
44
|
+
n++
|
|
45
|
+
}
|
|
46
|
+
return n
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// --- LeadingZeros ---
|
|
50
|
+
|
|
51
|
+
// LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
|
|
52
|
+
//
|
|
53
|
+
//gofront:native bits.LeadingZeros
|
|
54
|
+
func LeadingZeros(x uint) int { return 64 - Len64(uint64(x)) }
|
|
55
|
+
|
|
56
|
+
// LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
|
|
57
|
+
//
|
|
58
|
+
//gofront:native bits.LeadingZeros8
|
|
59
|
+
func LeadingZeros8(x uint8) int { return 8 - Len8(x) }
|
|
60
|
+
|
|
61
|
+
// LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
|
|
62
|
+
//
|
|
63
|
+
//gofront:native bits.LeadingZeros16
|
|
64
|
+
func LeadingZeros16(x uint16) int { return 16 - Len16(x) }
|
|
65
|
+
|
|
66
|
+
// LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
|
|
67
|
+
//
|
|
68
|
+
//gofront:native bits.LeadingZeros32
|
|
69
|
+
func LeadingZeros32(x uint32) int { return 32 - Len32(x) }
|
|
70
|
+
|
|
71
|
+
// LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
|
|
72
|
+
//
|
|
73
|
+
//gofront:native bits.LeadingZeros64
|
|
74
|
+
func LeadingZeros64(x uint64) int { return 64 - Len64(x) }
|
|
75
|
+
|
|
76
|
+
// --- TrailingZeros ---
|
|
77
|
+
|
|
78
|
+
// TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
|
|
79
|
+
//
|
|
80
|
+
//gofront:native bits.TrailingZeros
|
|
81
|
+
func TrailingZeros(x uint) int { return TrailingZeros64(uint64(x)) }
|
|
82
|
+
|
|
83
|
+
// TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
|
|
84
|
+
//
|
|
85
|
+
//gofront:native bits.TrailingZeros8
|
|
86
|
+
func TrailingZeros8(x uint8) int {
|
|
87
|
+
if x == 0 {
|
|
88
|
+
return 8
|
|
89
|
+
}
|
|
90
|
+
return len8(uint32(x&-x)) - 1
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
|
|
94
|
+
//
|
|
95
|
+
//gofront:native bits.TrailingZeros16
|
|
96
|
+
func TrailingZeros16(x uint16) int {
|
|
97
|
+
if x == 0 {
|
|
98
|
+
return 16
|
|
99
|
+
}
|
|
100
|
+
return Len16(x&-x) - 1
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
|
|
104
|
+
//
|
|
105
|
+
//gofront:native bits.TrailingZeros32
|
|
106
|
+
func TrailingZeros32(x uint32) int {
|
|
107
|
+
if x == 0 {
|
|
108
|
+
return 32
|
|
109
|
+
}
|
|
110
|
+
return Len32(x&-x) - 1
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
|
|
114
|
+
//
|
|
115
|
+
//gofront:native bits.TrailingZeros64
|
|
116
|
+
func TrailingZeros64(x uint64) int {
|
|
117
|
+
if x == 0 {
|
|
118
|
+
return 64
|
|
119
|
+
}
|
|
120
|
+
return Len64(x&-x) - 1
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// --- OnesCount ---
|
|
124
|
+
|
|
125
|
+
const (
|
|
126
|
+
m0 = 0x5555555555555555 // 01010101 ...
|
|
127
|
+
m1 = 0x3333333333333333 // 00110011 ...
|
|
128
|
+
m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
// OnesCount returns the number of one bits ("population count") in x.
|
|
132
|
+
//
|
|
133
|
+
//gofront:native bits.OnesCount
|
|
134
|
+
func OnesCount(x uint) int { return OnesCount64(uint64(x)) }
|
|
135
|
+
|
|
136
|
+
// OnesCount8 returns the number of one bits ("population count") in x.
|
|
137
|
+
//
|
|
138
|
+
//gofront:native bits.OnesCount8
|
|
139
|
+
func OnesCount8(x uint8) int { return OnesCount32(uint32(x)) }
|
|
140
|
+
|
|
141
|
+
// OnesCount16 returns the number of one bits ("population count") in x.
|
|
142
|
+
//
|
|
143
|
+
//gofront:native bits.OnesCount16
|
|
144
|
+
func OnesCount16(x uint16) int { return OnesCount32(uint32(x)) }
|
|
145
|
+
|
|
146
|
+
// OnesCount32 returns the number of one bits ("population count") in x.
|
|
147
|
+
//
|
|
148
|
+
//gofront:native bits.OnesCount32
|
|
149
|
+
func OnesCount32(x uint32) int {
|
|
150
|
+
const m = 1<<32 - 1
|
|
151
|
+
x = x>>1&(m0&m) + x&(m0&m)
|
|
152
|
+
x = x>>2&(m1&m) + x&(m1&m)
|
|
153
|
+
x = (x>>4 + x) & (m2 & m)
|
|
154
|
+
x += x >> 8
|
|
155
|
+
x += x >> 16
|
|
156
|
+
return int(x) & (1<<6 - 1)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// OnesCount64 returns the number of one bits ("population count") in x.
|
|
160
|
+
//
|
|
161
|
+
//gofront:native bits.OnesCount64
|
|
162
|
+
func OnesCount64(x uint64) int {
|
|
163
|
+
const m = 1<<64 - 1
|
|
164
|
+
x = x>>1&(m0&m) + x&(m0&m)
|
|
165
|
+
x = x>>2&(m1&m) + x&(m1&m)
|
|
166
|
+
x = (x>>4 + x) & (m2 & m)
|
|
167
|
+
x += x >> 8
|
|
168
|
+
x += x >> 16
|
|
169
|
+
x += x >> 32
|
|
170
|
+
return int(x & (1<<7 - 1)) // masked before int(): a uint64 above 2^53 does not survive int() in JS
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// --- RotateLeft ---
|
|
174
|
+
|
|
175
|
+
// RotateLeft returns the value of x rotated left by (k mod UintSize) bits.
|
|
176
|
+
// To rotate x right by k bits, call RotateLeft(x, -k).
|
|
177
|
+
func RotateLeft(x uint, k int) uint { return uint(RotateLeft64(uint64(x), k)) }
|
|
178
|
+
|
|
179
|
+
// RotateLeft8 returns the value of x rotated left by (k mod 8) bits.
|
|
180
|
+
func RotateLeft8(x uint8, k int) uint8 {
|
|
181
|
+
const n = 8
|
|
182
|
+
s := uint(k) & (n - 1)
|
|
183
|
+
return x<<s | x>>(n-s)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// RotateLeft16 returns the value of x rotated left by (k mod 16) bits.
|
|
187
|
+
func RotateLeft16(x uint16, k int) uint16 {
|
|
188
|
+
const n = 16
|
|
189
|
+
s := uint(k) & (n - 1)
|
|
190
|
+
return x<<s | x>>(n-s)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// RotateLeft32 returns the value of x rotated left by (k mod 32) bits.
|
|
194
|
+
func RotateLeft32(x uint32, k int) uint32 {
|
|
195
|
+
const n = 32
|
|
196
|
+
s := uint(k) & (n - 1)
|
|
197
|
+
return x<<s | x>>(n-s)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
|
|
201
|
+
func RotateLeft64(x uint64, k int) uint64 {
|
|
202
|
+
const n = 64
|
|
203
|
+
s := uint(k) & (n - 1)
|
|
204
|
+
return x<<s | x>>(n-s)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// --- Reverse ---
|
|
208
|
+
|
|
209
|
+
// Reverse returns the value of x with its bits in reversed order.
|
|
210
|
+
func Reverse(x uint) uint { return uint(Reverse64(uint64(x))) }
|
|
211
|
+
|
|
212
|
+
// Reverse8 returns the value of x with its bits in reversed order.
|
|
213
|
+
func Reverse8(x uint8) uint8 { return uint8(Reverse32(uint32(x)) >> 24) }
|
|
214
|
+
|
|
215
|
+
// Reverse16 returns the value of x with its bits in reversed order.
|
|
216
|
+
func Reverse16(x uint16) uint16 { return uint16(Reverse32(uint32(x)) >> 16) }
|
|
217
|
+
|
|
218
|
+
// Reverse32 returns the value of x with its bits in reversed order.
|
|
219
|
+
func Reverse32(x uint32) uint32 {
|
|
220
|
+
const m = 1<<32 - 1
|
|
221
|
+
x = x>>1&(m0&m) | x&(m0&m)<<1
|
|
222
|
+
x = x>>2&(m1&m) | x&(m1&m)<<2
|
|
223
|
+
x = x>>4&(m2&m) | x&(m2&m)<<4
|
|
224
|
+
return ReverseBytes32(x)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Reverse64 returns the value of x with its bits in reversed order.
|
|
228
|
+
func Reverse64(x uint64) uint64 {
|
|
229
|
+
const m = 1<<64 - 1
|
|
230
|
+
x = x>>1&(m0&m) | x&(m0&m)<<1
|
|
231
|
+
x = x>>2&(m1&m) | x&(m1&m)<<2
|
|
232
|
+
x = x>>4&(m2&m) | x&(m2&m)<<4
|
|
233
|
+
return ReverseBytes64(x)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// --- ReverseBytes ---
|
|
237
|
+
|
|
238
|
+
const m3 = 0x00ff00ff00ff00ff
|
|
239
|
+
const m4 = 0x0000ffff0000ffff
|
|
240
|
+
|
|
241
|
+
// ReverseBytes returns the value of x with its bytes in reversed order.
|
|
242
|
+
func ReverseBytes(x uint) uint { return uint(ReverseBytes64(uint64(x))) }
|
|
243
|
+
|
|
244
|
+
// ReverseBytes16 returns the value of x with its bytes in reversed order.
|
|
245
|
+
func ReverseBytes16(x uint16) uint16 { return x>>8 | x<<8 }
|
|
246
|
+
|
|
247
|
+
// ReverseBytes32 returns the value of x with its bytes in reversed order.
|
|
248
|
+
func ReverseBytes32(x uint32) uint32 {
|
|
249
|
+
const m = 1<<32 - 1
|
|
250
|
+
x = x>>8&(m3&m) | x&(m3&m)<<8
|
|
251
|
+
return x>>16 | x<<16
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ReverseBytes64 returns the value of x with its bytes in reversed order.
|
|
255
|
+
func ReverseBytes64(x uint64) uint64 {
|
|
256
|
+
const m = 1<<64 - 1
|
|
257
|
+
x = x>>8&(m3&m) | x&(m3&m)<<8
|
|
258
|
+
x = x>>16&(m4&m) | x&(m4&m)<<16
|
|
259
|
+
return x>>32 | x<<32
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// --- Len ---
|
|
263
|
+
|
|
264
|
+
// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
|
265
|
+
//
|
|
266
|
+
//gofront:native bits.Len
|
|
267
|
+
func Len(x uint) int { return Len64(uint64(x)) }
|
|
268
|
+
|
|
269
|
+
// Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
|
270
|
+
//
|
|
271
|
+
//gofront:native bits.Len8
|
|
272
|
+
func Len8(x uint8) int { return len8(uint32(x)) }
|
|
273
|
+
|
|
274
|
+
// Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
|
275
|
+
//
|
|
276
|
+
//gofront:native bits.Len16
|
|
277
|
+
func Len16(x uint16) (n int) {
|
|
278
|
+
if x >= 1<<8 {
|
|
279
|
+
x >>= 8
|
|
280
|
+
n = 8
|
|
281
|
+
}
|
|
282
|
+
return n + len8(uint32(x))
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
|
286
|
+
//
|
|
287
|
+
//gofront:native bits.Len32
|
|
288
|
+
func Len32(x uint32) (n int) {
|
|
289
|
+
if x >= 1<<16 {
|
|
290
|
+
x >>= 16
|
|
291
|
+
n = 16
|
|
292
|
+
}
|
|
293
|
+
if x >= 1<<8 {
|
|
294
|
+
x >>= 8
|
|
295
|
+
n += 8
|
|
296
|
+
}
|
|
297
|
+
return n + len8(x)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
|
|
301
|
+
//
|
|
302
|
+
//gofront:native bits.Len64
|
|
303
|
+
func Len64(x uint64) (n int) {
|
|
304
|
+
if x >= 1<<32 {
|
|
305
|
+
x >>= 32
|
|
306
|
+
n = 32
|
|
307
|
+
}
|
|
308
|
+
if x >= 1<<16 {
|
|
309
|
+
x >>= 16
|
|
310
|
+
n += 16
|
|
311
|
+
}
|
|
312
|
+
if x >= 1<<8 {
|
|
313
|
+
x >>= 8
|
|
314
|
+
n += 8
|
|
315
|
+
}
|
|
316
|
+
return n + len8(uint32(x))
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// --- Add with carry ---
|
|
320
|
+
|
|
321
|
+
// Add returns the sum with carry of x, y and carry: sum = x + y + carry.
|
|
322
|
+
// The carry input must be 0 or 1; otherwise the behavior is undefined.
|
|
323
|
+
// The carryOut output is guaranteed to be 0 or 1.
|
|
324
|
+
func Add(x, y, carry uint) (sum, carryOut uint) {
|
|
325
|
+
s64, c64 := Add64(uint64(x), uint64(y), uint64(carry))
|
|
326
|
+
return uint(s64), uint(c64)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Add32 returns the sum with carry of x, y and carry: sum = x + y + carry.
|
|
330
|
+
func Add32(x, y, carry uint32) (sum, carryOut uint32) {
|
|
331
|
+
sum64 := uint64(x) + uint64(y) + uint64(carry)
|
|
332
|
+
sum = uint32(sum64)
|
|
333
|
+
carryOut = uint32(sum64 >> 32)
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.
|
|
338
|
+
func Add64(x, y, carry uint64) (sum, carryOut uint64) {
|
|
339
|
+
sum = x + y + carry
|
|
340
|
+
carryOut = ((x & y) | ((x | y) &^ sum)) >> 63
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// --- Subtract with borrow ---
|
|
345
|
+
|
|
346
|
+
// Sub returns the difference of x, y and borrow: diff = x - y - borrow.
|
|
347
|
+
// The borrow input must be 0 or 1; otherwise the behavior is undefined.
|
|
348
|
+
// The borrowOut output is guaranteed to be 0 or 1.
|
|
349
|
+
func Sub(x, y, borrow uint) (diff, borrowOut uint) {
|
|
350
|
+
d64, b64 := Sub64(uint64(x), uint64(y), uint64(borrow))
|
|
351
|
+
return uint(d64), uint(b64)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Sub32 returns the difference of x, y and borrow, diff = x - y - borrow.
|
|
355
|
+
func Sub32(x, y, borrow uint32) (diff, borrowOut uint32) {
|
|
356
|
+
diff = x - y - borrow
|
|
357
|
+
borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 31
|
|
358
|
+
return
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Sub64 returns the difference of x, y and borrow: diff = x - y - borrow.
|
|
362
|
+
func Sub64(x, y, borrow uint64) (diff, borrowOut uint64) {
|
|
363
|
+
diff = x - y - borrow
|
|
364
|
+
borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 63
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// --- Full-width multiply ---
|
|
369
|
+
|
|
370
|
+
// Mul returns the full-width product of x and y: (hi, lo) = x * y
|
|
371
|
+
// with the product bits' upper half returned in hi and the lower
|
|
372
|
+
// half returned in lo.
|
|
373
|
+
func Mul(x, y uint) (hi, lo uint) {
|
|
374
|
+
h, l := Mul64(uint64(x), uint64(y))
|
|
375
|
+
return uint(h), uint(l)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y
|
|
379
|
+
// with the product bits' upper half returned in hi and the lower
|
|
380
|
+
// half returned in lo.
|
|
381
|
+
func Mul32(x, y uint32) (hi, lo uint32) {
|
|
382
|
+
tmp := uint64(x) * uint64(y)
|
|
383
|
+
hi, lo = uint32(tmp>>32), uint32(tmp)
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y
|
|
388
|
+
// with the product bits' upper half returned in hi and the lower
|
|
389
|
+
// half returned in lo.
|
|
390
|
+
func Mul64(x, y uint64) (hi, lo uint64) {
|
|
391
|
+
// Literals, not a local constant: JS output of a local untyped constant
|
|
392
|
+
// used as a uint64 operand names a BigInt variable it never declares.
|
|
393
|
+
x0 := x & 0xffffffff
|
|
394
|
+
x1 := x >> 32
|
|
395
|
+
y0 := y & 0xffffffff
|
|
396
|
+
y1 := y >> 32
|
|
397
|
+
w0 := x0 * y0
|
|
398
|
+
t := x1*y0 + w0>>32
|
|
399
|
+
w1 := t & 0xffffffff
|
|
400
|
+
w2 := t >> 32
|
|
401
|
+
w1 += x0 * y1
|
|
402
|
+
hi = x1*y1 + w2 + w1>>32
|
|
403
|
+
lo = x * y
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// --- Full-width divide ---
|
|
408
|
+
|
|
409
|
+
// Div returns the quotient and remainder of (hi, lo) divided by y:
|
|
410
|
+
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
|
|
411
|
+
// half in parameter hi and the lower half in parameter lo.
|
|
412
|
+
// Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
|
|
413
|
+
func Div(hi, lo, y uint) (quo, rem uint) {
|
|
414
|
+
q, r := Div64(uint64(hi), uint64(lo), uint64(y))
|
|
415
|
+
return uint(q), uint(r)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Div32 returns the quotient and remainder of (hi, lo) divided by y.
|
|
419
|
+
// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
|
|
420
|
+
func Div32(hi, lo, y uint32) (quo, rem uint32) {
|
|
421
|
+
if y != 0 && y <= hi {
|
|
422
|
+
panic(overflowError)
|
|
423
|
+
}
|
|
424
|
+
z := uint64(hi)<<32 | uint64(lo)
|
|
425
|
+
quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
|
|
426
|
+
return
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Div64 returns the quotient and remainder of (hi, lo) divided by y.
|
|
430
|
+
// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
|
|
431
|
+
func Div64(hi, lo, y uint64) (quo, rem uint64) {
|
|
432
|
+
if y == 0 {
|
|
433
|
+
panic(divideError)
|
|
434
|
+
}
|
|
435
|
+
if y <= hi {
|
|
436
|
+
panic(overflowError)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// If high part is zero, we can directly return the results.
|
|
440
|
+
if hi == 0 {
|
|
441
|
+
return lo / y, lo % y
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
s := uint(LeadingZeros64(y))
|
|
445
|
+
y <<= s
|
|
446
|
+
|
|
447
|
+
// two32 is 1<<32 (see Mul64 on why it is a variable).
|
|
448
|
+
var two32 uint64 = 1 << 32
|
|
449
|
+
yn1 := y >> 32
|
|
450
|
+
yn0 := y & 0xffffffff
|
|
451
|
+
un32 := hi<<s | lo>>(64-s)
|
|
452
|
+
un10 := lo << s
|
|
453
|
+
un1 := un10 >> 32
|
|
454
|
+
un0 := un10 & 0xffffffff
|
|
455
|
+
q1 := un32 / yn1
|
|
456
|
+
rhat := un32 - q1*yn1
|
|
457
|
+
|
|
458
|
+
for q1 >= two32 || q1*yn0 > two32*rhat+un1 {
|
|
459
|
+
q1--
|
|
460
|
+
rhat += yn1
|
|
461
|
+
if rhat >= two32 {
|
|
462
|
+
break
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
un21 := un32*two32 + un1 - q1*y
|
|
467
|
+
q0 := un21 / yn1
|
|
468
|
+
rhat = un21 - q0*yn1
|
|
469
|
+
|
|
470
|
+
for q0 >= two32 || q0*yn0 > two32*rhat+un0 {
|
|
471
|
+
q0--
|
|
472
|
+
rhat += yn1
|
|
473
|
+
if rhat >= two32 {
|
|
474
|
+
break
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return q1*two32 + q0, (un21*two32 + un0 - q0*y) >> s
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Rem returns the remainder of (hi, lo) divided by y. Rem panics for
|
|
482
|
+
// y == 0 (division by zero) but, unlike Div, it doesn't panic on a
|
|
483
|
+
// quotient overflow.
|
|
484
|
+
func Rem(hi, lo, y uint) uint {
|
|
485
|
+
return uint(Rem64(uint64(hi), uint64(lo), uint64(y)))
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics
|
|
489
|
+
// for y == 0 (division by zero) but, unlike Div32, it doesn't panic
|
|
490
|
+
// on a quotient overflow.
|
|
491
|
+
func Rem32(hi, lo, y uint32) uint32 {
|
|
492
|
+
return uint32((uint64(hi)<<32 | uint64(lo)) % uint64(y))
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics
|
|
496
|
+
// for y == 0 (division by zero) but, unlike Div64, it doesn't panic
|
|
497
|
+
// on a quotient overflow.
|
|
498
|
+
func Rem64(hi, lo, y uint64) uint64 {
|
|
499
|
+
// We scale down hi so that hi < y, then use Div64 to compute the
|
|
500
|
+
// rem with the guarantee that it won't panic on quotient overflow.
|
|
501
|
+
// Given that
|
|
502
|
+
// hi ≡ hi%y (mod y)
|
|
503
|
+
// we have
|
|
504
|
+
// hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y)
|
|
505
|
+
_, rem := Div64(hi%y, lo, y)
|
|
506
|
+
return rem
|
|
507
|
+
}
|
package/stdlib/math/extra.go
CHANGED
|
@@ -68,23 +68,14 @@ func Ldexp(frac float64, exp int) float64 {
|
|
|
68
68
|
return m * Float64frombits(x)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
func Modf(f float64) (int float64, frac float64) {
|
|
72
|
-
if IsInf(f, 0) {
|
|
73
|
-
return f, NaN()
|
|
74
|
-
}
|
|
75
|
-
if IsNaN(f) {
|
|
76
|
-
return NaN(), NaN()
|
|
77
|
-
}
|
|
78
|
-
int = Trunc(f)
|
|
79
|
-
frac = Copysign(f-int, f)
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
|
|
83
71
|
func RoundToEven(x float64) float64 {
|
|
84
72
|
b := Float64bits(x)
|
|
85
73
|
e := uint(b>>shift) & mask
|
|
86
74
|
if e >= bias {
|
|
87
75
|
e -= bias
|
|
76
|
+
if e >= shift {
|
|
77
|
+
return x
|
|
78
|
+
}
|
|
88
79
|
b += (halfMinusULP + (b>>(shift-e))&1) >> e
|
|
89
80
|
b = b &^ (fracMask >> e)
|
|
90
81
|
} else if e == bias-1 && b&fracMask != 0 {
|
|
@@ -143,14 +134,25 @@ func fmaNonzero(x uint64) uint64 {
|
|
|
143
134
|
return 0
|
|
144
135
|
}
|
|
145
136
|
|
|
137
|
+
// fmaShl and fmaShr shift the 128-bit value (u1, u2) by n < 128. Each branch
|
|
138
|
+
// keeps every shift count non-negative: a uint subtraction that would wrap
|
|
139
|
+
// does not on the JavaScript target.
|
|
146
140
|
func fmaShl(u1, u2 uint64, n uint) (r1 uint64, r2 uint64) {
|
|
147
|
-
|
|
141
|
+
if n < 64 {
|
|
142
|
+
r1 = u1<<n | u2>>(64-n)
|
|
143
|
+
} else {
|
|
144
|
+
r1 = u2 << (n - 64)
|
|
145
|
+
}
|
|
148
146
|
r2 = u2 << n
|
|
149
147
|
return
|
|
150
148
|
}
|
|
151
149
|
|
|
152
150
|
func fmaShr(u1, u2 uint64, n uint) (r1 uint64, r2 uint64) {
|
|
153
|
-
|
|
151
|
+
if n < 64 {
|
|
152
|
+
r2 = u2>>n | u1<<(64-n)
|
|
153
|
+
} else {
|
|
154
|
+
r2 = u1 >> (n - 64)
|
|
155
|
+
}
|
|
154
156
|
r1 = u1 >> n
|
|
155
157
|
return
|
|
156
158
|
}
|