ic-mops 0.8.4 → 0.8.5

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 (99) hide show
  1. package/.mops/base@0.7.4/LICENSE +208 -0
  2. package/.mops/base@0.7.4/README.md +64 -0
  3. package/.mops/base@0.7.4/mops.toml +5 -0
  4. package/.mops/base@0.7.4/src/Array.mo +686 -0
  5. package/.mops/base@0.7.4/src/AssocList.mo +203 -0
  6. package/.mops/base@0.7.4/src/Blob.mo +55 -0
  7. package/.mops/base@0.7.4/src/Bool.mo +44 -0
  8. package/.mops/base@0.7.4/src/Buffer.mo +1937 -0
  9. package/.mops/base@0.7.4/src/CertifiedData.mo +29 -0
  10. package/.mops/base@0.7.4/src/Char.mo +67 -0
  11. package/.mops/base@0.7.4/src/Debug.mo +15 -0
  12. package/.mops/base@0.7.4/src/Deque.mo +75 -0
  13. package/.mops/base@0.7.4/src/Error.mo +41 -0
  14. package/.mops/base@0.7.4/src/ExperimentalCycles.mo +51 -0
  15. package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +36 -0
  16. package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +121 -0
  17. package/.mops/base@0.7.4/src/Float.mo +150 -0
  18. package/.mops/base@0.7.4/src/Func.mo +38 -0
  19. package/.mops/base@0.7.4/src/Hash.mo +83 -0
  20. package/.mops/base@0.7.4/src/HashMap.mo +229 -0
  21. package/.mops/base@0.7.4/src/Heap.mo +113 -0
  22. package/.mops/base@0.7.4/src/Int.mo +150 -0
  23. package/.mops/base@0.7.4/src/Int16.mo +159 -0
  24. package/.mops/base@0.7.4/src/Int32.mo +160 -0
  25. package/.mops/base@0.7.4/src/Int64.mo +161 -0
  26. package/.mops/base@0.7.4/src/Int8.mo +160 -0
  27. package/.mops/base@0.7.4/src/Iter.mo +220 -0
  28. package/.mops/base@0.7.4/src/IterType.mo +7 -0
  29. package/.mops/base@0.7.4/src/List.mo +433 -0
  30. package/.mops/base@0.7.4/src/Nat.mo +75 -0
  31. package/.mops/base@0.7.4/src/Nat16.mo +146 -0
  32. package/.mops/base@0.7.4/src/Nat32.mo +146 -0
  33. package/.mops/base@0.7.4/src/Nat64.mo +146 -0
  34. package/.mops/base@0.7.4/src/Nat8.mo +146 -0
  35. package/.mops/base@0.7.4/src/None.mo +19 -0
  36. package/.mops/base@0.7.4/src/Option.mo +160 -0
  37. package/.mops/base@0.7.4/src/Order.mo +46 -0
  38. package/.mops/base@0.7.4/src/Prelude.mo +33 -0
  39. package/.mops/base@0.7.4/src/Principal.mo +58 -0
  40. package/.mops/base@0.7.4/src/RBTree.mo +218 -0
  41. package/.mops/base@0.7.4/src/Random.mo +188 -0
  42. package/.mops/base@0.7.4/src/Result.mo +210 -0
  43. package/.mops/base@0.7.4/src/Stack.mo +40 -0
  44. package/.mops/base@0.7.4/src/Text.mo +615 -0
  45. package/.mops/base@0.7.4/src/Time.mo +37 -0
  46. package/.mops/base@0.7.4/src/Trie.mo +1200 -0
  47. package/.mops/base@0.7.4/src/TrieMap.mo +180 -0
  48. package/.mops/base@0.7.4/src/TrieSet.mo +97 -0
  49. package/.mops/base@0.8.3/LICENSE +208 -0
  50. package/.mops/base@0.8.3/README.md +64 -0
  51. package/.mops/base@0.8.3/mops.toml +6 -0
  52. package/.mops/base@0.8.3/src/Array.mo +717 -0
  53. package/.mops/base@0.8.3/src/AssocList.mo +404 -0
  54. package/.mops/base@0.8.3/src/Blob.mo +212 -0
  55. package/.mops/base@0.8.3/src/Bool.mo +44 -0
  56. package/.mops/base@0.8.3/src/Buffer.mo +2660 -0
  57. package/.mops/base@0.8.3/src/CertifiedData.mo +53 -0
  58. package/.mops/base@0.8.3/src/Char.mo +65 -0
  59. package/.mops/base@0.8.3/src/Debug.mo +56 -0
  60. package/.mops/base@0.8.3/src/Deque.mo +243 -0
  61. package/.mops/base@0.8.3/src/Error.mo +68 -0
  62. package/.mops/base@0.8.3/src/ExperimentalCycles.mo +151 -0
  63. package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +60 -0
  64. package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +348 -0
  65. package/.mops/base@0.8.3/src/Float.mo +843 -0
  66. package/.mops/base@0.8.3/src/Func.mo +46 -0
  67. package/.mops/base@0.8.3/src/Hash.mo +82 -0
  68. package/.mops/base@0.8.3/src/HashMap.mo +457 -0
  69. package/.mops/base@0.8.3/src/Heap.mo +233 -0
  70. package/.mops/base@0.8.3/src/Int.mo +365 -0
  71. package/.mops/base@0.8.3/src/Int16.mo +521 -0
  72. package/.mops/base@0.8.3/src/Int32.mo +522 -0
  73. package/.mops/base@0.8.3/src/Int64.mo +522 -0
  74. package/.mops/base@0.8.3/src/Int8.mo +522 -0
  75. package/.mops/base@0.8.3/src/Iter.mo +227 -0
  76. package/.mops/base@0.8.3/src/IterType.mo +7 -0
  77. package/.mops/base@0.8.3/src/List.mo +930 -0
  78. package/.mops/base@0.8.3/src/Nat.mo +305 -0
  79. package/.mops/base@0.8.3/src/Nat16.mo +144 -0
  80. package/.mops/base@0.8.3/src/Nat32.mo +144 -0
  81. package/.mops/base@0.8.3/src/Nat64.mo +144 -0
  82. package/.mops/base@0.8.3/src/Nat8.mo +144 -0
  83. package/.mops/base@0.8.3/src/None.mo +19 -0
  84. package/.mops/base@0.8.3/src/Option.mo +154 -0
  85. package/.mops/base@0.8.3/src/Order.mo +46 -0
  86. package/.mops/base@0.8.3/src/Prelude.mo +33 -0
  87. package/.mops/base@0.8.3/src/Principal.mo +249 -0
  88. package/.mops/base@0.8.3/src/RBTree.mo +681 -0
  89. package/.mops/base@0.8.3/src/Random.mo +270 -0
  90. package/.mops/base@0.8.3/src/Result.mo +209 -0
  91. package/.mops/base@0.8.3/src/Stack.mo +93 -0
  92. package/.mops/base@0.8.3/src/Text.mo +761 -0
  93. package/.mops/base@0.8.3/src/Time.mo +36 -0
  94. package/.mops/base@0.8.3/src/Timer.mo +62 -0
  95. package/.mops/base@0.8.3/src/Trie.mo +1603 -0
  96. package/.mops/base@0.8.3/src/TrieMap.mo +392 -0
  97. package/.mops/base@0.8.3/src/TrieSet.mo +148 -0
  98. package/network.txt +1 -0
  99. package/package.json +2 -2
