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.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,143 @@
1
+ // Code CIN 官方标准库: 位运算 (bits)
2
+ // 用法: import "bits.cin"
3
+ //
4
+ // 说明: CIN 的 >> 是算术右移 (ASR), 对负数会补 1, 因此本库所有涉及负数
5
+ // 的位移都先做符号位/掩码处理, 保证结果与 64 位无符号语义一致。
6
+
7
+ // 64 位全 1 掩码
8
+ function bits_mask() -> int {
9
+ return 0xFFFFFFFFFFFFFFFF
10
+ }
11
+
12
+ // 二进制中 1 的个数 (popcount, 含符号位)
13
+ function bits_popcount(int x) -> int {
14
+ int n = 0
15
+ if (x < 0) { n = 1 }
16
+ int v = x & 0x7FFFFFFFFFFFFFFF
17
+ while (v != 0) {
18
+ n = n + (v & 1)
19
+ v = v >> 1
20
+ }
21
+ return n
22
+ }
23
+
24
+ // 前导零个数 (按 64 位计); x == 0 返回 64
25
+ function bits_clz(int x) -> int {
26
+ if (x < 0) { return 0 }
27
+ if (x == 0) { return 64 }
28
+ int n = 0
29
+ int bit = 0x4000000000000000
30
+ while (bit > 0 && (x & bit) == 0) {
31
+ n = n + 1
32
+ bit = bit >> 1
33
+ }
34
+ return n + 1
35
+ }
36
+
37
+ // 末尾零个数; x == 0 返回 64
38
+ function bits_ctz(int x) -> int {
39
+ if (x == 0) { return 64 }
40
+ int n = 0
41
+ int v = x
42
+ while ((v & 1) == 0) {
43
+ n = n + 1
44
+ v = v >> 1
45
+ }
46
+ return n
47
+ }
48
+
49
+ // 是否为 2 的幂 (仅正数)
50
+ function bits_is_pow2(int x) -> int {
51
+ if (x <= 0) { return 0 }
52
+ if ((x & (x - 1)) == 0) { return 1 }
53
+ return 0
54
+ }
55
+
56
+ // 不小于 x 的最小 2 的幂 (x <= 1 返回 1)
57
+ function bits_next_pow2(int x) -> int {
58
+ int p = 1
59
+ while (p < x && p > 0) {
60
+ p = p << 1
61
+ }
62
+ return p
63
+ }
64
+
65
+ // 读取第 i 位 (0..63)
66
+ function bits_test(int x, int i) -> int {
67
+ if (i < 0 || i > 63) { return 0 }
68
+ return (x >> i) & 1
69
+ }
70
+
71
+ // 置位 / 清位 / 取反某一位
72
+ function bits_set(int x, int i) -> int {
73
+ if (i < 0 || i > 63) { return x }
74
+ return x | (1 << i)
75
+ }
76
+
77
+ function bits_clear(int x, int i) -> int {
78
+ if (i < 0 || i > 63) { return x }
79
+ return x & ~(1 << i)
80
+ }
81
+
82
+ function bits_toggle(int x, int i) -> int {
83
+ if (i < 0 || i > 63) { return x }
84
+ return x ^ (1 << i)
85
+ }
86
+
87
+ // 循环左移 (n 自动对 64 取模)
88
+ function bits_rotl(int x, int n) -> int {
89
+ int k = n % 64
90
+ if (k < 0) { k = k + 64 }
91
+ if (k == 0) { return x }
92
+ int hi = (x >> (64 - k)) & ((1 << k) - 1)
93
+ return (x << k) | hi
94
+ }
95
+
96
+ // 循环右移
97
+ function bits_rotr(int x, int n) -> int {
98
+ int k = n % 64
99
+ if (k < 0) { k = k + 64 }
100
+ if (k == 0) { return x }
101
+ return bits_rotl(x, 64 - k)
102
+ }
103
+
104
+ // 64 位位序反转
105
+ function bits_reverse(int x) -> int {
106
+ int r = 0
107
+ for (int i = 0; i < 64; i = i + 1) {
108
+ r = r << 1
109
+ if (((x >> i) & 1) == 1) { r = r | 1 }
110
+ }
111
+ return r
112
+ }
113
+
114
+ // 区间掩码 [lo, hi] (0 <= lo <= hi <= 63)
115
+ function bits_range_mask(int lo, int hi) -> int {
116
+ if (lo < 0 || hi > 63 || hi < lo) { return 0 }
117
+ return ((1 << (hi - lo + 1)) - 1) << lo
118
+ }
119
+
120
+ // 取出区间 [lo, hi] 并右移到最低位
121
+ function bits_extract(int x, int lo, int hi) -> int {
122
+ if (lo < 0 || hi > 63 || hi < lo) { return 0 }
123
+ return (x >> lo) & ((1 << (hi - lo + 1)) - 1)
124
+ }
125
+
126
+ // 把区间 [lo, hi] 替换为 v 的低位
127
+ function bits_insert(int x, int lo, int hi, int v) -> int {
128
+ if (lo < 0 || hi > 63 || hi < lo) { return x }
129
+ int width = hi - lo + 1
130
+ int clear = ~bits_range_mask(lo, hi)
131
+ int bits = (v & ((1 << width) - 1)) << lo
132
+ return (x & clear) | bits
133
+ }
134
+
135
+ // 字节序翻转 (bswap64)
136
+ function bits_bswap(int x) -> int {
137
+ int r = 0
138
+ for (int i = 0; i < 8; i = i + 1) {
139
+ r = r << 8
140
+ r = r | ((x >> (i * 8)) & 0xFF)
141
+ }
142
+ return r
143
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "bitset"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 多字位集合"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/bitset.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "bitset"]
@@ -0,0 +1,374 @@
1
+ // Code CIN 官方标准库: 多字位集合 (bitset)
2
+ // 用法: import "bitset.cin"
3
+ //
4
+ // 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
5
+ // 位集合以定长全局数组 bs_data[16] 保存 (2 个槽 x 8 个 64 位字)。
6
+ // 与 bits.cin 的单字 (64 位) 运算互补: 本库把一整套 512 位的位集合
7
+ // 放在全局状态里, 提供置位 / 计数 / 查找 / 集合运算 / 整体位移。
8
+ //
9
+ // 容量常量:
10
+ // BS_WORDS = 8 (每个位集合 8 个 64 位字)
11
+ // BS_BITS = 512 (每个位集合 512 位, 合法下标 0..511)
12
+ // BS_SLOTS = 2 (槽号 0 / 1)
13
+ //
14
+ // 全局数据段占用 (每槽 8 字节):
15
+ // bs_data[16] = 16 槽
16
+ // 标量 (bs_cur) = 1 槽
17
+ // 合计 17 槽 = 136 字节, 远小于默认 64 KiB 数据段。
18
+ //
19
+ // 槽位模型 (重要):
20
+ // * 库内有两个互相独立的位集合槽: 槽 0 与槽 1;
21
+ // * bs_cur 记录"当前槽", 所有单槽操作 (bs_set / bs_count / bs_not /
22
+ // bs_shift_left / bs_from_int / ...) 都只作用于当前槽;
23
+ // * bs_select(which) 把当前槽切换为 which (0 或 1);
24
+ // 初始当前槽为 0, bs_reset() 会把当前槽复位为 0;
25
+ // * bs_and / bs_or / bs_xor / bs_equals 的参数 other 是"另一操作数槽"的
26
+ // 槽号, 读的是那个槽; 三个逻辑运算把结果**写回当前槽**(原地), 另一个
27
+ // 槽保持不变; bs_equals 只比较, 不改动任何槽。
28
+ //
29
+ // 边界约定:
30
+ // * 位下标越界 (i < 0 或 i > 511): bs_test 返回 0; bs_set / bs_clear /
31
+ // bs_toggle 返回 0 且**不写内存**; bs_next_set 返回 -1;
32
+ // * 槽号越界 (不在 0..1): bs_select / bs_copy_from / bs_clear_slot /
33
+ // bs_and / bs_or / bs_xor / bs_equals 返回 0 且不改变任何状态;
34
+ // * bs_count_word(w) 的 w 越界返回 -1 (合法的字内计数是 0..64);
35
+ // bs_get_word(w) 的 w 越界返回 0 (64 位字的取值本身就是任意整数,
36
+ // 没有无歧义的失败值); bs_set_word(w, v) 的 w 越界返回 0 无操作;
37
+ // * 位移量 k < 0 视为非法: 无操作; k == 0 无操作;
38
+ // k >= BS_BITS (512) 时当前槽整体清零;
39
+ // * bs_not() 严格按 BS_BITS = 512 位取反 (逐字与 64 位全 1 掩码异或,
40
+ // 不使用 `~` 直接取反 64 位字), 因此空集取反得到满集 (512 位全 1)。
41
+
42
+ int bs_data[16]
43
+ int bs_cur = 0
44
+
45
+ // ---------------- 常量 ----------------
46
+
47
+ // 总位数 BS_BITS = 512
48
+ function bs_bits() -> int {
49
+ return 512
50
+ }
51
+
52
+ // 每个位集合的字数 BS_WORDS = 8
53
+ function bs_words() -> int {
54
+ return 8
55
+ }
56
+
57
+ // 可用的槽数 BS_SLOTS = 2
58
+ function bs_slots() -> int {
59
+ return 2
60
+ }
61
+
62
+ // 内部辅助: 槽号是否合法 (0 或 1)
63
+ function bs_valid(int which) -> int {
64
+ if (which < 0 || which > 1) { return 0 }
65
+ return 1
66
+ }
67
+
68
+ // 内部辅助: 一个 64 位字的 popcount (含符号位)
69
+ function bs_popcount_word(int x) -> int {
70
+ int n = 0
71
+ int v = x
72
+ while (v != 0) {
73
+ n = n + (v & 1)
74
+ v = (v >> 1) & 0x7FFFFFFFFFFFFFFF
75
+ }
76
+ return n
77
+ }
78
+
79
+ // 内部辅助: 一个非零 64 位字的最低置位下标 (0..63); x == 0 时行为未定义
80
+ function bs_ctz_word(int x) -> int {
81
+ int n = 0
82
+ int v = x
83
+ while ((v & 1) == 0) {
84
+ n = n + 1
85
+ v = v >> 1
86
+ }
87
+ return n
88
+ }
89
+
90
+ // 内部辅助: 一个非零 64 位字的最高置位下标 (0..63); x == 0 时返回 -1
91
+ function bs_high_bit(int x) -> int {
92
+ int b = 63
93
+ while (b >= 0 && ((x >> b) & 1) == 0) {
94
+ b = b - 1
95
+ }
96
+ return b
97
+ }
98
+
99
+ // ---------------- 槽管理 ----------------
100
+
101
+ // 把当前槽切换为 which; 1 成功, 0 表示槽号非法 (当前槽不变)
102
+ function bs_select(int which) -> int {
103
+ if (bs_valid(which) == 0) { return 0 }
104
+ bs_cur = which
105
+ return 1
106
+ }
107
+
108
+ // 当前槽号 (0 或 1)
109
+ function bs_selected() -> int {
110
+ return bs_cur
111
+ }
112
+
113
+ // 清空两个槽, 并把当前槽复位为 0 (库初始化用)
114
+ function bs_reset() -> void {
115
+ for (int w = 0; w < 16; w = w + 1) {
116
+ bs_data[w] = 0
117
+ }
118
+ bs_cur = 0
119
+ }
120
+
121
+ // 清空当前槽的全部 512 位
122
+ function bs_clear_all() -> void {
123
+ for (int w = 0; w < 8; w = w + 1) {
124
+ bs_data[bs_cur * 8 + w] = 0
125
+ }
126
+ }
127
+
128
+ // 清空指定槽 which 的全部 512 位; 1 成功, 0 表示槽号非法
129
+ function bs_clear_slot(int which) -> int {
130
+ if (bs_valid(which) == 0) { return 0 }
131
+ for (int w = 0; w < 8; w = w + 1) {
132
+ bs_data[which * 8 + w] = 0
133
+ }
134
+ return 1
135
+ }
136
+
137
+ // 把槽 src 整体复制到当前槽; 1 成功, 0 表示槽号非法
138
+ function bs_copy_from(int src) -> int {
139
+ if (bs_valid(src) == 0) { return 0 }
140
+ for (int w = 0; w < 8; w = w + 1) {
141
+ bs_data[bs_cur * 8 + w] = bs_data[src * 8 + w]
142
+ }
143
+ return 1
144
+ }
145
+
146
+ // ---------------- 单字访问 ----------------
147
+
148
+ // 读取当前槽第 w 个 64 位字; w 越界返回 0
149
+ function bs_get_word(int w) -> int {
150
+ if (w < 0 || w > 7) { return 0 }
151
+ return bs_data[bs_cur * 8 + w]
152
+ }
153
+
154
+ // 写入当前槽第 w 个 64 位字; 1 成功, 0 表示 w 越界 (无操作)
155
+ function bs_set_word(int w, int v) -> int {
156
+ if (w < 0 || w > 7) { return 0 }
157
+ bs_data[bs_cur * 8 + w] = v
158
+ return 1
159
+ }
160
+
161
+ // 当前槽第 w 个字里 1 的个数; w 越界返回 -1
162
+ function bs_count_word(int w) -> int {
163
+ if (w < 0 || w > 7) { return -1 }
164
+ return bs_popcount_word(bs_data[bs_cur * 8 + w])
165
+ }
166
+
167
+ // ---------------- 单位操作 ----------------
168
+
169
+ // 置位第 i 位; 1 成功, 0 表示 i 越界 (无操作)
170
+ function bs_set(int i) -> int {
171
+ if (i < 0 || i > 511) { return 0 }
172
+ int idx = bs_cur * 8 + idiv(i, 64)
173
+ bs_data[idx] = bs_data[idx] | (1 << (i % 64))
174
+ return 1
175
+ }
176
+
177
+ // 清位第 i 位; 1 成功, 0 表示 i 越界 (无操作); 该位本来为 0 也返回 1
178
+ function bs_clear(int i) -> int {
179
+ if (i < 0 || i > 511) { return 0 }
180
+ int idx = bs_cur * 8 + idiv(i, 64)
181
+ int bit = 1 << (i % 64)
182
+ bs_data[idx] = bs_data[idx] & (bit ^ 0xFFFFFFFFFFFFFFFF)
183
+ return 1
184
+ }
185
+
186
+ // 反转第 i 位; 1 成功, 0 表示 i 越界 (无操作)
187
+ function bs_toggle(int i) -> int {
188
+ if (i < 0 || i > 511) { return 0 }
189
+ int idx = bs_cur * 8 + idiv(i, 64)
190
+ bs_data[idx] = bs_data[idx] ^ (1 << (i % 64))
191
+ return 1
192
+ }
193
+
194
+ // 测试第 i 位; 返回 0 / 1, i 越界返回 0
195
+ function bs_test(int i) -> int {
196
+ if (i < 0 || i > 511) { return 0 }
197
+ return (bs_data[bs_cur * 8 + idiv(i, 64)] >> (i % 64)) & 1
198
+ }
199
+
200
+ // 当前槽整 512 位取反 (原地); 结果仍然只占 BS_BITS 位
201
+ function bs_not() -> void {
202
+ for (int w = 0; w < 8; w = w + 1) {
203
+ int i = bs_cur * 8 + w
204
+ bs_data[i] = bs_data[i] ^ 0xFFFFFFFFFFFFFFFF
205
+ }
206
+ }
207
+
208
+ // ---------------- 查询 ----------------
209
+
210
+ // 当前槽中 1 的总个数 (0..512)
211
+ function bs_count() -> int {
212
+ int n = 0
213
+ for (int w = 0; w < 8; w = w + 1) {
214
+ n = n + bs_popcount_word(bs_data[bs_cur * 8 + w])
215
+ }
216
+ return n
217
+ }
218
+
219
+ // 是否至少有一位为 1; 1/0
220
+ function bs_any() -> int {
221
+ for (int w = 0; w < 8; w = w + 1) {
222
+ if (bs_data[bs_cur * 8 + w] != 0) { return 1 }
223
+ }
224
+ return 0
225
+ }
226
+
227
+ // 是否一位都没有 (空集); 1/0
228
+ function bs_none() -> int {
229
+ if (bs_any() == 1) { return 0 }
230
+ return 1
231
+ }
232
+
233
+ // 最小置位下标; 空集返回 -1
234
+ function bs_first_set() -> int {
235
+ for (int w = 0; w < 8; w = w + 1) {
236
+ int v = bs_data[bs_cur * 8 + w]
237
+ if (v != 0) { return w * 64 + bs_ctz_word(v) }
238
+ }
239
+ return -1
240
+ }
241
+
242
+ // 最大置位下标; 空集返回 -1
243
+ function bs_last_set() -> int {
244
+ int w = 7
245
+ while (w >= 0) {
246
+ int v = bs_data[bs_cur * 8 + w]
247
+ if (v != 0) { return w * 64 + bs_high_bit(v) }
248
+ w = w - 1
249
+ }
250
+ return -1
251
+ }
252
+
253
+ // 大于等于 from 的下一个置位下标; 没有则返回 -1; from 越界返回 -1
254
+ function bs_next_set(int from) -> int {
255
+ if (from < 0 || from > 511) { return -1 }
256
+ int i = from
257
+ while (i < 512) {
258
+ if (bs_test(i) == 1) { return i }
259
+ i = i + 1
260
+ }
261
+ return -1
262
+ }
263
+
264
+ // ---------------- 逻辑运算 (结果写回当前槽) ----------------
265
+
266
+ // 当前槽 &= 槽 other; 1 成功, 0 表示 other 非法 (无操作)
267
+ function bs_and(int other) -> int {
268
+ if (bs_valid(other) == 0) { return 0 }
269
+ for (int w = 0; w < 8; w = w + 1) {
270
+ bs_data[bs_cur * 8 + w] = bs_data[bs_cur * 8 + w] & bs_data[other * 8 + w]
271
+ }
272
+ return 1
273
+ }
274
+
275
+ // 当前槽 |= 槽 other; 1 成功, 0 表示 other 非法 (无操作)
276
+ function bs_or(int other) -> int {
277
+ if (bs_valid(other) == 0) { return 0 }
278
+ for (int w = 0; w < 8; w = w + 1) {
279
+ bs_data[bs_cur * 8 + w] = bs_data[bs_cur * 8 + w] | bs_data[other * 8 + w]
280
+ }
281
+ return 1
282
+ }
283
+
284
+ // 当前槽 ^= 槽 other; 1 成功, 0 表示 other 非法 (无操作)
285
+ function bs_xor(int other) -> int {
286
+ if (bs_valid(other) == 0) { return 0 }
287
+ for (int w = 0; w < 8; w = w + 1) {
288
+ bs_data[bs_cur * 8 + w] = bs_data[bs_cur * 8 + w] ^ bs_data[other * 8 + w]
289
+ }
290
+ return 1
291
+ }
292
+
293
+ // 当前槽是否与槽 other 完全相同; 1/0; other 非法返回 0
294
+ function bs_equals(int other) -> int {
295
+ if (bs_valid(other) == 0) { return 0 }
296
+ for (int w = 0; w < 8; w = w + 1) {
297
+ if (bs_data[bs_cur * 8 + w] != bs_data[other * 8 + w]) { return 0 }
298
+ }
299
+ return 1
300
+ }
301
+
302
+ // ---------------- 整体位移 ----------------
303
+
304
+ // 当前槽整体左移 k 位 (下标 i -> i + k), 移出 511 的位丢弃;
305
+ // k < 0 或 k == 0 无操作; k >= 512 时当前槽清零
306
+ function bs_shift_left(int k) -> void {
307
+ if (k < 0) { return }
308
+ if (k >= 512) {
309
+ bs_clear_all()
310
+ return
311
+ }
312
+ if (k == 0) { return }
313
+ int ws = idiv(k, 64)
314
+ int bits = k % 64
315
+ int w = 7
316
+ while (w >= 0) {
317
+ int src = w - ws
318
+ int val = 0
319
+ if (src >= 0) {
320
+ val = bs_data[bs_cur * 8 + src] << bits
321
+ if (bits > 0 && src - 1 >= 0) {
322
+ int lo = (bs_data[bs_cur * 8 + src - 1] >> (64 - bits)) & ((1 << bits) - 1)
323
+ val = val | lo
324
+ }
325
+ }
326
+ bs_data[bs_cur * 8 + w] = val
327
+ w = w - 1
328
+ }
329
+ }
330
+
331
+ // 当前槽整体右移 k 位 (下标 i -> i - k), 移出 0 以下的位丢弃;
332
+ // k < 0 或 k == 0 无操作; k >= 512 时当前槽清零
333
+ function bs_shift_right(int k) -> void {
334
+ if (k < 0) { return }
335
+ if (k >= 512) {
336
+ bs_clear_all()
337
+ return
338
+ }
339
+ if (k == 0) { return }
340
+ int ws = idiv(k, 64)
341
+ int bits = k % 64
342
+ int w = 0
343
+ while (w < 8) {
344
+ int src = w + ws
345
+ int val = 0
346
+ if (src < 8) {
347
+ if (bits == 0) {
348
+ val = bs_data[bs_cur * 8 + src]
349
+ } else {
350
+ val = (bs_data[bs_cur * 8 + src] >> bits) & ((1 << (64 - bits)) - 1)
351
+ if (src + 1 < 8) {
352
+ val = val | (bs_data[bs_cur * 8 + src + 1] << (64 - bits))
353
+ }
354
+ }
355
+ }
356
+ bs_data[bs_cur * 8 + w] = val
357
+ w = w + 1
358
+ }
359
+ }
360
+
361
+ // ---------------- 与整数互转 (只涉及低 64 位) ----------------
362
+
363
+ // 当前槽 = v 的低 64 位 (其余 448 位清零)
364
+ function bs_from_int(int v) -> void {
365
+ bs_data[bs_cur * 8] = v
366
+ for (int w = 1; w < 8; w = w + 1) {
367
+ bs_data[bs_cur * 8 + w] = 0
368
+ }
369
+ }
370
+
371
+ // 当前槽的低 64 位 (第 0 个字); 高位被丢弃
372
+ function bs_to_int() -> int {
373
+ return bs_data[bs_cur * 8]
374
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "codec"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 编解码与简单密码"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/codec.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "codec"]