functionalscript 0.0.569 → 0.0.570

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.569",
3
+ "version": "0.0.570",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "scripts": {
6
6
  "tsc": "tsc",
@@ -44,17 +44,33 @@ const scalar_mul = ({ 0: _0, add }) => a => n => {
44
44
  }
45
45
  }
46
46
 
47
- /** @type {(a: bigint) => bigint} */
48
- const log2 = a => {
49
- // Possible optimization: use a binary search in 32 bit value
50
- let i = -1n
51
- while (a > 0n) {
52
- ++i
53
- a >>= 1n
47
+ const bit_len = (/** @type {bigint} */v) => {
48
+ if (v < 0n) { v = -v }
49
+ if (v === 0n) { return 0n }
50
+ let result = 1n
51
+ let i = 1n
52
+ while (true) {
53
+ const n = v >> i
54
+ if (n === 0n) {
55
+ break
56
+ }
57
+ v = n
58
+ result += i
59
+ i <<= 1n
54
60
  }
55
- return i
61
+ while (i !== 0n) {
62
+ i >>= 1n
63
+ const n = v >> i
64
+ if (n !== 0n) {
65
+ result += i
66
+ v = n
67
+ }
68
+ }
69
+ return result
56
70
  }
57
71
 
72
+ const log2 = (/** @type {bigint} */v) => v <= 0n ? -1n : bit_len(v) - 1n
73
+
58
74
  module.exports = {
59
75
  /** @readonly */
60
76
  addition,
@@ -70,4 +86,6 @@ module.exports = {
70
86
  scalar_mul,
71
87
  /** @readonly */
72
88
  log2,
89
+ /** @readonly */
90
+ bit_len,
73
91
  }
@@ -1,4 +1,4 @@
1
- const { sum, abs, serialize, log2 } = require('./module.f.cjs')
1
+ const { sum, abs, serialize, log2, bit_len } = require('./module.f.cjs')
2
2
 
3
3
  module.exports = {
4
4
  sum: () => {
@@ -69,6 +69,48 @@ module.exports = {
69
69
  () => {
70
70
  const result = log2(16n)
71
71
  if (result !== 4n) { throw result }
72
+ },
73
+ () => {
74
+ // max for Bun (131_072 Bytes)
75
+ const v = 1_048_575n
76
+ const result = log2(1n << v)
77
+ if (result !== v) { throw result }
72
78
  }
73
- ]
79
+ ],
80
+ bit_len: {
81
+ 0: () => {
82
+ const s = bit_len(0n)
83
+ if (s !== 0n) { throw s }
84
+ },
85
+ m: () => {
86
+ let i = 0n
87
+ while (i < 10_000n) {
88
+ const s = bit_len(1n << i)
89
+ if (s !== i + 1n) { throw [s, i] }
90
+ i += 1n
91
+ }
92
+ },
93
+ big: () => {
94
+ const s = bit_len(1n << 1_000_000n)
95
+ if (s !== 1_000_001n) { throw s }
96
+ },
97
+ neg: [
98
+ () => {
99
+ const s = bit_len(-1n)
100
+ if (s !== 1n) { throw s }
101
+ },
102
+ () => {
103
+ const s = bit_len(-2n)
104
+ if (s !== 2n) { throw s }
105
+ },
106
+ () => {
107
+ const s = bit_len(-3n)
108
+ if (s !== 2n) { throw s }
109
+ },
110
+ () => {
111
+ const s = bit_len(-4n)
112
+ if (s !== 3n) { throw s }
113
+ },
114
+ ]
115
+ }
74
116
  }