@@ -0,0 +1,522 @@
1
+ /// 32-bit signed integers with checked arithmetic.
2
+ ///
3
+ /// Common 32-bit integer functions.
4
+ /// Most operations are available as built-in operators (e.g. `1 + 1`).
5
+ import Int "Int";
6
+ import Prim "mo:⛔";
7
+
8
+ module {
9
+
10
+ /// 32-bit signed integers.
11
+ public type Int32 = Prim.Types.Int32;
12
+
13
+ /// Minimum 32-bit integer value, `-2 ** 31`.
14
+ public let minimumValue = -2_147_483_648 : Int32;
15
+
16
+ /// Maximum 32-bit integer value, `+2 ** 31 - 1`.
17
+ public let maximumValue = 2_147_483_647 : Int32;
18
+
19
+ /// Converts a 32-bit signed integer to a signed integer with infinite precision.
20
+ ///
21
+ /// Example:
22
+ /// ```motoko
23
+ /// import Int32 "mo:base/Int32";
24
+ ///
25
+ /// Int32.toInt(123_456) // => 123_456 : Int
26
+ /// ```
27
+ public let toInt : Int32 -> Int = Prim.int32ToInt;
28
+
29
+ /// Converts a signed integer with infinite precision to a 32-bit signed integer.
30
+ ///
31
+ /// Traps on overflow/underflow.
32
+ ///
33
+ /// Example:
34
+ /// ```motoko
35
+ /// import Int32 "mo:base/Int32";
36
+ ///
37
+ /// Int32.fromInt(123_456) // => +123_456 : Int32
38
+ /// ```
39
+ public let fromInt : Int -> Int32 = Prim.intToInt32;
40
+
41
+ /// Converts a signed integer with infinite precision to a 32-bit signed integer.
42
+ ///
43
+ /// Wraps on overflow/underflow.
44
+ ///
45
+ /// Example:
46
+ /// ```motoko
47
+ /// import Int32 "mo:base/Int32";
48
+ ///
49
+ /// Int32.fromIntWrap(-123_456) // => -123_456 : Int
50
+ /// ```
51
+ public let fromIntWrap : Int -> Int32 = Prim.intToInt32Wrap;
52
+
53
+ /// Converts an unsigned 32-bit integer to a signed 32-bit integer.
54
+ ///
55
+ /// Wraps on overflow/underflow.
56
+ ///
57
+ /// Example:
58
+ /// ```motoko
59
+ /// import Int32 "mo:base/Int32";
60
+ ///
61
+ /// Int32.fromNat32(123_456) // => +123_456 : Int32
62
+ /// ```
63
+ public let fromNat32 : Nat32 -> Int32 = Prim.nat32ToInt32;
64
+
65
+ /// Converts a signed 32-bit integer to an unsigned 32-bit integer.
66
+ ///
67
+ /// Wraps on overflow/underflow.
68
+ ///
69
+ /// Example:
70
+ /// ```motoko
71
+ /// import Int32 "mo:base/Int32";
72
+ ///
73
+ /// Int32.toNat32(-1) // => 4_294_967_295 : Nat32 // underflow
74
+ /// ```
75
+ public let toNat32 : Int32 -> Nat32 = Prim.int32ToNat32;
76
+
77
+ /// Returns the Text representation of `x`.
78
+ /// Formats the integer in decimal representation without underscore separators for thousand figures.
79
+ ///
80
+ /// Example:
81
+ /// ```motoko
82
+ /// import Int32 "mo:base/Int32";
83
+ ///
84
+ /// Int32.toText(-123456) // => "-123456"
85
+ /// ```
86
+ public func toText(x : Int32) : Text {
87
+ Int.toText(toInt(x))
88
+ };
89
+
90
+ /// Returns the absolute value of `x`.
91
+ ///
92
+ /// Traps when `x == -2 ** 31` (the minimum `Int32` value).
93
+ ///
94
+ /// Example:
95
+ /// ```motoko
96
+ /// import Int32 "mo:base/Int32";
97
+ ///
98
+ /// Int32.abs(-123456) // => +123_456
99
+ /// ```
100
+ public func abs(x : Int32) : Int32 {
101
+ fromInt(Int.abs(toInt(x)))
102
+ };
103
+
104
+ /// Returns the minimum of `x` and `y`.
105
+ ///
106
+ /// Example:
107
+ /// ```motoko
108
+ /// import Int32 "mo:base/Int32";
109
+ ///
110
+ /// Int32.min(+2, -3) // => -3
111
+ /// ```
112
+ public func min(x : Int32, y : Int32) : Int32 {
113
+ if (x < y) { x } else { y }
114
+ };
115
+
116
+ /// Returns the maximum of `x` and `y`.
117
+ ///
118
+ /// Example:
119
+ /// ```motoko
120
+ /// import Int32 "mo:base/Int32";
121
+ ///
122
+ /// Int32.max(+2, -3) // => +2
123
+ /// ```
124
+ public func max(x : Int32, y : Int32) : Int32 {
125
+ if (x < y) { y } else { x }
126
+ };
127
+
128
+ /// Returns `x == y`.
129
+ ///
130
+ /// Example:
131
+ /// ```motoko
132
+ /// import Int32 "mo:base/Int32";
133
+ ///
134
+ /// Int32.equal(123, 123) // => true
135
+ /// ```
136
+ public func equal(x : Int32, y : Int32) : Bool { x == y };
137
+
138
+ /// Returns `x != y`.
139
+ ///
140
+ /// Example:
141
+ /// ```motoko
142
+ /// import Int32 "mo:base/Int32";
143
+ ///
144
+ /// Int32.notEqual(123, 123) // => false
145
+ /// ```
146
+ public func notEqual(x : Int32, y : Int32) : Bool { x != y };
147
+
148
+ /// Returns `x < y`.
149
+ ///
150
+ /// Example:
151
+ /// ```motoko
152
+ /// import Int32 "mo:base/Int32";
153
+ ///
154
+ /// Int32.less(123, 1234) // => true
155
+ /// ```
156
+ public func less(x : Int32, y : Int32) : Bool { x < y };
157
+
158
+ /// Returns `x <= y`.
159
+ ///
160
+ /// Example:
161
+ /// ```motoko
162
+ /// import Int32 "mo:base/Int32";
163
+ ///
164
+ /// Int32.lessOrEqual(123, 1234) // => true
165
+ /// ```
166
+ public func lessOrEqual(x : Int32, y : Int32) : Bool { x <= y };
167
+
168
+ /// Returns `x > y`.
169
+ ///
170
+ /// Example:
171
+ /// ```motoko
172
+ /// import Int32 "mo:base/Int32";
173
+ ///
174
+ /// Int32.greater(1234, 123) // => true
175
+ /// ```
176
+ public func greater(x : Int32, y : Int32) : Bool { x > y };
177
+
178
+ /// Returns `x >= y`.
179
+ ///
180
+ /// Example:
181
+ /// ```motoko
182
+ /// import Int32 "mo:base/Int32";
183
+ ///
184
+ /// Int32.greaterOrEqual(1234, 123) // => true
185
+ /// ```
186
+ public func greaterOrEqual(x : Int32, y : Int32) : Bool { x >= y };
187
+
188
+ /// Returns the order of `x` and `y`.
189
+ ///
190
+ /// Example:
191
+ /// ```motoko
192
+ /// import Int32 "mo:base/Int32";
193
+ ///
194
+ /// Int32.compare(123, 1234) // => #less
195
+ /// ```
196
+ public func compare(x : Int32, y : Int32) : { #less; #equal; #greater } {
197
+ if (x < y) { #less } else if (x == y) { #equal } else { #greater }
198
+ };
199
+
200
+ /// Returns the negation of `x`, `-x`.
201
+ ///
202
+ /// Traps on overflow, i.e. for `neg(-2 ** 31)`.
203
+ ///
204
+ ///
205
+ /// Example:
206
+ /// ```motoko
207
+ /// import Int32 "mo:base/Int32";
208
+ ///
209
+ /// Int32.neg(123) // => -123
210
+ /// ```
211
+ public func neg(x : Int32) : Int32 { -x };
212
+
213
+ /// Returns the sum of `x` and `y`, `x + y`.
214
+ ///
215
+ /// Traps on overflow/underflow.
216
+ ///
217
+ /// Example:
218
+ /// ```motoko
219
+ /// import Int32 "mo:base/Int32";
220
+ ///
221
+ /// Int32.add(1234, 123) // => +1_357
222
+ /// ```
223
+ public func add(x : Int32, y : Int32) : Int32 { x + y };
224
+
225
+ /// Returns the difference of `x` and `y`, `x - y`.
226
+ ///
227
+ /// Traps on overflow/underflow.
228
+ ///
229
+ /// Example:
230
+ /// ```motoko
231
+ /// import Int32 "mo:base/Int32";
232
+ ///
233
+ /// Int32.sub(1234, 123) // => +1_111
234
+ /// ```
235
+ public func sub(x : Int32, y : Int32) : Int32 { x - y };
236
+
237
+ /// Returns the product of `x` and `y`, `x * y`.
238
+ ///
239
+ /// Traps on overflow/underflow.
240
+ ///
241
+ /// Example:
242
+ /// ```motoko
243
+ /// import Int32 "mo:base/Int32";
244
+ ///
245
+ /// Int32.mul(123, 100) // => +12_300
246
+ /// ```
247
+ public func mul(x : Int32, y : Int32) : Int32 { x * y };
248
+
249
+ /// Returns the signed integer division of `x` by `y`, `x / y`.
250
+ /// Rounds the quotient towards zero, which is the same as truncating the decimal places of the quotient.
251
+ ///
252
+ /// Traps when `y` is zero.
253
+ ///
254
+ /// Example:
255
+ /// ```motoko
256
+ /// import Int32 "mo:base/Int32";
257
+ ///
258
+ /// Int32.div(123, 10) // => +12
259
+ /// ```
260
+ public func div(x : Int32, y : Int32) : Int32 { x / y };
261
+
262
+ /// Returns the remainder of the signed integer division of `x` by `y`, `x % y`,
263
+ /// which is defined as `x - x / y * y`.
264
+ ///
265
+ /// Traps when `y` is zero.
266
+ ///
267
+ /// Example:
268
+ /// ```motoko
269
+ /// import Int32 "mo:base/Int32";
270
+ ///
271
+ /// Int32.rem(123, 10) // => +3
272
+ /// ```
273
+ public func rem(x : Int32, y : Int32) : Int32 { x % y };
274
+
275
+ /// Returns `x` to the power of `y`, `x ** y`.
276
+ ///
277
+ /// Traps on overflow/underflow and when `y < 0 or y >= 32`.
278
+ ///
279
+ /// Example:
280
+ /// ```motoko
281
+ /// import Int32 "mo:base/Int32";
282
+ ///
283
+ /// Int32.pow(2, 10) // => +1_024
284
+ /// ```
285
+ public func pow(x : Int32, y : Int32) : Int32 { x ** y };
286
+
287
+ /// Returns the bitwise negation of `x`, `^x`.
288
+ ///
289
+ /// Example:
290
+ /// ```motoko
291
+ /// import Int32 "mo:base/Int32";
292
+ ///
293
+ /// Int32.bitnot(-256 /* 0xffff_ff00 */) // => +255 // 0xff
294
+ /// ```
295
+ public func bitnot(x : Int32) : Int32 { ^x };
296
+
297
+ /// Returns the bitwise "and" of `x` and `y`, `x & y`.
298
+ ///
299
+ /// Example:
300
+ /// ```motoko
301
+ /// import Int32 "mo:base/Int32";
302
+ ///
303
+ /// Int32.bitand(0xffff, 0x00f0) // => +240 // 0xf0
304
+ /// ```
305
+ public func bitand(x : Int32, y : Int32) : Int32 { x & y };
306
+
307
+ /// Returns the bitwise "or" of `x` and `y`, `x | y`.
308
+ ///
309
+ /// Example:
310
+ /// ```motoko
311
+ /// import Int32 "mo:base/Int32";
312
+ ///
313
+ /// Int32.bitor(0xffff, 0x00f0) // => +65_535 // 0xffff
314
+ /// ```
315
+ public func bitor(x : Int32, y : Int32) : Int32 { x | y };
316
+
317
+ /// Returns the bitwise "exclusive or" of `x` and `y`, `x ^ y`.
318
+ ///
319
+ /// Example:
320
+ /// ```motoko
321
+ /// import Int32 "mo:base/Int32";
322
+ ///
323
+ /// Int32.bitxor(0xffff, 0x00f0) // => +65_295 // 0xff0f
324
+ /// ```
325
+ public func bitxor(x : Int32, y : Int32) : Int32 { x ^ y };
326
+
327
+ /// Returns the bitwise left shift of `x` by `y`, `x << y`.
328
+ /// The right bits of the shift filled with zeros.
329
+ /// Left-overflowing bits, including the sign bit, are discarded.
330
+ ///
331
+ /// For `y >= 32`, the semantics is the same as for `bitshiftLeft(x, y % 32)`.
332
+ /// For `y < 0`, the semantics is the same as for `bitshiftLeft(x, y + y % 32)`.
333
+ ///
334
+ /// Example:
335
+ /// ```motoko
336
+ /// import Int32 "mo:base/Int32";
337
+ ///
338
+ /// Int32.bitshiftLeft(1, 8) // => +256 // 0x100 equivalent to `2 ** 8`.
339
+ /// ```
340
+ public func bitshiftLeft(x : Int32, y : Int32) : Int32 { x << y };
341
+
342
+ /// Returns the signed bitwise right shift of `x` by `y`, `x >> y`.
343
+ /// The sign bit is retained and the left side is filled with the sign bit.
344
+ /// Right-underflowing bits are discarded, i.e. not rotated to the left side.
345
+ ///
346
+ /// For `y >= 32`, the semantics is the same as for `bitshiftRight(x, y % 32)`.
347
+ /// For `y < 0`, the semantics is the same as for `bitshiftRight (x, y + y % 32)`.
348
+ ///
349
+ /// Example:
350
+ /// ```motoko
351
+ /// import Int32 "mo:base/Int32";
352
+ ///
353
+ /// Int32.bitshiftRight(1024, 8) // => +4 // equivalent to `1024 / (2 ** 8)`
354
+ /// ```
355
+ public func bitshiftRight(x : Int32, y : Int32) : Int32 { x >> y };
356
+
357
+ /// Returns the bitwise left rotatation of `x` by `y`, `x <<> y`.
358
+ /// Each left-overflowing bit is inserted again on the right side.
359
+ /// The sign bit is rotated like other bits, i.e. the rotation interprets the number as unsigned.
360
+ ///
361
+ /// Changes the direction of rotation for negative `y`.
362
+ /// For `y >= 32`, the semantics is the same as for `bitrotLeft(x, y % 32)`.
363
+ ///
364
+ /// Example:
365
+ /// ```motoko
366
+ /// import Int32 "mo:base/Int32";
367
+ ///
368
+ /// Int32.bitrotLeft(0x2000_0001, 4) // => +18 // 0x12.
369
+ /// ```
370
+ public func bitrotLeft(x : Int32, y : Int32) : Int32 { x <<> y };
371
+
372
+ /// Returns the bitwise right rotation of `x` by `y`, `x <>> y`.
373
+ /// Each right-underflowing bit is inserted again on the right side.
374
+ /// The sign bit is rotated like other bits, i.e. the rotation interprets the number as unsigned.
375
+ ///
376
+ /// Changes the direction of rotation for negative `y`.
377
+ /// For `y >= 32`, the semantics is the same as for `bitrotRight(x, y % 32)`.
378
+ ///
379
+ /// Example:
380
+ /// ```motoko
381
+ /// import Int32 "mo:base/Int32";
382
+ ///
383
+ /// Int32.bitrotRight(0x0002_0001, 8) // => +16_777_728 // 0x0100_0200.
384
+ /// ```
385
+ public func bitrotRight(x : Int32, y : Int32) : Int32 { x <>> y };
386
+
387
+ /// Returns the value of bit `p` in `x`, `x & 2**p == 2**p`.
388
+ /// If `p >= 32`, the semantics is the same as for `bittest(x, p % 32)`.
389
+ ///
390
+ /// Example:
391
+ /// ```motoko
392
+ /// import Int32 "mo:base/Int32";
393
+ ///
394
+ /// Int32.bittest(128, 7) // => true
395
+ /// ```
396
+ public func bittest(x : Int32, p : Nat) : Bool {
397
+ Prim.btstInt32(x, Prim.intToInt32(p))
398
+ };
399
+
400
+ /// Returns the value of setting bit `p` in `x` to `1`.
401
+ /// If `p >= 32`, the semantics is the same as for `bitset(x, p % 32)`.
402
+ ///
403
+ /// Example:
404
+ /// ```motoko
405
+ /// import Int32 "mo:base/Int32";
406
+ ///
407
+ /// Int32.bitset(0, 7) // => +128
408
+ /// ```
409
+ public func bitset(x : Int32, p : Nat) : Int32 {
410
+ x | (1 << Prim.intToInt32(p))
411
+ };
412
+
413
+ /// Returns the value of clearing bit `p` in `x` to `0`.
414
+ /// If `p >= 32`, the semantics is the same as for `bitclear(x, p % 32)`.
415
+ ///
416
+ /// Example:
417
+ /// ```motoko
418
+ /// import Int32 "mo:base/Int32";
419
+ ///
420
+ /// Int32.bitclear(-1, 7) // => -129
421
+ /// ```
422
+ public func bitclear(x : Int32, p : Nat) : Int32 {
423
+ x & ^(1 << Prim.intToInt32(p))
424
+ };
425
+
426
+ /// Returns the value of flipping bit `p` in `x`.
427
+ /// If `p >= 32`, the semantics is the same as for `bitclear(x, p % 32)`.
428
+ ///
429
+ /// Example:
430
+ /// ```motoko
431
+ /// import Int32 "mo:base/Int32";
432
+ ///
433
+ /// Int32.bitflip(255, 7) // => +127
434
+ /// ```
435
+ public func bitflip(x : Int32, p : Nat) : Int32 {
436
+ x ^ (1 << Prim.intToInt32(p))
437
+ };
438
+
439
+ /// Returns the count of non-zero bits in `x`.
440
+ ///
441
+ /// Example:
442
+ /// ```motoko
443
+ /// import Int32 "mo:base/Int32";
444
+ ///
445
+ /// Int32.bitcountNonZero(0xffff) // => +16
446
+ /// ```
447
+ public let bitcountNonZero : (x : Int32) -> Int32 = Prim.popcntInt32;
448
+
449
+ /// Returns the count of leading zero bits in `x`.
450
+ ///
451
+ /// Example:
452
+ /// ```motoko
453
+ /// import Int32 "mo:base/Int32";
454
+ ///
455
+ /// Int32.bitcountLeadingZero(0x8000) // => +16
456
+ /// ```
457
+ public let bitcountLeadingZero : (x : Int32) -> Int32 = Prim.clzInt32;
458
+
459
+ /// Returns the count of trailing zero bits in `x`.
460
+ ///
461
+ /// Example:
462
+ /// ```motoko
463
+ /// import Int32 "mo:base/Int32";
464
+ ///
465
+ /// Int32.bitcountTrailingZero(0x0201_0000) // => +16
466
+ /// ```
467
+ public let bitcountTrailingZero : (x : Int32) -> Int32 = Prim.ctzInt32;
468
+
469
+ /// Returns the sum of `x` and `y`, `x +% y`.
470
+ ///
471
+ /// Wraps on overflow/underflow.
472
+ ///
473
+ ///
474
+ /// Example:
475
+ /// ```motoko
476
+ /// import Int32 "mo:base/Int32";
477
+ ///
478
+ /// Int32.addWrap(2 ** 30, 2 ** 30) // => -2_147_483_648 // overflow
479
+ /// ```
480
+ public func addWrap(x : Int32, y : Int32) : Int32 { x +% y };
481
+
482
+ /// Returns the difference of `x` and `y`, `x -% y`.
483
+ ///
484
+ /// Wraps on overflow/underflow.
485
+ ///
486
+ ///
487
+ /// Example:
488
+ /// ```motoko
489
+ /// import Int32 "mo:base/Int32";
490
+ ///
491
+ /// Int32.subWrap(-2 ** 31, 1) // => +2_147_483_647 // underflow
492
+ /// ```
493
+ public func subWrap(x : Int32, y : Int32) : Int32 { x -% y };
494
+
495
+ /// Returns the product of `x` and `y`, `x *% y`. Wraps on overflow.
496
+ ///
497
+ /// Wraps on overflow/underflow.
498
+ ///
499
+ ///
500
+ /// Example:
501
+ /// ```motoko
502
+ /// import Int32 "mo:base/Int32";
503
+ ///
504
+ /// Int32.mulWrap(2 ** 16, 2 ** 16) // => 0 // overflow
505
+ /// ```
506
+ public func mulWrap(x : Int32, y : Int32) : Int32 { x *% y };
507
+
508
+ /// Returns `x` to the power of `y`, `x **% y`.
509
+ ///
510
+ /// Wraps on overflow/underflow.
511
+ /// Traps if `y < 0 or y >= 32`.
512
+ ///
513
+ ///
514
+ /// Example:
515
+ /// ```motoko
516
+ /// import Int32 "mo:base/Int32";
517
+ ///
518
+ /// Int32.powWrap(2, 31) // => -2_147_483_648 // overflow
519
+ /// ```
520
+ public func powWrap(x : Int32, y : Int32) : Int32 { x **% y };
521
+
522
+ }