@grain/stdlib 0.5.13 → 0.6.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.
Files changed (155) hide show
  1. package/CHANGELOG.md +201 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +62 -40
  21. package/hash.md +27 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2071 -0
  31. package/json.md +646 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +193 -174
  92. package/runtime/numberUtils.md +29 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +543 -245
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/int64.gr CHANGED
@@ -1,47 +1,42 @@
1
1
  /**
2
- * @module Int64: Utilities for working with the Int64 type.
3
- * @example import Int64 from "int64"
2
+ * Utilities for working with the Int64 type.
3
+ *
4
+ * @example from "int64" include Int64
4
5
  *
5
6
  * @since v0.2.0
6
7
  */
7
- import WasmI32 from "runtime/unsafe/wasmi32"
8
- import WasmI64 from "runtime/unsafe/wasmi64"
9
- import Exception from "runtime/exception"
8
+ module Int64
9
+
10
+ from "runtime/unsafe/wasmi32" include WasmI32
11
+ from "runtime/unsafe/wasmi64" include WasmI64
12
+ use WasmI64.{ (==), (!=), (&), (|), (^), (<<), (>>), (<), (<=), (>), (>=) }
13
+ from "runtime/exception" include Exception
10
14
 
11
- import { newInt64 } from "runtime/dataStructures"
15
+ from "runtime/dataStructures" include DataStructures
16
+ use DataStructures.{ newInt64 }
12
17
 
13
- import {
18
+ from "runtime/numbers" include Numbers
19
+ use Numbers.{
14
20
  coerceNumberToInt64 as fromNumber,
15
21
  coerceInt64ToNumber as toNumber,
16
- } from "runtime/numbers"
22
+ }
17
23
 
18
- /**
19
- * @section Conversions: Functions for converting between Numbers and the Int64 type.
20
- */
24
+ provide { fromNumber, toNumber }
21
25
 
22
26
  /**
23
- * Converts a Number to an Int64.
27
+ * Converts a Uint64 to an Int64.
24
28
  *
25
29
  * @param number: The value to convert
26
- * @returns The Number represented as an Int64
27
- *
28
- * @since v0.2.0
29
- */
30
- export fromNumber
31
-
32
- /**
33
- * Converts an Int64 to a Number.
34
- *
35
- * @param value: The value to convert
36
- * @returns The Int64 represented as a Number
30
+ * @returns The Uint64 represented as an Int64
37
31
  *
38
- * @since v0.2.0
39
- */
40
- export toNumber
41
-
42
- /**
43
- * @section Operations: Mathematical operations for Int64 values.
32
+ * @since v0.6.0
44
33
  */
34
+ @unsafe
35
+ provide let fromUint64 = (number: Uint64) => {
36
+ let x = WasmI64.load(WasmI32.fromGrain(number), 8n)
37
+ let result = newInt64(x)
38
+ WasmI32.toGrain(result): Int64
39
+ }
45
40
 
46
41
  /**
47
42
  * Increments the value by one.
@@ -52,9 +47,10 @@ export toNumber
52
47
  * @since v0.2.0
53
48
  */
54
49
  @unsafe
