codecin 7.22.10__py3-none-win_amd64.whl
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.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "bigint"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 任意精度整数"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/bigint.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "bigint"]
|
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 任意精度整数 (bigint)
|
|
2
|
+
// 用法: import "bigint.cin"
|
|
3
|
+
//
|
|
4
|
+
// 表示: 十进制数位数组, 低位存放在下标 0 (d[0] 是个位), n 为有效数位个数,
|
|
5
|
+
// neg 为符号位 (0 非负 / 1 负)。整块数据都装在 struct 的固长数组里,
|
|
6
|
+
// 因此不需要任何动态内存, 也不依赖宿主能力内建。
|
|
7
|
+
//
|
|
8
|
+
// 容量: BIGINT_DIGITS = 64 位十进制数位 (最大 10^64 - 1)。
|
|
9
|
+
// 溢出: 结果超出容量时**饱和** —— 绝对值变成 10^64 - 1 (符号保留), 同时把
|
|
10
|
+
// 全局溢出标志置 1; 用 bi_overflow() 查询 / bi_reset_overflow() 清除。
|
|
11
|
+
// 饱和后继续参与运算会继续得到饱和值, 不会崩溃。
|
|
12
|
+
//
|
|
13
|
+
// 内存: 每个 BigInt 值是 66 槽 = 528 字节。遵循 CIN 值语义的写法
|
|
14
|
+
// (`BigInt r = bi_add(a, b)`) 会在堆上分配一块 struct 且不回收;
|
|
15
|
+
// 默认 64 KiB 内存下整个程序大约只能承受 50 余次这样的"返回新值"调用。
|
|
16
|
+
// 库内所有循环都改用 bi_xxx_into(a, b, out) 复用调用方提供的 out,
|
|
17
|
+
// 所以 bi_fact / bi_pow 这类长循环**只占固定几块内存**。
|
|
18
|
+
// 自己写长循环时也请用 *_into 形式, 或给 cpu.py 加大 --mem-size。
|
|
19
|
+
|
|
20
|
+
struct BigInt {
|
|
21
|
+
int d[64]
|
|
22
|
+
int n
|
|
23
|
+
int neg
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 容量常量 (与 struct 的 d[64] 维度一致, 约定只读)
|
|
27
|
+
int BIGINT_DIGITS = 64
|
|
28
|
+
|
|
29
|
+
// 溢出标志: 1 表示曾经发生过容量饱和 (粘滞, 需要手动清除)
|
|
30
|
+
int bi_ovf = 0
|
|
31
|
+
|
|
32
|
+
// ---------------- 基础 ----------------
|
|
33
|
+
|
|
34
|
+
// 固定返回容量常量 64
|
|
35
|
+
function bi_capacity() -> int {
|
|
36
|
+
return 64
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 是否发生过越界饱和: 1 / 0
|
|
40
|
+
function bi_overflow() -> int {
|
|
41
|
+
return bi_ovf
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 清除溢出标志
|
|
45
|
+
function bi_reset_overflow() -> void {
|
|
46
|
+
bi_ovf = 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 置零 (清空全部数位, n = 1, neg = 0)
|
|
50
|
+
function bi_clear(BigInt a) -> void {
|
|
51
|
+
for (int i = 0; i < 64; i = i + 1) {
|
|
52
|
+
a.d[i] = 0
|
|
53
|
+
}
|
|
54
|
+
a.n = 1
|
|
55
|
+
a.neg = 0
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 规范化: 收紧 n、去掉前导零、零的符号归 0
|
|
59
|
+
function bi_norm(BigInt a) -> void {
|
|
60
|
+
int n = a.n
|
|
61
|
+
if (n < 1) {
|
|
62
|
+
n = 1
|
|
63
|
+
a.d[0] = 0
|
|
64
|
+
}
|
|
65
|
+
if (n > 64) { n = 64 }
|
|
66
|
+
int i = n - 1
|
|
67
|
+
while (i > 0 && a.d[i] == 0) { i = i - 1 }
|
|
68
|
+
a.n = i + 1
|
|
69
|
+
if (a.n == 1 && a.d[0] == 0) { a.neg = 0 }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 值拷贝 (槽级复制, 不做指针别名), out 可与 a 为同一结构体
|
|
73
|
+
function bi_copy(BigInt dst, BigInt src) -> void {
|
|
74
|
+
int i = 0
|
|
75
|
+
while (i < 64) {
|
|
76
|
+
dst.d[i] = src.d[i]
|
|
77
|
+
i = i + 1
|
|
78
|
+
}
|
|
79
|
+
dst.n = src.n
|
|
80
|
+
dst.neg = src.neg
|
|
81
|
+
bi_norm(dst)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 饱和到 10^64 - 1, 符号由 s 决定并置溢出标志
|
|
85
|
+
function bi_saturate(BigInt a, int s) -> void {
|
|
86
|
+
for (int i = 0; i < 64; i = i + 1) { a.d[i] = 9 }
|
|
87
|
+
a.n = 64
|
|
88
|
+
a.neg = 0
|
|
89
|
+
if (s < 0) { a.neg = 1 }
|
|
90
|
+
bi_ovf = 1
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 符号: -1 / 0 / 1 (会先规范化 a)
|
|
94
|
+
function bi_sign(BigInt a) -> int {
|
|
95
|
+
bi_norm(a)
|
|
96
|
+
if (a.n == 1 && a.d[0] == 0) { return 0 }
|
|
97
|
+
if (a.neg != 0) { return -1 }
|
|
98
|
+
return 1
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 是否为零: 1 / 0
|
|
102
|
+
function bi_is_zero(BigInt a) -> int {
|
|
103
|
+
if (bi_sign(a) == 0) { return 1 }
|
|
104
|
+
return 0
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 是否为负数: 1 / 0 (零返回 0)
|
|
108
|
+
function bi_is_negative(BigInt a) -> int {
|
|
109
|
+
if (bi_sign(a) < 0) { return 1 }
|
|
110
|
+
return 0
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 去掉前导零后的位数 (零返回 1)
|
|
114
|
+
function bi_digits(BigInt a) -> int {
|
|
115
|
+
bi_norm(a)
|
|
116
|
+
return a.n
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 是否已经饱和到容量上界 10^64 - 1
|
|
120
|
+
function bi_is_saturated(BigInt a) -> int {
|
|
121
|
+
if (bi_digits(a) != 64) { return 0 }
|
|
122
|
+
int i = 0
|
|
123
|
+
while (i < 64) {
|
|
124
|
+
if (a.d[i] != 9) { return 0 }
|
|
125
|
+
i = i + 1
|
|
126
|
+
}
|
|
127
|
+
return 1
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---------------- 构造 ----------------
|
|
131
|
+
|
|
132
|
+
// 零
|
|
133
|
+
function bi_zero() -> BigInt {
|
|
134
|
+
BigInt r
|
|
135
|
+
r.d[0] = 0
|
|
136
|
+
r.n = 1
|
|
137
|
+
r.neg = 0
|
|
138
|
+
return r
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 由 64 位整数构造 (int 最多 19 位十进制, 永远不会溢出)
|
|
142
|
+
function bi_from_int(int v) -> BigInt {
|
|
143
|
+
BigInt r
|
|
144
|
+
r.d[0] = 0
|
|
145
|
+
r.n = 1
|
|
146
|
+
r.neg = 0
|
|
147
|
+
int x = v
|
|
148
|
+
int sgn = 1
|
|
149
|
+
if (x < 0) { sgn = -1 }
|
|
150
|
+
int i = 0
|
|
151
|
+
while (x != 0) {
|
|
152
|
+
if (i >= 64) {
|
|
153
|
+
bi_saturate(r, sgn)
|
|
154
|
+
return r
|
|
155
|
+
}
|
|
156
|
+
int dig = x % 10
|
|
157
|
+
if (dig < 0) { dig = 0 - dig }
|
|
158
|
+
r.d[i] = dig
|
|
159
|
+
x = idiv(x, 10)
|
|
160
|
+
i = i + 1
|
|
161
|
+
}
|
|
162
|
+
if (i == 0) { return r }
|
|
163
|
+
r.n = i
|
|
164
|
+
r.neg = 0
|
|
165
|
+
if (sgn < 0) { r.neg = 1 }
|
|
166
|
+
bi_norm(r)
|
|
167
|
+
return r
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 解析十进制字符串: 允许前导 '+' / '-'; 遇到第一个非数字字符即停止解析
|
|
171
|
+
// (如 "12ab" -> 12, "abc" -> 0); 有效数字超过容量时饱和为 10^64 - 1
|
|
172
|
+
function bi_from_str(string s) -> BigInt {
|
|
173
|
+
BigInt r
|
|
174
|
+
r.d[0] = 0
|
|
175
|
+
r.n = 1
|
|
176
|
+
r.neg = 0
|
|
177
|
+
int len = strlen(s)
|
|
178
|
+
int i = 0
|
|
179
|
+
int neg = 0
|
|
180
|
+
if (len > 0) {
|
|
181
|
+
int c0 = s[0]
|
|
182
|
+
if (c0 == 45) {
|
|
183
|
+
neg = 1
|
|
184
|
+
i = 1
|
|
185
|
+
} else if (c0 == 43) {
|
|
186
|
+
i = 1
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
int last = i - 1
|
|
190
|
+
int count = 0
|
|
191
|
+
while (i < len) {
|
|
192
|
+
int c = s[i]
|
|
193
|
+
if (c < 48 || c > 57) { break }
|
|
194
|
+
last = i
|
|
195
|
+
count = count + 1
|
|
196
|
+
i = i + 1
|
|
197
|
+
}
|
|
198
|
+
if (count == 0) { return r }
|
|
199
|
+
if (count > 64) {
|
|
200
|
+
bi_saturate(r, 1)
|
|
201
|
+
if (neg == 1) { r.neg = 1 }
|
|
202
|
+
return r
|
|
203
|
+
}
|
|
204
|
+
int k = 0
|
|
205
|
+
while (k < count) {
|
|
206
|
+
r.d[k] = s[last - k] - 48
|
|
207
|
+
k = k + 1
|
|
208
|
+
}
|
|
209
|
+
r.n = count
|
|
210
|
+
r.neg = 0
|
|
211
|
+
if (neg == 1) { r.neg = 1 }
|
|
212
|
+
bi_norm(r)
|
|
213
|
+
return r
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 十进制字符串: 零输出 "0", 负数带前导 '-'
|
|
217
|
+
function bi_to_str(BigInt a) -> string {
|
|
218
|
+
bi_norm(a)
|
|
219
|
+
string out = ""
|
|
220
|
+
if (a.neg != 0) { out = "-" }
|
|
221
|
+
int i = a.n - 1
|
|
222
|
+
while (i >= 0) {
|
|
223
|
+
out = out + int_to_str(a.d[i])
|
|
224
|
+
i = i - 1
|
|
225
|
+
}
|
|
226
|
+
return out
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ---------------- 比较 ----------------
|
|
230
|
+
|
|
231
|
+
// 绝对值比较: -1 / 0 / 1
|
|
232
|
+
function bi_cmp_mag(BigInt a, BigInt b) -> int {
|
|
233
|
+
bi_norm(a)
|
|
234
|
+
bi_norm(b)
|
|
235
|
+
int na = a.n
|
|
236
|
+
int nb = b.n
|
|
237
|
+
if (na != nb) {
|
|
238
|
+
if (na > nb) { return 1 }
|
|
239
|
+
return -1
|
|
240
|
+
}
|
|
241
|
+
int i = na - 1
|
|
242
|
+
while (i >= 0) {
|
|
243
|
+
if (a.d[i] != b.d[i]) {
|
|
244
|
+
if (a.d[i] > b.d[i]) { return 1 }
|
|
245
|
+
return -1
|
|
246
|
+
}
|
|
247
|
+
i = i - 1
|
|
248
|
+
}
|
|
249
|
+
return 0
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// 与数学顺序一致的比较: -1 (a < b) / 0 / 1 (a > b)
|
|
253
|
+
function bi_cmp(BigInt a, BigInt b) -> int {
|
|
254
|
+
int sa = bi_sign(a)
|
|
255
|
+
int sb = bi_sign(b)
|
|
256
|
+
if (sa == 0 && sb == 0) { return 0 }
|
|
257
|
+
if (sa != sb) {
|
|
258
|
+
if (sa < sb) { return -1 }
|
|
259
|
+
return 1
|
|
260
|
+
}
|
|
261
|
+
int m = bi_cmp_mag(a, b)
|
|
262
|
+
if (sa < 0) { return 0 - m }
|
|
263
|
+
return m
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ---------------- 加减核心 (无堆分配) ----------------
|
|
267
|
+
|
|
268
|
+
// out = |a| + |b|; out 必须与 a、b 为不同的结构体
|
|
269
|
+
// (只写 [0, n) 区间并显式设置 n, 因此不需要预先清零整块 64 位)
|
|
270
|
+
function bi_add_mag_into(BigInt a, BigInt b, BigInt out) -> void {
|
|
271
|
+
int na = bi_digits(a)
|
|
272
|
+
int nb = bi_digits(b)
|
|
273
|
+
int n = na
|
|
274
|
+
if (nb > n) { n = nb }
|
|
275
|
+
int carry = 0
|
|
276
|
+
int i = 0
|
|
277
|
+
while (i < n) {
|
|
278
|
+
int t = carry
|
|
279
|
+
if (i < na) { t = t + a.d[i] }
|
|
280
|
+
if (i < nb) { t = t + b.d[i] }
|
|
281
|
+
out.d[i] = t % 10
|
|
282
|
+
carry = idiv(t, 10)
|
|
283
|
+
i = i + 1
|
|
284
|
+
}
|
|
285
|
+
if (carry > 0) {
|
|
286
|
+
if (n >= 64) {
|
|
287
|
+
bi_saturate(out, 1)
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
out.d[n] = carry
|
|
291
|
+
n = n + 1
|
|
292
|
+
}
|
|
293
|
+
out.n = n
|
|
294
|
+
out.neg = 0
|
|
295
|
+
bi_norm(out)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// out = |a| - |b|, 要求 |a| >= |b|; out 必须与 a、b 为不同的结构体
|
|
299
|
+
function bi_sub_mag_into(BigInt a, BigInt b, BigInt out) -> void {
|
|
300
|
+
int na = bi_digits(a)
|
|
301
|
+
int nb = bi_digits(b)
|
|
302
|
+
int borrow = 0
|
|
303
|
+
int i = 0
|
|
304
|
+
while (i < na) {
|
|
305
|
+
int t = a.d[i] - borrow
|
|
306
|
+
if (i < nb) { t = t - b.d[i] }
|
|
307
|
+
if (t < 0) {
|
|
308
|
+
t = t + 10
|
|
309
|
+
borrow = 1
|
|
310
|
+
} else {
|
|
311
|
+
borrow = 0
|
|
312
|
+
}
|
|
313
|
+
out.d[i] = t
|
|
314
|
+
i = i + 1
|
|
315
|
+
}
|
|
316
|
+
out.n = na
|
|
317
|
+
out.neg = 0
|
|
318
|
+
bi_norm(out)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// out = a + b (符号规则与数学一致); out 必须与 a、b 为不同的结构体
|
|
322
|
+
function bi_add_into(BigInt a, BigInt b, BigInt out) -> void {
|
|
323
|
+
int sa = bi_sign(a)
|
|
324
|
+
int sb = bi_sign(b)
|
|
325
|
+
if (sa == sb) {
|
|
326
|
+
bi_add_mag_into(a, b, out)
|
|
327
|
+
if (sa < 0 && bi_is_zero(out) == 0) { out.neg = 1 }
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
int c = bi_cmp_mag(a, b)
|
|
331
|
+
if (c == 0) {
|
|
332
|
+
out.d[0] = 0
|
|
333
|
+
out.n = 1
|
|
334
|
+
out.neg = 0
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
if (c > 0) {
|
|
338
|
+
bi_sub_mag_into(a, b, out)
|
|
339
|
+
if (sa < 0) { out.neg = 1 }
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
bi_sub_mag_into(b, a, out)
|
|
343
|
+
if (sb < 0) { out.neg = 1 }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// out = a - b; out 必须与 a、b 为不同的结构体
|
|
347
|
+
function bi_sub_into(BigInt a, BigInt b, BigInt out) -> void {
|
|
348
|
+
int sa = bi_sign(a)
|
|
349
|
+
int sb = bi_sign(b)
|
|
350
|
+
if (sa != sb) {
|
|
351
|
+
bi_add_mag_into(a, b, out)
|
|
352
|
+
// a - b 的符号: a 非零时取 a 的符号, a 为零时取 b 的反号
|
|
353
|
+
if (sa == 0) {
|
|
354
|
+
if (sb > 0) { out.neg = 1 }
|
|
355
|
+
} else if (sa < 0) {
|
|
356
|
+
out.neg = 1
|
|
357
|
+
}
|
|
358
|
+
return
|
|
359
|
+
}
|
|
360
|
+
int c = bi_cmp_mag(a, b)
|
|
361
|
+
if (c == 0) {
|
|
362
|
+
out.d[0] = 0
|
|
363
|
+
out.n = 1
|
|
364
|
+
out.neg = 0
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
if (c > 0) {
|
|
368
|
+
bi_sub_mag_into(a, b, out)
|
|
369
|
+
if (sa < 0) { out.neg = 1 }
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
bi_sub_mag_into(b, a, out)
|
|
373
|
+
// 同号且 |b| > |a| 时结果符号与 a 相反 (a 为正则结果为负)
|
|
374
|
+
if (sa > 0) { out.neg = 1 }
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// out = -a
|
|
378
|
+
function bi_neg_into(BigInt a, BigInt out) -> void {
|
|
379
|
+
bi_copy(out, a)
|
|
380
|
+
if (bi_is_zero(out) == 0) {
|
|
381
|
+
if (out.neg == 0) {
|
|
382
|
+
out.neg = 1
|
|
383
|
+
} else {
|
|
384
|
+
out.neg = 0
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// out = |a|
|
|
390
|
+
function bi_abs_into(BigInt a, BigInt out) -> void {
|
|
391
|
+
bi_copy(out, a)
|
|
392
|
+
out.neg = 0
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// ---------------- 乘法核心 (无堆分配) ----------------
|
|
396
|
+
|
|
397
|
+
// out = a * b, 逐位相乘 + 进位 (不依赖原生 64 位乘法);
|
|
398
|
+
// out 必须与 a、b 为不同的结构体
|
|
399
|
+
function bi_mul_into(BigInt a, BigInt b, BigInt out) -> void {
|
|
400
|
+
int sa = bi_sign(a)
|
|
401
|
+
int sb = bi_sign(b)
|
|
402
|
+
int na = a.n
|
|
403
|
+
int nb = b.n
|
|
404
|
+
if (sa == 0 || sb == 0) {
|
|
405
|
+
out.d[0] = 0
|
|
406
|
+
out.n = 1
|
|
407
|
+
out.neg = 0
|
|
408
|
+
return
|
|
409
|
+
}
|
|
410
|
+
if (na + nb - 1 > 64) {
|
|
411
|
+
bi_saturate(out, sa * sb)
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
// 只清零累加区间 [0, na+nb), 不必清整块 64 位
|
|
415
|
+
int lim = na + nb
|
|
416
|
+
if (lim > 64) { lim = 64 }
|
|
417
|
+
int z = 0
|
|
418
|
+
while (z < lim) {
|
|
419
|
+
out.d[z] = 0
|
|
420
|
+
z = z + 1
|
|
421
|
+
}
|
|
422
|
+
int i = 0
|
|
423
|
+
while (i < na) {
|
|
424
|
+
int carry = 0
|
|
425
|
+
int j = 0
|
|
426
|
+
while (j < nb) {
|
|
427
|
+
int t = out.d[i + j] + a.d[i] * b.d[j] + carry
|
|
428
|
+
out.d[i + j] = t % 10
|
|
429
|
+
carry = idiv(t, 10)
|
|
430
|
+
j = j + 1
|
|
431
|
+
}
|
|
432
|
+
int k = i + nb
|
|
433
|
+
while (carry > 0) {
|
|
434
|
+
if (k >= 64) {
|
|
435
|
+
bi_saturate(out, sa * sb)
|
|
436
|
+
return
|
|
437
|
+
}
|
|
438
|
+
int t2 = out.d[k] + carry
|
|
439
|
+
out.d[k] = t2 % 10
|
|
440
|
+
carry = idiv(t2, 10)
|
|
441
|
+
k = k + 1
|
|
442
|
+
}
|
|
443
|
+
i = i + 1
|
|
444
|
+
}
|
|
445
|
+
out.n = 64
|
|
446
|
+
out.neg = 0
|
|
447
|
+
bi_norm(out)
|
|
448
|
+
if (bi_is_zero(out) == 0 && sa * sb < 0) { out.neg = 1 }
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// out = a * k, k 为 64 位整数; 要求 |k| <= 10^17 (更大请用 bi_mul_into);
|
|
452
|
+
// out 必须与 a 为不同的结构体
|
|
453
|
+
function bi_mul_small_into(BigInt a, int k, BigInt out) -> void {
|
|
454
|
+
int m = k
|
|
455
|
+
int sgn = 1
|
|
456
|
+
if (m < 0) {
|
|
457
|
+
m = 0 - m
|
|
458
|
+
sgn = -1
|
|
459
|
+
}
|
|
460
|
+
int sa = bi_sign(a)
|
|
461
|
+
int na = a.n
|
|
462
|
+
if (m == 0 || sa == 0) {
|
|
463
|
+
out.d[0] = 0
|
|
464
|
+
out.n = 1
|
|
465
|
+
out.neg = 0
|
|
466
|
+
return
|
|
467
|
+
}
|
|
468
|
+
int carry = 0
|
|
469
|
+
int i = 0
|
|
470
|
+
while (i < na) {
|
|
471
|
+
int t = a.d[i] * m + carry
|
|
472
|
+
out.d[i] = t % 10
|
|
473
|
+
carry = idiv(t, 10)
|
|
474
|
+
i = i + 1
|
|
475
|
+
}
|
|
476
|
+
int p = na
|
|
477
|
+
while (carry > 0) {
|
|
478
|
+
if (p >= 64) {
|
|
479
|
+
bi_saturate(out, sa * sgn)
|
|
480
|
+
return
|
|
481
|
+
}
|
|
482
|
+
out.d[p] = carry % 10
|
|
483
|
+
carry = idiv(carry, 10)
|
|
484
|
+
p = p + 1
|
|
485
|
+
}
|
|
486
|
+
out.n = p
|
|
487
|
+
out.neg = 0
|
|
488
|
+
bi_norm(out)
|
|
489
|
+
if (bi_is_zero(out) == 0 && sa * sgn < 0) { out.neg = 1 }
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// ---------------- 返回值语义包装 ----------------
|
|
493
|
+
|
|
494
|
+
// 绝对值
|
|
495
|
+
function bi_abs(BigInt a) -> BigInt {
|
|
496
|
+
BigInt r
|
|
497
|
+
bi_abs_into(a, r)
|
|
498
|
+
return r
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// 相反数 (零的相反数仍为 0)
|
|
502
|
+
function bi_neg(BigInt a) -> BigInt {
|
|
503
|
+
BigInt r
|
|
504
|
+
bi_neg_into(a, r)
|
|
505
|
+
return r
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// a + b
|
|
509
|
+
function bi_add(BigInt a, BigInt b) -> BigInt {
|
|
510
|
+
BigInt r
|
|
511
|
+
bi_add_into(a, b, r)
|
|
512
|
+
return r
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// a - b
|
|
516
|
+
function bi_sub(BigInt a, BigInt b) -> BigInt {
|
|
517
|
+
BigInt r
|
|
518
|
+
bi_sub_into(a, b, r)
|
|
519
|
+
return r
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// a * b (逐位相乘 + 进位)
|
|
523
|
+
function bi_mul(BigInt a, BigInt b) -> BigInt {
|
|
524
|
+
BigInt r
|
|
525
|
+
bi_mul_into(a, b, r)
|
|
526
|
+
return r
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// a * k
|
|
530
|
+
function bi_mul_small(BigInt a, int k) -> BigInt {
|
|
531
|
+
BigInt r
|
|
532
|
+
bi_mul_small_into(a, k, r)
|
|
533
|
+
return r
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// a 的 e 次幂 (快速幂); e < 0 视为 0, 即返回 1
|
|
537
|
+
function bi_pow(BigInt a, int e) -> BigInt {
|
|
538
|
+
BigInt base
|
|
539
|
+
BigInt acc
|
|
540
|
+
BigInt tmp
|
|
541
|
+
int k = e
|
|
542
|
+
int neg = 0
|
|
543
|
+
if (k < 0) { k = 0 }
|
|
544
|
+
bi_abs_into(a, base)
|
|
545
|
+
if (a.neg != 0 && k % 2 == 1) { neg = 1 }
|
|
546
|
+
acc.d[0] = 1
|
|
547
|
+
acc.n = 1
|
|
548
|
+
acc.neg = 0
|
|
549
|
+
while (k > 0) {
|
|
550
|
+
if (k % 2 == 1) {
|
|
551
|
+
bi_mul_into(acc, base, tmp)
|
|
552
|
+
bi_copy(acc, tmp)
|
|
553
|
+
if (bi_is_saturated(acc) == 1 && k > 1) {
|
|
554
|
+
bi_saturate(acc, 1)
|
|
555
|
+
if (neg == 1) { acc.neg = 1 }
|
|
556
|
+
return acc
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
k = idiv(k, 2)
|
|
560
|
+
if (k > 0) {
|
|
561
|
+
bi_mul_into(base, base, tmp)
|
|
562
|
+
bi_copy(base, tmp)
|
|
563
|
+
if (bi_is_saturated(base) == 1) {
|
|
564
|
+
bi_saturate(acc, 1)
|
|
565
|
+
if (neg == 1) { acc.neg = 1 }
|
|
566
|
+
return acc
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
if (neg == 1 && bi_is_zero(acc) == 0) { acc.neg = 1 }
|
|
571
|
+
return acc
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// n 的阶乘 (n < 0 视为 0! = 1); 超过容量时饱和并置溢出标志
|
|
575
|
+
function bi_fact(int n) -> BigInt {
|
|
576
|
+
BigInt acc
|
|
577
|
+
BigInt tmp
|
|
578
|
+
acc.d[0] = 1
|
|
579
|
+
acc.n = 1
|
|
580
|
+
acc.neg = 0
|
|
581
|
+
int i = 2
|
|
582
|
+
while (i <= n) {
|
|
583
|
+
bi_mul_small_into(acc, i, tmp)
|
|
584
|
+
bi_copy(acc, tmp)
|
|
585
|
+
if (bi_is_saturated(acc) == 1) { break }
|
|
586
|
+
i = i + 1
|
|
587
|
+
}
|
|
588
|
+
return acc
|
|
589
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "bits"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 位运算"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/bits.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "bits"]
|