@grain/stdlib 0.5.12 → 0.6.0

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