55
- export let incr = (value: Int64) => {
50
+ provide let incr = (value: Int64) => {
51
+ use WasmI64.{ (+) }
56
52
  let value = WasmI32.fromGrain(value)
57
- let ptr = newInt64(WasmI64.add(WasmI64.load(value, 8n), 1N))
53
+ let ptr = newInt64(WasmI64.load(value, 8n) + 1N)
58
54
  WasmI32.toGrain(ptr): Int64
59
55
  }
60
56
 
@@ -67,9 +63,10 @@ export let incr = (value: Int64) => {
67
63
  * @since v0.2.0
68
64
  */
69
65
  @unsafe
70
- export let decr = (value: Int64) => {
66
+ provide let decr = (value: Int64) => {
67
+ use WasmI64.{ (-) }
71
68
  let value = WasmI32.fromGrain(value)
72
- let ptr = newInt64(WasmI64.sub(WasmI64.load(value, 8n), 1N))
69
+ let ptr = newInt64(WasmI64.load(value, 8n) - 1N)
73
70
  WasmI32.toGrain(ptr): Int64
74
71
  }
75
72
 
@@ -80,13 +77,15 @@ export let decr = (value: Int64) => {
80
77
  * @param y: The second operand
81
78
  * @returns The sum of the two operands
82
79
  *
83
- * @since v0.2.0
80
+ * @since v0.6.0
81
+ * @history v0.2.0: Originally named `add`
84
82
  */
85
83
  @unsafe
86
- export let add = (x: Int64, y: Int64) => {
84
+ provide let (+) = (x: Int64, y: Int64) => {
85
+ use WasmI64.{ (+) }
87
86
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
88
87
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
89
- let ptr = newInt64(WasmI64.add(xv, yv))
88
+ let ptr = newInt64(xv + yv)
90
89
  WasmI32.toGrain(ptr): Int64
91
90
  }
92
91
 
@@ -97,13 +96,15 @@ export let add = (x: Int64, y: Int64) => {
97
96
  * @param y: The second operand
98
97
  * @returns The difference of the two operands
99
98
  *
100
- * @since v0.2.0
99
+ * @since v0.6.0
100
+ * @history v0.2.0: Originally named `sub`
101
101
  */
102
102
  @unsafe
103
- export let sub = (x: Int64, y: Int64) => {
103
+ provide let (-) = (x: Int64, y: Int64) => {
104
+ use WasmI64.{ (-) }
104
105
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
105
106
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
106
- let ptr = newInt64(WasmI64.sub(xv, yv))
107
+ let ptr = newInt64(xv - yv)
107
108
  WasmI32.toGrain(ptr): Int64
108
109
  }
109
110
 
@@ -114,13 +115,15 @@ export let sub = (x: Int64, y: Int64) => {
114
115
  * @param y: The second operand
115
116
  * @returns The product of the two operands
116
117
  *
117
- * @since v0.2.0
118
+ * @since v0.6.0
119
+ * @history v0.2.0: Originally named `*`
118
120
  */
119
121
  @unsafe
120
- export let mul = (x: Int64, y: Int64) => {
122
+ provide let (*) = (x: Int64, y: Int64) => {
123
+ use WasmI64.{ (*) }
121
124
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
122
125
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
123
- let ptr = newInt64(WasmI64.mul(xv, yv))
126
+ let ptr = newInt64(xv * yv)
124
127
  WasmI32.toGrain(ptr): Int64
125
128
  }
126
129
 
@@ -131,30 +134,15 @@ export let mul = (x: Int64, y: Int64) => {
131
134
  * @param y: The second operand
132
135
  * @returns The quotient of its operands
133
136
  *
134
- * @since v0.2.0
135
- */
136
- @unsafe
137
- export let div = (x: Int64, y: Int64) => {
138
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
139
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
140
- let ptr = newInt64(WasmI64.divS(xv, yv))
141
- WasmI32.toGrain(ptr): Int64
142
- }
143
-
144
- /**
145
- * Computes the quotient of its operands using unsigned division.
146
- *
147
- * @param x: The first operand
148
- * @param y: The second operand
149
- * @returns The quotient of its operands
150
- *
151
- * @since v0.2.0
137
+ * @since v0.6.0
138
+ * @history v0.2.0: Originally named `div`
152
139
  */
153
140
  @unsafe
154
- export let divU = (x: Int64, y: Int64) => {
141
+ provide let (/) = (x: Int64, y: Int64) => {
142
+ use WasmI64.{ (/) }
155
143
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
156
144
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
157
- let ptr = newInt64(WasmI64.divU(xv, yv))
145
+ let ptr = newInt64(xv / yv)
158
146
  WasmI32.toGrain(ptr): Int64
159
147
  }
160
148
 
@@ -168,34 +156,18 @@ export let divU = (x: Int64, y: Int64) => {
168
156
  * @since v0.2.0
169
157
  */
170
158
  @unsafe
171
- export let rem = (x: Int64, y: Int64) => {
159
+ provide let rem = (x: Int64, y: Int64) => {
172
160
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
173
161
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
174
162
  let ptr = newInt64(WasmI64.remS(xv, yv))
175
163
  WasmI32.toGrain(ptr): Int64
176
164
  }
177
165
 
178
- /**
179
- * Computes the remainder of the division of its operands using unsigned division.
180
- *
181
- * @param x: The first operand
182
- * @param y: The second operand
183
- * @returns The remainder of its operands
184
- *
185
- * @since v0.2.0
186
- */
187
- @unsafe
188
- export let remU = (x: Int64, y: Int64) => {
189
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
190
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
191
- let ptr = newInt64(WasmI64.remU(xv, yv))
192
- WasmI32.toGrain(ptr): Int64
193
- }
194
-
195
166
  @unsafe
196
167
  let abs = n => {
197
- let mask = WasmI64.shrS(n, 63N)
198
- WasmI64.sub(WasmI64.xor(n, mask), mask)
168
+ use WasmI64.{ (-) }
169
+ let mask = n >> 63N
170
+ (n ^ mask) - mask
199
171
  }
200
172
 
201
173
  /**
@@ -208,10 +180,12 @@ let abs = n => {
208
180
  *
209
181
  * @throws ModuloByZero: When `y` is zero
210
182
  *
211
- * @since v0.2.0
183
+ * @since v0.6.0
184
+ * @history v0.2.0: Originally named `mod`
212
185
  */
213
186
  @unsafe
214
- export let mod = (x: Int64, y: Int64) => {
187
+ provide let (%) = (x: Int64, y: Int64) => {
188
+ use WasmI64.{ (-) }
215
189
  let xval = WasmI64.load(WasmI32.fromGrain(x), 8n)
216
190
  let yval = WasmI64.load(WasmI32.fromGrain(y), 8n)
217
191
 
@@ -219,26 +193,18 @@ export let mod = (x: Int64, y: Int64) => {
219
193
  throw Exception.ModuloByZero
220
194
  }
221
195
 
222
- let ptr = if (WasmI64.ltS(WasmI64.xor(xval, yval), 0N)) {
196
+ let ptr = if ((xval ^ yval) < 0N) {
223
197
  let xabs = abs(xval)
224
198
  let yabs = abs(yval)
225
199
  let mval = WasmI64.remS(xabs, yabs)
226
- let mres = WasmI64.sub(yabs, mval)
227
- newInt64(
228
- if (WasmI64.ne(mval, 0N)) (
229
- if (WasmI64.ltS(yval, 0N)) WasmI64.sub(0N, mres) else mres
230
- ) else 0N
231
- )
200
+ let mres = yabs - mval
201
+ newInt64(if (mval != 0N) (if (yval < 0N) 0N - mres else mres) else 0N)
232
202
  } else {
233
203
  newInt64(WasmI64.remS(xval, yval))
234
204
  }
235
205
  WasmI32.toGrain(ptr): Int64
236
206
  }
237
207
 
238
- /**
239
- * @section Bitwise operations: Functions for operating on bits of Int64 values.
240
- */
241
-
242
208
  /**
243
209
  * Rotates the bits of the value left by the given number of bits.
244
210
  *
@@ -249,7 +215,7 @@ export let mod = (x: Int64, y: Int64) => {
249
215
  * @since v0.4.0
250
216
  */
251
217
  @unsafe
252
- export let rotl = (value: Int64, amount: Int64) => {
218
+ provide let rotl = (value: Int64, amount: Int64) => {
253
219
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
254
220
  let yv = WasmI64.load(WasmI32.fromGrain(amount), 8n)
255
221
  let ptr = newInt64(WasmI64.rotl(xv, yv))
@@ -266,7 +232,7 @@ export let rotl = (value: Int64, amount: Int64) => {
266
232
  * @since v0.4.0
267
233
  */
268
234
  @unsafe
269
- export let rotr = (value: Int64, amount: Int64) => {
235
+ provide let rotr = (value: Int64, amount: Int64) => {
270
236
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
271
237
  let yv = WasmI64.load(WasmI32.fromGrain(amount), 8n)
272
238
  let ptr = newInt64(WasmI64.rotr(xv, yv))
@@ -280,13 +246,14 @@ export let rotr = (value: Int64, amount: Int64) => {
280
246
  * @param amount: The number of bits to shift by
281
247
  * @returns The shifted value
282
248
  *
283
- * @since v0.2.0
249
+ * @since v0.6.0
250
+ * @history v0.2.0: Originally named `shl`
284
251
  */
285
252
  @unsafe
286
- export let shl = (value: Int64, amount: Int64) => {
253
+ provide let (<<) = (value: Int64, amount: Int64) => {
287
254
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
288
255
  let yv = WasmI64.load(WasmI32.fromGrain(amount), 8n)
289
- let ptr = newInt64(WasmI64.shl(xv, yv))
256
+ let ptr = newInt64(xv << yv)
290
257
  WasmI32.toGrain(ptr): Int64
291
258
  }
292
259
 
@@ -297,37 +264,17 @@ export let shl = (value: Int64, amount: Int64) => {
297
264
  * @param amount: The amount to shift by
298
265
  * @returns The shifted value
299
266
  *
300
- * @since v0.2.0
267
+ * @since v0.6.0
268
+ * @history v0.2.0: Originally named `shr`
301
269
  */
302
270
  @unsafe
303
- export let shr = (value: Int64, amount: Int64) => {
271
+ provide let (>>) = (value: Int64, amount: Int64) => {
304
272
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
305
273
  let yv = WasmI64.load(WasmI32.fromGrain(amount), 8n)
306
- let ptr = newInt64(WasmI64.shrS(xv, yv))
274
+ let ptr = newInt64(xv >> yv)
307
275
  WasmI32.toGrain(ptr): Int64
308
276
  }
309
277
 
310
- /**
311
- * Shifts the bits of the value right by the given number of bits.
312
- *
313
- * @param value: The value to shift
314
- * @param amount: The amount to shift by
315
- * @returns The shifted value
316
- *
317
- * @since v0.2.0
318
- */
319
- @unsafe
320
- export let shrU = (value: Int64, amount: Int64) => {
321
- let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
322
- let yv = WasmI64.load(WasmI32.fromGrain(amount), 8n)
323
- let ptr = newInt64(WasmI64.shrU(xv, yv))
324
- WasmI32.toGrain(ptr): Int64
325
- }
326
-
327
- /**
328
- * @section Comparisons: Functions for comparing Int64 values.
329
- */
330
-
331
278
  /**
332
279
  * Checks if the first value is equal to the second value.
333
280
  *
@@ -335,13 +282,14 @@ export let shrU = (value: Int64, amount: Int64) => {
335
282
  * @param y: The second value
336
283
  * @returns `true` if the first value is equal to the second value or `false` otherwise
337
284
  *
338
- * @since v0.4.0
285
+ * @since v0.6.0
286
+ * @history v0.4.0: Originally named `eq`
339
287
  */
340
288
  @unsafe
341
- export let eq = (x: Int64, y: Int64) => {
289
+ provide let (==) = (x: Int64, y: Int64) => {
342
290
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
343
291
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
344
- WasmI64.eq(xv, yv)
292
+ xv == yv
345
293
  }
346
294
 
347
295
  /**
@@ -351,13 +299,14 @@ export let eq = (x: Int64, y: Int64) => {
351
299
  * @param y: The second value
352
300
  * @returns `true` if the first value is not equal to the second value or `false` otherwise
353
301
  *
354
- * @since v0.4.0
302
+ * @since v0.6.0
303
+ * @history v0.4.0: Originally named `ne`
355
304
  */
356
305
  @unsafe
357
- export let ne = (x: Int64, y: Int64) => {
306
+ provide let (!=) = (x: Int64, y: Int64) => {
358
307
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
359
308
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
360
- WasmI64.ne(xv, yv)
309
+ xv != yv
361
310
  }
362
311
 
363
312
  /**
@@ -369,7 +318,7 @@ export let ne = (x: Int64, y: Int64) => {
369
318
  * @since v0.4.0
370
319
  */
371
320
  @unsafe
372
- export let eqz = (value: Int64) => {
321
+ provide let eqz = (value: Int64) => {
373
322
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
374
323
  WasmI64.eqz(xv)
375
324
  }
@@ -381,29 +330,14 @@ export let eqz = (value: Int64) => {
381
330
  * @param y: The second value
382
331
  * @returns `true` if the first value is less than the second value or `false` otherwise
383
332
  *
384
- * @since v0.2.0
385
- */
386
- @unsafe
387
- export let lt = (x: Int64, y: Int64) => {
388
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
389
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
390
- WasmI64.ltS(xv, yv)
391
- }
392
-
393
- /**
394
- * Checks if the first unsigned value is less than the second unsigned value.
395
- *
396
- * @param x: The first value
397
- * @param y: The second value
398
- * @returns `true` if the first value is less than the second value or `false` otherwise
399
- *
400
- * @since v0.5.0
333
+ * @since v0.6.0
334
+ * @history v0.2.0: Originally named `lt`
401
335
  */
402
336
  @unsafe
403
- export let ltU = (x: Int64, y: Int64) => {
337
+ provide let (<) = (x: Int64, y: Int64) => {
404
338
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
405
339
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
406
- WasmI64.ltU(xv, yv)
340
+ xv < yv
407
341
  }
408
342
 
409
343
  /**
@@ -413,29 +347,14 @@ export let ltU = (x: Int64, y: Int64) => {
413
347
  * @param y: The second value
414
348
  * @returns `true` if the first value is greater than the second value or `false` otherwise
415
349
  *
416
- * @since v0.2.0
417
- */
418
- @unsafe
419
- export let gt = (x: Int64, y: Int64) => {
420
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
421
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
422
- WasmI64.gtS(xv, yv)
423
- }
424
-
425
- /**
426
- * Checks if the first unsigned value is greater than the second unsigned value.
427
- *
428
- * @param x: The first value
429
- * @param y: The second value
430
- * @returns `true` if the first value is greater than the second value or `false` otherwise
431
- *
432
- * @since v0.5.0
350
+ * @since v0.6.0
351
+ * @history v0.2.0: Originally named `gt`
433
352
  */
434
353
  @unsafe
435
- export let gtU = (x: Int64, y: Int64) => {
354
+ provide let (>) = (x: Int64, y: Int64) => {
436
355
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
437
356
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
438
- WasmI64.gtU(xv, yv)
357
+ xv > yv
439
358
  }
440
359
 
441
360
  /**
@@ -445,29 +364,14 @@ export let gtU = (x: Int64, y: Int64) => {
445
364
  * @param y: The second value
446
365
  * @returns `true` if the first value is less than or equal to the second value or `false` otherwise
447
366
  *
448
- * @since v0.2.0
367
+ * @since v0.6.0
368
+ * @history v0.2.0: Originally named `lte`
449
369
  */
450
370
  @unsafe
451
- export let lte = (x: Int64, y: Int64) => {
371
+ provide let (<=) = (x: Int64, y: Int64) => {
452
372
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
453
373
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
454
- WasmI64.leS(xv, yv)
455
- }
456
-
457
- /**
458
- * Checks if the first unsigned value is less than or equal to the second unsigned value.
459
- *
460
- * @param x: The first value
461
- * @param y: The second value
462
- * @returns `true` if the first value is less than or equal to the second value or `false` otherwise
463
- *
464
- * @since v0.5.0
465
- */
466
- @unsafe
467
- export let lteU = (x: Int64, y: Int64) => {
468
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
469
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
470
- WasmI64.leU(xv, yv)
374
+ xv <= yv
471
375
  }
472
376
 
473
377
  /**
@@ -477,35 +381,16 @@ export let lteU = (x: Int64, y: Int64) => {
477
381
  * @param y: The second value
478
382
  * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
479
383
  *
480
- * @since v0.2.0
384
+ * @since v0.6.0
385
+ * @history v0.2.0: Originally named `gte`
481
386
  */
482
387
  @unsafe
483
- export let gte = (x: Int64, y: Int64) => {
388
+ provide let (>=) = (x: Int64, y: Int64) => {
484
389
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
485
390
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
486
- WasmI64.geS(xv, yv)
391
+ xv >= yv
487
392
  }
488
393
 
489
- /**
490
- * Checks if the first unsigned value is greater than or equal to the second unsigned value.
491
- *
492
- * @param x: The first value
493
- * @param y: The second value
494
- * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
495
- *
496
- * @since v0.5.0
497
- */
498
- @unsafe
499
- export let gteU = (x: Int64, y: Int64) => {
500
- let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
501
- let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
502
- WasmI64.geU(xv, yv)
503
- }
504
-
505
- /**
506
- * @section Bitwise logic: Boolean operations on the bits of Int64 values.
507
- */
508
-
509
394
  /**
510
395
  * Computes the bitwise NOT of the given value.
511
396
  *
@@ -515,9 +400,9 @@ export let gteU = (x: Int64, y: Int64) => {
515
400
  * @since v0.2.0
516
401
  */
517
402
  @unsafe
518
- export let lnot = (value: Int64) => {
403
+ provide let lnot = (value: Int64) => {
519
404
  let xv = WasmI64.load(WasmI32.fromGrain(value), 8n)
520
- let ptr = newInt64(WasmI64.xor(xv, 0xffffffffffffffffN))
405
+ let ptr = newInt64(xv ^ 0xffffffffffffffffN)
521
406
  WasmI32.toGrain(ptr): Int64
522
407
  }
523
408
 
@@ -528,13 +413,14 @@ export let lnot = (value: Int64) => {
528
413
  * @param y: The second operand
529
414
  * @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
530
415
  *
531
- * @since v0.2.0
416
+ * @since v0.6.0
417
+ * @history v0.2.0: Originally named `land`
532
418
  */
533
419
  @unsafe
534
- export let land = (x: Int64, y: Int64) => {
420
+ provide let (&) = (x: Int64, y: Int64) => {
535
421
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
536
422
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
537
- let ptr = newInt64(WasmI64.and(xv, yv))
423
+ let ptr = newInt64(xv & yv)
538
424
  WasmI32.toGrain(ptr): Int64
539
425
  }
540
426
 
@@ -545,13 +431,14 @@ export let land = (x: Int64, y: Int64) => {
545
431
  * @param y: The second operand
546
432
  * @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
547
433
  *
548
- * @since v0.2.0
434
+ * @since v0.6.0
435
+ * @history v0.2.0: Originally named `lor`
549
436
  */
550
437
  @unsafe
551
- export let lor = (x: Int64, y: Int64) => {
438
+ provide let (|) = (x: Int64, y: Int64) => {
552
439
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
553
440
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
554
- let ptr = newInt64(WasmI64.or(xv, yv))
441
+ let ptr = newInt64(xv | yv)
555
442
  WasmI32.toGrain(ptr): Int64
556
443
  }
557
444
 
@@ -562,13 +449,14 @@ export let lor = (x: Int64, y: Int64) => {
562
449
  * @param y: The second operand
563
450
  * @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
564
451
  *
565
- * @since v0.2.0
452
+ * @since v0.6.0
453
+ * @history v0.2.0: Originally named `lxor`
566
454
  */
567
455
  @unsafe
568
- export let lxor = (x: Int64, y: Int64) => {
456
+ provide let (^) = (x: Int64, y: Int64) => {
569
457
  let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
570
458
  let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
571
- let ptr = newInt64(WasmI64.xor(xv, yv))
459
+ let ptr = newInt64(xv ^ yv)
572
460
  WasmI32.toGrain(ptr): Int64
573
461
  }
574
462
 
@@ -581,7 +469,7 @@ export let lxor = (x: Int64, y: Int64) => {
581
469
  * @since v0.4.0
582
470
  */
583
471
  @unsafe
584
- export let clz = (value: Int64) => {
472
+ provide let clz = (value: Int64) => {
585
473
  let nv = WasmI64.load(WasmI32.fromGrain(value), 8n)
586
474
  let ptr = newInt64(WasmI64.clz(nv))
587
475
  WasmI32.toGrain(ptr): Int64
@@ -596,7 +484,7 @@ export let clz = (value: Int64) => {
596
484
  * @since v0.4.0
597
485
  */
598
486
  @unsafe
599
- export let ctz = (value: Int64) => {
487
+ provide let ctz = (value: Int64) => {
600
488
  let nv = WasmI64.load(WasmI32.fromGrain(value), 8n)
601
489
  let ptr = newInt64(WasmI64.ctz(nv))
602
490
  WasmI32.toGrain(ptr): Int64
@@ -611,8 +499,37 @@ export let ctz = (value: Int64) => {
611
499
  * @since v0.4.0
612
500
  */
613
501
  @unsafe
614
- export let popcnt = (value: Int64) => {
502
+ provide let popcnt = (value: Int64) => {
615
503
  let nv = WasmI64.load(WasmI32.fromGrain(value), 8n)
616
504
  let ptr = newInt64(WasmI64.popcnt(nv))
617
505
  WasmI32.toGrain(ptr): Int64
618
506
  }
507
+
508
+ // Exponentiation by squaring https://en.wikipedia.org/wiki/Exponentiation_by_squaring special path for int^int
509
+ let rec expBySquaring = (y, x, n) => {
510
+ if (n == 0L) {
511
+ 1L
512
+ } else if (n == 1L) {
513
+ x * y
514
+ } else if (n % 2L == 0L) {
515
+ expBySquaring(y, x * x, n / 2L)
516
+ } else {
517
+ expBySquaring(x * y, x * x, (n - 1L) / 2L)
518
+ }
519
+ }
520
+
521
+ /**
522
+ * Computes the exponentiation of the given base and power.
523
+ *
524
+ * @param base: The base number
525
+ * @param power: The exponent number
526
+ * @returns The base raised to the given power
527
+ *
528
+ * @since v0.6.0
529
+ */
530
+ provide let (**) = (base, power) => {
531
+ if (power < 0L)
532
+ return expBySquaring(1L, 1L / base, power * -1L)
533
+ else
534
+ return expBySquaring(1L, base, power)
535
+ }