chore: 全仓同步 TPV→VPC 注释改名与 range(int(...)) 显式收窄适配

This commit is contained in:
TermiNexus committed 2026-10-12 20:59:35 +08:00
1 parent 44c1bc4168
commit 7f82bec5d1
38 files changed
+307 -321

No files matched your search

+3 -3
View File
@@ -58,7 +58,7 @@ class dict:
def _find(self, key: str) -> sizet: def _find(self, key: str) -> sizet:
"""Find index of target key; returns __count__ if not found""" """Find index of target key; returns __count__ if not found"""
for i in range(self.__count__): for i in range(int(self.__count__)):
# Element address: `T*(base + byte_offset)`. rawptr is i8* and `+` scales by 1 byte, byte-identical to the old uint64(base)+n form (no integer round-trip). # Element address: `T*(base + byte_offset)`. rawptr is i8* and `+` scales by 1 byte, byte-identical to the old uint64(base)+n form (no integer round-trip).
# Same form below (__getitem__/_set_variant_raw/get/get_vtype/__next__/to_json), not annotated again. # Same form below (__getitem__/_set_variant_raw/get/get_vtype/__next__/to_json), not annotated again.
key_ptr: str* = str*(self.__keys__ + i * 8) key_ptr: str* = str*(self.__keys__ + i * 8)
@@ -199,7 +199,7 @@ class dict:
"""Convert dict instance to JsonValue object (JSON_OBJECT). """Convert dict instance to JsonValue object (JSON_OBJECT).
Automatically select corresponding JSON value type according to Variant tag.""" Automatically select corresponding JSON value type according to Variant tag."""
obj: json.JsonValue* = json.object(pool) obj: json.JsonValue* = json.object(pool)
for i in range(self.__count__): for i in range(int(self.__count__)):
key_ptr: str* = str*(self.__keys__ + i * 8) key_ptr: str* = str*(self.__keys__ + i * 8)
vtype_ptr: int* = int*(self.__values__ + i * _VARIANT_SIZE) vtype_ptr: int* = int*(self.__values__ + i * _VARIANT_SIZE)
val_ptr: str* = str*(self.__values__ + i * _VARIANT_SIZE + 8) val_ptr: str* = str*(self.__values__ + i * _VARIANT_SIZE + 8)
@@ -243,7 +243,7 @@ class dict:
if cur.vtype == 2: # JSON_INT if cur.vtype == 2: # JSON_INT
storage: rawptr = self.__mbuddy__.alloc(8) storage: rawptr = self.__mbuddy__.alloc(8)
ip: int* = int*(storage) ip: int* = int*(storage)
ip[0] = cur.int_val ip[0] = int(cur.int_val)
self._set_variant_raw(cur.key, VARIANT_INT, storage) self._set_variant_raw(cur.key, VARIANT_INT, storage)
elif cur.vtype == 4: # JSON_STRING elif cur.vtype == 4: # JSON_STRING
self._set_variant_raw(cur.key, VARIANT_STR, cur.str_val) self._set_variant_raw(cur.key, VARIANT_STR, cur.str_val)
+6 -6
View File
@@ -191,7 +191,7 @@ class ArgumentParser:
def _find_long(self, token: str) -> int: def _find_long(self, token: str) -> int:
"""Exact match for long option name""" """Exact match for long option name"""
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg: Argument* = self._get_arg(i) arg: Argument* = self._get_arg(i)
if string.strcmp(arg.name, token) == 0: if string.strcmp(arg.name, token) == 0:
return i return i
@@ -199,7 +199,7 @@ class ArgumentParser:
def _find_long_prefix(self, token: str) -> int: def _find_long_prefix(self, token: str) -> int:
"""Match prefix for --name=value syntax""" """Match prefix for --name=value syntax"""
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg: Argument* = self._get_arg(i) arg: Argument* = self._get_arg(i)
name_len: int = int(string.strlen(arg.name)) # 显式 cast:避免 64→32 隐式窄化 name_len: int = int(string.strlen(arg.name)) # 显式 cast:避免 64→32 隐式窄化
if string.strncmp(token, arg.name, name_len) == 0: if string.strncmp(token, arg.name, name_len) == 0:
@@ -208,7 +208,7 @@ class ArgumentParser:
return -1 return -1
def _find_short(self, token: str) -> int: def _find_short(self, token: str) -> int:
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg: Argument* = self._get_arg(i) arg: Argument* = self._get_arg(i)
if arg.short_name is not None: if arg.short_name is not None:
if string.strcmp(arg.short_name, token) == 0: if string.strcmp(arg.short_name, token) == 0:
@@ -217,7 +217,7 @@ class ArgumentParser:
def _find_positional(self, pos_idx: int) -> int: def _find_positional(self, pos_idx: int) -> int:
count: int = 0 count: int = 0
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg: Argument* = self._get_arg(i) arg: Argument* = self._get_arg(i)
if arg.is_positional: if arg.is_positional:
if count == pos_idx: if count == pos_idx:
@@ -386,7 +386,7 @@ class ArgumentParser:
idx += 1 idx += 1
# Validate required arguments # Validate required arguments
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg = self._get_arg(i) arg = self._get_arg(i)
if arg.required: if arg.required:
if not args.has(arg.dest): if not args.has(arg.dest):
@@ -412,7 +412,7 @@ class ArgumentParser:
stdio.printf(" %-20s %s\n", arg.name, ht) stdio.printf(" %-20s %s\n", arg.name, ht)
i += 1 i += 1
stdio.printf("\noptional arguments:\n") stdio.printf("\noptional arguments:\n")
for i in range(self._arg_count): for i in range(int(self._arg_count)):
arg = self._get_arg(i) arg = self._get_arg(i)
if not arg.is_positional: if not arg.is_positional:
ht: str = arg.help_text ht: str = arg.help_text
+6 -6
View File
@@ -145,7 +145,7 @@ def _init_ast(node: AST*, pool: memhub.MemManager*, lineno: int, col: int):
def _set_parent_list(lst: list[AST*]*, parent: AST*): def _set_parent_list(lst: list[AST*]*, parent: AST*):
"""Set list children parent pointers.""" """Set list children parent pointers."""
if lst is None: return if lst is None: return
for i in range(lst.__len__()): for i in range(int(lst.__len__())):
child: AST* = lst.get(i) child: AST* = lst.get(i)
if child is not None: child.parent = parent if child is not None: child.parent = parent
@@ -216,7 +216,7 @@ def _emit(buf: str, size: sizet, pos: sizet, text: str) -> sizet:
if buf is None or pos >= size: return pos if buf is None or pos >= size: return pos
cur: str = str(uint64(buf) + pos) cur: str = str(uint64(buf) + pos)
rem: sizet = size - pos rem: sizet = size - pos
n: int = viperlib.snprintf(cur, rem, "%s", text) n: int = int(viperlib.snprintf(cur, rem, "%s", text))
if n < 0: return pos if n < 0: return pos
return pos + sizet(n) return pos + sizet(n)
@@ -226,7 +226,7 @@ def _emit_str(buf: str, size: sizet, pos: sizet, text: str) -> sizet:
if buf is None or pos >= size: return pos if buf is None or pos >= size: return pos
cur: str = str(uint64(buf) + pos) cur: str = str(uint64(buf) + pos)
rem: sizet = size - pos rem: sizet = size - pos
n: int = viperlib.snprintf(cur, rem, "%s", text) n: int = int(viperlib.snprintf(cur, rem, "%s", text))
if n < 0: return pos if n < 0: return pos
return pos + sizet(n) return pos + sizet(n)
@@ -236,7 +236,7 @@ def _emit_int(buf: str, size: sizet, pos: sizet, val: int64) -> sizet:
if buf is None or pos >= size: return pos if buf is None or pos >= size: return pos
cur: str = str(uint64(buf) + pos) cur: str = str(uint64(buf) + pos)
rem: sizet = size - pos rem: sizet = size - pos
n: int = viperlib.snprintf(cur, rem, "%lld", val) n: int = int(viperlib.snprintf(cur, rem, "%lld", val))
if n < 0: return pos if n < 0: return pos
return pos + sizet(n) return pos + sizet(n)
@@ -283,7 +283,7 @@ def _dump_list(lst: list[AST*]*, buf: str, size: sizet, pos: sizet) -> sizet:
pos = _emit(buf, size, pos, "[]") pos = _emit(buf, size, pos, "[]")
return pos return pos
pos = _emit(buf, size, pos, "[") pos = _emit(buf, size, pos, "[")
for i in range(lst.__len__()): for i in range(int(lst.__len__())):
if i > 0: pos = _emit(buf, size, pos, ", ") if i > 0: pos = _emit(buf, size, pos, ", ")
child: AST* = lst.get(i) child: AST* = lst.get(i)
if child is not None: pos = child.dump(buf, size, pos) if child is not None: pos = child.dump(buf, size, pos)
@@ -298,7 +298,7 @@ def _dump_op_list(lst: t.CPtr, buf: str, size: sizet, pos: sizet) -> sizet:
return pos return pos
ops_list: list[int]* = (list[int]*)(lst) ops_list: list[int]* = (list[int]*)(lst)
pos = _emit(buf, size, pos, "[") pos = _emit(buf, size, pos, "[")
for i in range(ops_list.__len__()): for i in range(int(ops_list.__len__())):
if i > 0: pos = _emit(buf, size, pos, ", ") if i > 0: pos = _emit(buf, size, pos, ", ")
op: int = ops_list.get(i) op: int = ops_list.get(i)
pos = _emit(buf, size, pos, _op_name(op)) pos = _emit(buf, size, pos, _op_name(op))
+2 -2
View File
@@ -24,7 +24,7 @@ def b64encode(s: str, out: str) -> sizet:
j: sizet = 0 j: sizet = 0
val: uint32 = 0 val: uint32 = 0
# Process input in groups of 3 bytes, produce 4 Base64 characters # Process input in groups of 3 bytes, produce 4 Base64 characters
for i in range(0, length, 3): for i in range(0, int(length), 3):
# Assemble 24-bit chunk # Assemble 24-bit chunk
val = ((uint32(_s[i + 0]) << 16 ) | val = ((uint32(_s[i + 0]) << 16 ) |
(uint32(_s[i + 1]) << 8 if (i + 1 < length) else 0) | (uint32(_s[i + 1]) << 8 if (i + 1 < length) else 0) |
@@ -50,7 +50,7 @@ def b64decode(s: str, out: uint8*) -> sizet:
val: uint32 = 0 val: uint32 = 0
c: int c: int
# Restore 3 bytes from every 4 Base64 characters # Restore 3 bytes from every 4 Base64 characters
for i in range(0, length, 4): for i in range(0, int(length), 4):
if s[i] == '=': if s[i] == '=':
break break
val = 0 val = 0
+8 -8
View File
@@ -109,13 +109,13 @@ def init_binascii():
def crc32(data: str, length: int, crc: uint) -> uint: def crc32(data: str, length: int, crc: uint) -> uint:
s: uint = crc ^ 0xFFFFFFFF s: uint = crc ^ 0xFFFFFFFF
for i in range(length): for i in range(int(length)):
s = CRC32_TABLE[(s ^ uchar(data[i])) & 0xFF] ^ (s >> 8) s = CRC32_TABLE[(s ^ uchar(data[i])) & 0xFF] ^ (s >> 8)
return s ^ 0xFFFFFFFF return s ^ 0xFFFFFFFF
def crc_hqx(data: str, length: int, crc: ushort) -> ushort: def crc_hqx(data: str, length: int, crc: ushort) -> ushort:
s: ushort = crc s: ushort = crc
for i in range(length): for i in range(int(length)):
s = CRC_HQX_TABLE[uchar(s >> 8)] ^ (s << 1 | (data[i] & 0xFF)) s = CRC_HQX_TABLE[uchar(s >> 8)] ^ (s << 1 | (data[i] & 0xFF))
return s return s
@@ -124,7 +124,7 @@ def hexlify(data: str, length: int) -> str:
out: str = str(stdlib.malloc(length * 2 + 1)) out: str = str(stdlib.malloc(length * 2 + 1))
if not out: return None if not out: return None
oi: int = 0 oi: int = 0
for i in range(length): for i in range(int(length)):
s: uchar = uchar(data[i]) s: uchar = uchar(data[i])
out[oi + 0] = HEX_CHARS[(s >> 4) & 0x0F] out[oi + 0] = HEX_CHARS[(s >> 4) & 0x0F]
out[oi + 1] = HEX_CHARS[(s >> 0) & 0x0F] out[oi + 1] = HEX_CHARS[(s >> 0) & 0x0F]
@@ -141,7 +141,7 @@ def a2b_hex(hexstr: str, length: int) -> str:
out: str = str(stdlib.malloc(out_len + 1)) out: str = str(stdlib.malloc(out_len + 1))
if not out: return None if not out: return None
oi: int = 0 oi: int = 0
for i in range(0, length, 2): for i in range(0, int(length), 2):
hi: int = HEX_VALS[uchar(hexstr[i])] hi: int = HEX_VALS[uchar(hexstr[i])]
lo: int = HEX_VALS[uchar(hexstr[i + 1])] lo: int = HEX_VALS[uchar(hexstr[i + 1])]
if hi < 0 or lo < 0: if hi < 0 or lo < 0:
@@ -164,7 +164,7 @@ def b2a_base64(data: str, length: int) -> str:
out: str = str(stdlib.malloc(olen + 1)) out: str = str(stdlib.malloc(olen + 1))
if not out: return None if not out: return None
oi: int = 0 oi: int = 0
for i in range(0, length, 3): for i in range(0, int(length), 3):
b0: uchar = uchar(data[i]) b0: uchar = uchar(data[i])
b1: uchar = 0 b1: uchar = 0
b2: uchar = 0 b2: uchar = 0
@@ -199,7 +199,7 @@ def a2b_base64(data: str, length: int) -> str:
out: str = str(stdlib.malloc(olen + 1)) out: str = str(stdlib.malloc(olen + 1))
if not out: return None if not out: return None
oi: int = 0 oi: int = 0
for i in range(0, length, 4): for i in range(0, int(length), 4):
v0: int = 0 v0: int = 0
v1: int = 0 v1: int = 0
v2: int = 0 v2: int = 0
@@ -236,7 +236,7 @@ def b2a_uu(data: str, length: int) -> str:
oi: int = 0 oi: int = 0
out[oi] = UU_CHARS[length] out[oi] = UU_CHARS[length]
oi += 1 oi += 1
for i in range(0, length, 3): for i in range(0, int(length), 3):
b0: uchar = uchar(data[i]) b0: uchar = uchar(data[i])
b1: uchar = 0 b1: uchar = 0
b2: uchar = 0 b2: uchar = 0
@@ -339,7 +339,7 @@ def a2b_hqx(data: str, crc: ushort = 0) -> tuple[str, ushort]:
def adler32(data: str, length: int, adler: uint) -> uint: def adler32(data: str, length: int, adler: uint) -> uint:
a: uint = adler & 0xFFFF a: uint = adler & 0xFFFF
b: uint = (adler >> 16) & 0xFFFF b: uint = (adler >> 16) & 0xFFFF
for i in range(length): for i in range(int(length)):
a = (a + uchar(data[i])) % 65521 a = (a + uchar(data[i])) % 65521
b = (b + a) % 65521 b = (b + a) % 65521
return (b << 16) | a return (b << 16) | a
+6 -6
View File
@@ -172,7 +172,7 @@ def _fill(n: int) -> int:
def _bits(n: int) -> int: def _bits(n: int) -> int:
global _g_bits, _g_bitcnt global _g_bits, _g_bitcnt
if _fill(n) != 0: if _fill(int(n)) != 0:
return -1 return -1
v: uint64 = _g_bits & ((uint64(1) << n) - 1) v: uint64 = _g_bits & ((uint64(1) << n) - 1)
_g_bits = _g_bits >> n _g_bits = _g_bits >> n
@@ -244,7 +244,7 @@ def _collect(len_arr: int*, cnt: int*,
sorted_arr: int*, nsym: int, sorted_arr: int*, nsym: int,
pos: int*): pos: int*):
string.memset(cnt, 0, 17 * 4) string.memset(cnt, 0, 17 * 4)
for s1 in range(nsym): for s1 in range(int(nsym)):
if len_arr[s1] != 0: if len_arr[s1] != 0:
cnt[len_arr[s1]] += 1 cnt[len_arr[s1]] += 1
run: int = 0 run: int = 0
@@ -253,7 +253,7 @@ def _collect(len_arr: int*, cnt: int*,
pos[l5] = run pos[l5] = run
run += cnt[l5] run += cnt[l5]
l5 += 1 l5 += 1
for s2 in range(nsym): for s2 in range(int(nsym)):
if len_arr[s2] != 0: if len_arr[s2] != 0:
sorted_arr[pos[len_arr[s2]]] = s2 sorted_arr[pos[len_arr[s2]]] = s2
pos[len_arr[s2]] += 1 pos[len_arr[s2]] += 1
@@ -429,7 +429,7 @@ def _gzip_decode(src: rawptr, src_len: sizet,
if (slen ^ snlen) != 0xFFFF: if (slen ^ snlen) != 0xFFFF:
err = -7 err = -7
break break
for i in range(sizet(slen)): for i in range(int(sizet(slen))):
ch: int = _byte() ch: int = _byte()
if ch < 0: if ch < 0:
err = -8 err = -8
@@ -468,7 +468,7 @@ def _gzip_decode(src: rawptr, src_len: sizet,
err = -11 err = -11
break break
string.memset(cl_len, 0, 19 * 4) string.memset(cl_len, 0, 19 * 4)
for k in range(hclen): for k in range(int(hclen)):
vv: int = _bits(3) vv: int = _bits(3)
if vv < 0: if vv < 0:
err = -12 err = -12
@@ -577,7 +577,7 @@ def _gzip_decode(src: rawptr, src_len: sizet,
if dist == 0 or dist > wpos: if dist == 0 or dist > wpos:
err = -22 err = -22
break break
for ci in range(length): for ci in range(int(length)):
if wpos >= dst_cap: if wpos >= dst_cap:
err = -23 err = -23
break break
+2 -2
View File
@@ -100,7 +100,7 @@ class HashTable:
mask: sizet = self.__capacity__ - 1 mask: sizet = self.__capacity__ - 1
idx: sizet = sizet(h) & mask idx: sizet = sizet(h) & mask
first_tombstone: sizet = self.__capacity__ first_tombstone: sizet = self.__capacity__
for i in range(self.__capacity__): for i in range(int(self.__capacity__)):
sh: uint64 = self._slot_hash(idx) sh: uint64 = self._slot_hash(idx)
if sh == _HT_EMPTY: if sh == _HT_EMPTY:
if first_tombstone < self.__capacity__: if first_tombstone < self.__capacity__:
@@ -131,7 +131,7 @@ class HashTable:
self.__capacity__ = new_cap self.__capacity__ = new_cap
self.__count__ = 0 self.__count__ = 0
self.__tombstones__ = 0 self.__tombstones__ = 0
for i in range(old_cap): for i in range(int(old_cap)):
old_h_ptr: uint64* = t.CPtr(uint64(old_slots) + i * _HT_SLOT_SIZE) old_h_ptr: uint64* = t.CPtr(uint64(old_slots) + i * _HT_SLOT_SIZE)
h: uint64 = old_h_ptr[0] h: uint64 = old_h_ptr[0]
if h != _HT_EMPTY and h != _HT_TOMBSTONE: if h != _HT_EMPTY and h != _HT_TOMBSTONE:
+1 -1
View File
@@ -1,7 +1,7 @@
from stdint import * from stdint import *
import memhub import memhub
import string import string
import viperio
# ============================================================ # ============================================================
# JSON value type enum # JSON value type enum
+1 -2
View File
@@ -1,4 +1,3 @@
import t, c
from stdint import * from stdint import *
import memhub import memhub
import string import string
@@ -145,7 +144,7 @@ def _parse_number(s: ParseState*) -> JsonValue*:
if buf == None: if buf == None:
return null(s.pool) return null(s.pool)
for i in range(num_len): for i in range(int(num_len)):
buf[i] = s.src[start + i] buf[i] = s.src[start + i]
buf[num_len] = '\0' buf[num_len] = '\0'
+3 -5
View File
@@ -1,4 +1,3 @@
import t, c
from stdint import * from stdint import *
import memhub import memhub
import string import string
@@ -35,12 +34,11 @@ def _write_char(buf: str, pos: sizet, cap: sizet, ch: char) -> sizet:
def _write_str(buf: str, pos: sizet, cap: sizet, s: str) -> sizet: def _write_str(buf: str, pos: sizet, cap: sizet, s: str) -> sizet:
if s == None: return pos if s == None: return pos
if pos >= cap: if pos >= cap: return pos # overflow flag already set: stop writing
return pos # overflow flag already set: stop writing
slen: sizet = string.strlen(s) slen: sizet = string.strlen(s)
if pos + slen >= cap: if pos + slen >= cap:
return cap # cannot reserve trailing NUL slot => set overflow flag return cap # cannot reserve trailing NUL slot => set overflow flag
string.memcpy(char(uint64(buf) + pos, t.CPtr), s, slen) string.memcpy(buf + pos, s, slen)
return pos + slen return pos + slen
def _write_string_escaped(buf: str, pos: sizet, cap: sizet, s: str) -> sizet: def _write_string_escaped(buf: str, pos: sizet, cap: sizet, s: str) -> sizet:
@@ -51,7 +49,7 @@ def _write_string_escaped(buf: str, pos: sizet, cap: sizet, s: str) -> sizet:
if pos >= cap: if pos >= cap:
return pos # already overflowed: stop writing return pos # already overflowed: stop writing
slen: sizet = string.strlen(s) slen: sizet = string.strlen(s)
for i in range(slen): for i in range(int(slen)):
if pos >= cap: if pos >= cap:
return pos # already overflowed: converge early, skip pointless per-char looping return pos # already overflowed: converge early, skip pointless per-char looping
ch: char = s[i] ch: char = s[i]
+3 -3
View File
@@ -405,7 +405,7 @@ def factorial(n: int) -> double:
if n < 0: return 0.0 if n < 0: return 0.0
if n == 0 or n == 1: return 1.0 if n == 0 or n == 1: return 1.0
result: double = 1.0 result: double = 1.0
for i in range(2, n + 1): for i in range(2, int(n + 1)):
result *= i result *= i
return result return result
@@ -415,7 +415,7 @@ def combination(n: int, k: int) -> double:
if k == 0 or k == n: return 1.0 if k == 0 or k == n: return 1.0
k = k if k < n - k else n - k k = k if k < n - k else n - k
result: double = 1.0 result: double = 1.0
for i in range(1, k + 1): for i in range(1, int(k + 1)):
result = result * (n - k + i) / i result = result * (n - k + i) / i
return result return result
@@ -423,7 +423,7 @@ def combination(n: int, k: int) -> double:
def permutation(n: int, k: int) -> double: def permutation(n: int, k: int) -> double:
if k < 0 or k > n: return 0.0 if k < 0 or k > n: return 0.0
result: double = 1.0 result: double = 1.0
for i in range(0, k): for i in range(0, int(k)):
result *= (n - i) result *= (n - i)
return result return result
+25 -25
View File
@@ -302,7 +302,7 @@ def _zero_alloc_note() -> sizet:
return _zero_alloc_hits return _zero_alloc_hits
# [CTX] Current context (stage name / current file or module name): set by TPV via SetAllocCtx at # [CTX] Current context (stage name / current file or module name): set by VPC via SetAllocCtx at
# key stages, used only to attribute zero-size / allocation-failure reports. The two fields are kept # key stages, used only to attribute zero-size / allocation-failure reports. The two fields are kept
# separate (no concatenation, no allocation). None = not set => report prints ? (never fabricated). # separate (no concatenation, no allocation). None = not set => report prints ? (never fabricated).
_alloc_ctx_stage: bytes = None _alloc_ctx_stage: bytes = None
@@ -310,7 +310,7 @@ _alloc_ctx_detail: bytes = None
def SetAllocCtx(stage: bytes, detail: bytes = None): def SetAllocCtx(stage: bytes, detail: bytes = None):
"""Set the current context (TPV side); the caller keeps both strings alive for the stage.""" """Set the current context (VPC side); the caller keeps both strings alive for the stage."""
global _alloc_ctx_stage, _alloc_ctx_detail global _alloc_ctx_stage, _alloc_ctx_detail
_alloc_ctx_stage = stage _alloc_ctx_stage = stage
_alloc_ctx_detail = detail _alloc_ctx_detail = detail
@@ -573,7 +573,7 @@ class MemSlab(MemManager):
self.block_size = bs self.block_size = bs
# Clear bitmap # Clear bitmap
for idx in range(map_bytes): for idx in range(int(map_bytes)):
self.alloc_map[idx] = 0 self.alloc_map[idx] = 0
self.block_count = self.usable_size / bs self.block_count = self.usable_size / bs
@@ -581,7 +581,7 @@ class MemSlab(MemManager):
# Construct free list: the first 8 bytes of each block store pointer to next block # Construct free list: the first 8 bytes of each block store pointer to next block
self.free_list = None self.free_list = None
for i in range(self.block_count): for i in range(int(self.block_count)):
block: rawptr = self.usable + i * bs block: rawptr = self.usable + i * bs
*((uint64*)(block)) = uint64(self.free_list) *((uint64*)(block)) = uint64(self.free_list)
self.free_list = block self.free_list = block
@@ -627,7 +627,7 @@ class MemSlab(MemManager):
def reset(self) -> int: def reset(self) -> int:
self.used_count = 0 self.used_count = 0
self.free_list = None self.free_list = None
for i in range(self.block_count): for i in range(int(self.block_count)):
block: rawptr = self.usable + i * self.block_size block: rawptr = self.usable + i * self.block_size
*((uint64*)(block)) = uint64(self.free_list) *((uint64*)(block)) = uint64(self.free_list)
self.free_list = block self.free_list = block
@@ -713,7 +713,7 @@ class MemBuddy(MemManager):
# Initialize all free list heads to be 0 # Initialize all free list heads to be 0
i: int i: int
for i in range(MEMBUDDY_MAX_ORDERS + 1): for i in range(int(MEMBUDDY_MAX_ORDERS + 1)):
self.free_lists[i] = 0 self.free_lists[i] = 0
if size <= fl_bytes: if size <= fl_bytes:
@@ -987,7 +987,7 @@ class MemBuddy(MemManager):
if self.detection != 0: if self.detection != 0:
l_usr = l_usr + 2 l_usr = l_usr + 2
lr_hit: int = 0 lr_hit: int = 0
for lr_i in range(self._det_count): for lr_i in range(int(self._det_count)):
if uint64(self._det_records[lr_i * 2]) == l_usr: if uint64(self._det_records[lr_i * 2]) == l_usr:
# Single load + explicit bit test (print and decision share the # Single load + explicit bit test (print and decision share the
# source word): the raw 8 bytes of the size slot are read once. # source word): the raw 8 bytes of the size slot are read once.
@@ -1085,7 +1085,7 @@ class MemBuddy(MemManager):
if self.detection != 0: if self.detection != 0:
usr_d = usr_d + 2 usr_d = usr_d + 2
hit_d: int = 0 hit_d: int = 0
for ri_d in range(self._det_count): for ri_d in range(int(self._det_count)):
if uint64(self._det_records[ri_d * 2]) == usr_d: if uint64(self._det_records[ri_d * 2]) == usr_d:
w_d: uint64 = self._det_records[ri_d * 2 + 1] w_d: uint64 = self._det_records[ri_d * 2 + 1]
d_sz: uint64 = w_d & ((uint64(1) << 63) - 1) d_sz: uint64 = w_d & ((uint64(1) << 63) - 1)
@@ -1143,7 +1143,7 @@ class MemBuddy(MemManager):
hit_v: sizet = 0 hit_v: sizet = 0
in_pool: sizet = 0 in_pool: sizet = 0
out_pool: sizet = 0 out_pool: sizet = 0
for i in range(nw): for i in range(int(nw)):
wv: uint64 = self._ff_word(rawptr(l_addr), int(i) + 1) wv: uint64 = self._ff_word(rawptr(l_addr), int(i) + 1)
if wv == lcan: if wv == lcan:
hit_v += 1 hit_v += 1
@@ -1234,7 +1234,7 @@ class MemBuddy(MemManager):
if uint64(block) + bs + 16 <= uint64(self.usable) + self.usable_size: if uint64(block) + bs + 16 <= uint64(self.usable) + self.usable_size:
dump_n = 24 dump_n = 24
stdio.printf("[FREEFRAME] %d bytes from canary: ", dump_n) stdio.printf("[FREEFRAME] %d bytes from canary: ", dump_n)
for bi in range(dump_n): for bi in range(int(dump_n)):
bv: uint8 = *((uint8*)(can_addr + uint64(bi))) bv: uint8 = *((uint8*)(can_addr + uint64(bi)))
stdio.printf("%02X ", uint32(bv)) stdio.printf("%02X ", uint32(bv))
stdio.printf("\n") stdio.printf("\n")
@@ -1451,7 +1451,7 @@ class MemBuddy(MemManager):
# are no longer recorded, so the check is skipped to avoid rejecting valid frees. # are no longer recorded, so the check is skipped to avoid rejecting valid frees.
if self.chk_stack is not None and self.chk_depth < self.chk_cap: if self.chk_stack is not None and self.chk_depth < self.chk_cap:
alive: int = 0 alive: int = 0
for ci in range(self.chk_depth): for ci in range(int(self.chk_depth)):
if self.chk_stack[ci] == uint64(block): if self.chk_stack[ci] == uint64(block):
alive = 1 alive = 1
break break
@@ -1558,7 +1558,7 @@ class MemBuddy(MemManager):
# Overlap check: the new block's address must not already be an active # Overlap check: the new block's address must not already be an active
# record (same block allocated twice = free list corruption). # record (same block allocated twice = free list corruption).
ff_d: uint64 = uint64(1) << 63 ff_d: uint64 = uint64(1) << 63
for dup_i in range(self._det_count): for dup_i in range(int(self._det_count)):
drp: uint64 = self._det_records[dup_i * 2] drp: uint64 = self._det_records[dup_i * 2]
drs: uint64 = self._det_records[dup_i * 2 + 1] drs: uint64 = self._det_records[dup_i * 2 + 1]
if drp == rec_ptr_d and (drs & ff_d) == 0: if drp == rec_ptr_d and (drs & ff_d) == 0:
@@ -1625,7 +1625,7 @@ class MemBuddy(MemManager):
"(constant unresolved/shadowed by a same-named symbol/evaluated to 0)\n") "(constant unresolved/shadowed by a same-named symbol/evaluated to 0)\n")
# Fields below are read from existing state only (this branch runs only on # Fields below are read from existing state only (this branch runs only on
# failure): site tag, pool alloc sequence, zero-size hit count, pool state, # failure): site tag, pool alloc sequence, zero-size hit count, pool state,
# and the stage/detail context set by TPV. # and the stage/detail context set by VPC.
usable_addr: uint64 = 0 usable_addr: uint64 = 0
if self.usable is not None: if self.usable is not None:
usable_addr = uint64(self.usable) usable_addr = uint64(self.usable)
@@ -1650,7 +1650,7 @@ class MemBuddy(MemManager):
size, needed2, order2, self.max_order, self.used_bytes(), size, needed2, order2, self.max_order, self.used_bytes(),
uint64(self.usable)) uint64(self.usable))
# Attribution (always-on, no --detection needed): site comes from the alloc_tagged site # Attribution (always-on, no --detection needed): site comes from the alloc_tagged site
# tag, ctx=stage/current file from TPV's SetAllocCtx (? if unset). Failure path only. # tag, ctx=stage/current file from VPC's SetAllocCtx (? if unset). Failure path only.
stdio.printf("[MEM] attribution site=%s ctx=%s/%s pool capacity=%zu\n", stdio.printf("[MEM] attribution site=%s ctx=%s/%s pool capacity=%zu\n",
_det_site_name(_alloc_site_current()), _det_site_name(_alloc_site_current()),
_alloc_ctx_stage_str(), _alloc_ctx_detail_str(), _alloc_ctx_stage_str(), _alloc_ctx_detail_str(),
@@ -1721,7 +1721,7 @@ class MemBuddy(MemManager):
# Read this record's real size (detection fills 0xCD after free, so a later # Read this record's real size (detection fills 0xCD after free, so a later
# write-through breaks the fill = use-after-free). Keep the record and set the # write-through breaks the fill = use-after-free). Keep the record and set the
# free bit; the probe checks the 0xCD fill of still-free blocks. # free bit; the probe checks the 0xCD fill of still-free blocks.
for ri in range(self._det_count): for ri in range(int(self._det_count)):
rp: uint64 = self._det_records[ri * 2] rp: uint64 = self._det_records[ri * 2]
rs: uint64 = self._det_records[ri * 2 + 1] rs: uint64 = self._det_records[ri * 2 + 1]
if rp == uint64(ptr) and (rs & (uint64(1) << 63)) == 0: if rp == uint64(ptr) and (rs & (uint64(1) << 63)) == 0:
@@ -1832,7 +1832,7 @@ class MemBuddy(MemManager):
no longer match); a power-of-two arena is a single max_order block. no longer match); a power-of-two arena is a single max_order block.
""" """
i: int i: int
for i in range(MEMBUDDY_MAX_ORDERS + 1): for i in range(int(MEMBUDDY_MAX_ORDERS + 1)):
self.free_lists[i] = 0 self.free_lists[i] = 0
pos: sizet = 0 pos: sizet = 0
rem: sizet = self.usable_size rem: sizet = self.usable_size
@@ -1911,7 +1911,7 @@ class MemBuddy(MemManager):
return 1 return 1
target: uint64 = uint64(ptr) target: uint64 = uint64(ptr)
free_flag: uint64 = uint64(1) << 63 free_flag: uint64 = uint64(1) << 63
for i in range(self._det_count): for i in range(int(self._det_count)):
rp: uint64 = self._det_records[i * 2] rp: uint64 = self._det_records[i * 2]
rs: uint64 = self._det_records[i * 2 + 1] rs: uint64 = self._det_records[i * 2 + 1]
if rp == target and (rs & free_flag) == 0: if rp == target and (rs & free_flag) == 0:
@@ -1978,7 +1978,7 @@ class MemBuddy(MemManager):
# Overlap check: a ptr with several records means alloc handed the same block to # Overlap check: a ptr with several records means alloc handed the same block to
# different size requests (they overwrite each other). # different size requests (they overwrite each other).
stdio.printf("\nsame-ptr records: ") stdio.printf("\nsame-ptr records: ")
for ci3 in range(self._det_count): for ci3 in range(int(self._det_count)):
cp3: uint64 = self._det_records[ci3 * 2] cp3: uint64 = self._det_records[ci3 * 2]
cs3: uint64 = self._det_records[ci3 * 2 + 1] cs3: uint64 = self._det_records[ci3 * 2 + 1]
if cp3 == p and (cs3 & free_flag) == 0: if cp3 == p and (cs3 & free_flag) == 0:
@@ -2007,7 +2007,7 @@ class MemBuddy(MemManager):
count: sizet = 0 count: sizet = 0
i: int i: int
max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2 max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2
for i in range(MEMBUDDY_MAX_ORDERS + 1): for i in range(int(MEMBUDDY_MAX_ORDERS + 1)):
head_val: uint64 = self.free_lists[i] head_val: uint64 = self.free_lists[i]
cur: rawptr = rawptr(head_val) cur: rawptr = rawptr(head_val)
if uint64(cur) != 0 and self._fl_block_ok(cur) == 0: if uint64(cur) != 0 and self._fl_block_ok(cur) == 0:
@@ -2044,7 +2044,7 @@ class MemBuddy(MemManager):
total: sizet = 0 total: sizet = 0
i: int i: int
max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2 max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2
for i in range(MEMBUDDY_MAX_ORDERS + 1): for i in range(int(MEMBUDDY_MAX_ORDERS + 1)):
head_val: uint64 = self.free_lists[i] head_val: uint64 = self.free_lists[i]
cur: rawptr = rawptr(head_val) cur: rawptr = rawptr(head_val)
if uint64(cur) != 0 and self._fl_block_ok(cur) == 0: if uint64(cur) != 0 and self._fl_block_ok(cur) == 0:
@@ -2061,7 +2061,7 @@ class MemBuddy(MemManager):
stdio.printf("[MEM] free_lists[%d] chain cyclic or node count over limit (next pointer damaged): head=%llu; stop counting to avoid spinning, heap corrupted\n", stdio.printf("[MEM] free_lists[%d] chain cyclic or node count over limit (next pointer damaged): head=%llu; stop counting to avoid spinning, heap corrupted\n",
i, head_val) i, head_val)
break break
total += _block_size_at_order(i) total += _block_size_at_order(int(i))
next_b: rawptr = rawptr(*((uint64*)(cur))) next_b: rawptr = rawptr(*((uint64*)(cur)))
if uint64(next_b) != 0 and self._fl_block_ok(next_b) == 0: if uint64(next_b) != 0 and self._fl_block_ok(next_b) == 0:
stdio.printf("[MEM] free_lists[%d] node invalid (not an in-pool aligned block address; metadata possibly uninitialized/written through): " stdio.printf("[MEM] free_lists[%d] node invalid (not an in-pool aligned block address; metadata possibly uninitialized/written through): "
@@ -2160,7 +2160,7 @@ class MemBuddy(MemManager):
win_live, win_bytes_a) win_live, win_bytes_a)
stdio.printf("[MEM] live-block order distribution (%zu blocks recorded total; single block=%llu<<order bytes):\n", stdio.printf("[MEM] live-block order distribution (%zu blocks recorded total; single block=%llu<<order bytes):\n",
live, uint64(MEMBUDDY_MIN_BLOCK)) live, uint64(MEMBUDDY_MIN_BLOCK))
for oi in range(max_o + 1): for oi in range(int(max_o + 1)):
cnt: uint64 = counts[oi] cnt: uint64 = counts[oi]
if cnt != 0: if cnt != 0:
blk: uint64 = uint64(MEMBUDDY_MIN_BLOCK) << oi blk: uint64 = uint64(MEMBUDDY_MIN_BLOCK) << oi
@@ -2210,7 +2210,7 @@ class MemBuddy(MemManager):
# bound the walk would spin forever (a corruption sentinel must terminate). # bound the walk would spin forever (a corruption sentinel must terminate).
max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2 max_steps: sizet = self.usable_size / MEMBUDDY_MIN_BLOCK + 2
i: int i: int
for i in range(MEMBUDDY_MAX_ORDERS + 1): for i in range(int(MEMBUDDY_MAX_ORDERS + 1)):
head_val: uint64 = self.free_lists[i] head_val: uint64 = self.free_lists[i]
cur: rawptr = rawptr(head_val) cur: rawptr = rawptr(head_val)
if uint64(cur) != 0 and self._fl_block_ok(cur) == 0: if uint64(cur) != 0 and self._fl_block_ok(cur) == 0:
@@ -2230,9 +2230,9 @@ class MemBuddy(MemManager):
return 1 return 1
if uint64(cur) >= uint64(self.usable) + self.usable_size: if uint64(cur) >= uint64(self.usable) + self.usable_size:
return 2 return 2
# Check alignment: order i blocks must be aligned to size _block_size_at_order(i) # Check alignment: order i blocks must be aligned to size _block_size_at_order(int(i))
offset: sizet = uint64(cur) - uint64(self.usable) offset: sizet = uint64(cur) - uint64(self.usable)
bs: sizet = _block_size_at_order(i) bs: sizet = _block_size_at_order(int(i))
if offset % bs != 0: if offset % bs != 0:
return 3 return 3
cur = rawptr(*((uint64*)(cur))) cur = rawptr(*((uint64*)(cur)))
+6 -6
View File
@@ -191,10 +191,10 @@ def _extract_def_name(pool: memhub.MemBuddy*,
while text[i] != '\0': while text[i] != '\0':
if text[i] == ' ' and text[i + 1] == '=' and text[i + 2] == ' ': if text[i] == ' ' and text[i + 1] == '=' and text[i + 2] == ' ':
# Found " = " at position i # Found " = " at position i
name_len: int = i name_len: int = int(i)
name: str = pool.alloc(name_len + 1) name: str = pool.alloc(name_len + 1)
if name is None: return None if name is None: return None
for j in range(name_len): for j in range(int(name_len)):
name[j] = text[j] name[j] = text[j]
name[name_len] = '\0' name[name_len] = '\0'
return name return name
@@ -233,11 +233,11 @@ def _check_uses_in_line(table: GSList[NameEntry]*,
while text[i] != '\0': while text[i] != '\0':
if text[i] == '%': if text[i] == '%':
# Extract name # Extract name
name_start: int = i name_start: int = int(i)
i += 1 i += 1
while text[i] != '\0' and _is_char_name(text[i]) == 1: while text[i] != '\0' and _is_char_name(text[i]) == 1:
i += 1 i += 1
name_len: int = i - name_start name_len: int = int(i - name_start)
if name_len > 1: if name_len > 1:
# Build no temp string (no stack alloc); compare char by char. # Build no temp string (no stack alloc); compare char by char.
if _name_matches_at(text, name_start, name_len, table) == 0: if _name_matches_at(text, name_start, name_len, table) == 0:
@@ -272,7 +272,7 @@ def _str_matches_at(text: str, start: int, length: int,
# Check target length # Check target length
target_len: sizet = string.strlen(target) target_len: sizet = string.strlen(target)
if target_len != length: return 0 if target_len != length: return 0
for i in range(length): for i in range(int(length)):
if text[start + i] != target[i]: return 0 if text[start + i] != target[i]: return 0
return 1 return 1
@@ -314,7 +314,7 @@ def verify_function(pool: memhub.MemBuddy*,
if blk_ref is not None: if blk_ref is not None:
# Build the "%<name>" string # Build the "%<name>" string
blk_ref[0] = '%' blk_ref[0] = '%'
for k in range(blk_name_len): for k in range(int(blk_name_len)):
blk_ref[k + 1] = blk_iter.Name[k] blk_ref[k + 1] = blk_iter.Name[k]
blk_ref[blk_name_len + 1] = '\0' blk_ref[blk_name_len + 1] = '\0'
_name_table_add(name_table, pool, blk_ref) _name_table_add(name_table, pool, blk_ref)
+89 -92
View File
@@ -1,9 +1,7 @@
from stdint import * from stdint import *
import stdlib
import string import string
import math import math
import stdio import stdio
import t, c
import memhub import memhub
@@ -12,8 +10,8 @@ import memhub
# ============================================================ # ============================================================
define MAX_NDIM = 4 define MAX_NDIM = 4
float64: t.CTypedef = double typedef float64 = double
float32: t.CTypedef = float typedef float32 = float
define pi = 3.14159265358979323846 define pi = 3.14159265358979323846
define e = 2.71828182845904523536 define e = 2.71828182845904523536
@@ -29,8 +27,8 @@ define e = 2.71828182845904523536
# ============================================================ # ============================================================
class ndarray[T]: class ndarray[T]:
data: T* data: T*
shape: t.CArray[sizet, MAX_NDIM] shape: [MAX_NDIM]sizet
strides: t.CArray[sizet, MAX_NDIM] strides: [MAX_NDIM]sizet
ndim: int ndim: int
size: sizet size: sizet
owns_data: int owns_data: int
@@ -50,28 +48,28 @@ class ndarray[T]:
def __add__(self, other: ndarray[T]*) -> ndarray[T]*: def __add__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
result.data[i] = self.data[i] + other.data[i] result.data[i] = self.data[i] + other.data[i]
return result return result
def __sub__(self, other: ndarray[T]*) -> ndarray[T]*: def __sub__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
result.data[i] = self.data[i] - other.data[i] result.data[i] = self.data[i] - other.data[i]
return result return result
def __mul__(self, other: ndarray[T]*) -> ndarray[T]*: def __mul__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
result.data[i] = self.data[i] * other.data[i] result.data[i] = self.data[i] * other.data[i]
return result return result
def __truediv__(self, other: ndarray[T]*) -> ndarray[T]*: def __truediv__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
if other.data[i] != T(0): if other.data[i] != T(0):
result.data[i] = self.data[i] / other.data[i] result.data[i] = self.data[i] / other.data[i]
else: else:
@@ -81,7 +79,7 @@ class ndarray[T]:
def __floordiv__(self, other: ndarray[T]*) -> ndarray[T]*: def __floordiv__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
if other.data[i] != T(0): if other.data[i] != T(0):
result.data[i] = math.floor(self.data[i] / other.data[i]) result.data[i] = math.floor(self.data[i] / other.data[i])
else: else:
@@ -91,7 +89,7 @@ class ndarray[T]:
def __mod__(self, other: ndarray[T]*) -> ndarray[T]*: def __mod__(self, other: ndarray[T]*) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
if other.data[i] != T(0): if other.data[i] != T(0):
result.data[i] = math.fmod(self.data[i], other.data[i]) result.data[i] = math.fmod(self.data[i], other.data[i])
else: else:
@@ -101,7 +99,7 @@ class ndarray[T]:
def __neg__(self) -> ndarray[T]*: def __neg__(self) -> ndarray[T]*:
result: ndarray[T]* = _empty_like(self) result: ndarray[T]* = _empty_like(self)
if not result: return None if not result: return None
for i in range(self.size): for i in range(int(self.size)):
result.data[i] = -self.data[i] result.data[i] = -self.data[i]
return result return result
@@ -122,7 +120,7 @@ class ndarray[T]:
self.pool.free(self) self.pool.free(self)
def fill(self, val: T): def fill(self, val: T):
for i in range(self.size): for i in range(int(self.size)):
self.data[i] = val self.data[i] = val
def copy(self) -> ndarray[T]*: def copy(self) -> ndarray[T]*:
@@ -134,7 +132,7 @@ class ndarray[T]:
def reshape(self, new_shape: INTPTR, new_ndim: int) -> ndarray[T]*: def reshape(self, new_shape: INTPTR, new_ndim: int) -> ndarray[T]*:
new_size: sizet = 1 new_size: sizet = 1
i: int = 0 i: int = 0
for i in range(new_ndim): for i in range(int(new_ndim)):
new_size *= sizet(new_shape[i]) new_size *= sizet(new_shape[i])
if new_size != self.size: return None if new_size != self.size: return None
result: ndarray[T]* = _alloc_ndarray(self.pool) result: ndarray[T]* = _alloc_ndarray(self.pool)
@@ -144,14 +142,14 @@ class ndarray[T]:
result.ndim = new_ndim result.ndim = new_ndim
result.size = self.size result.size = self.size
result.owns_data = 0 result.owns_data = 0
for i in range(new_ndim): for i in range(int(new_ndim)):
result.shape[i] = sizet(new_shape[i]) result.shape[i] = sizet(new_shape[i])
_compute_strides(result) _compute_strides(result)
return result return result
def sum(self) -> T: def sum(self) -> T:
s: T = T(0) s: T = T(0)
for i in range(self.size): for i in range(int(self.size)):
s += self.data[i] s += self.data[i]
return s return s
@@ -204,7 +202,7 @@ class ndarray[T]:
def dot(self, other: ndarray[T]*) -> T: def dot(self, other: ndarray[T]*) -> T:
s: T = T(0) s: T = T(0)
n: sizet = self.size if (self.size < other.size) else other.size n: sizet = self.size if (self.size < other.size) else other.size
for i in range(n): for i in range(int(n)):
s += self.data[i] * other.data[i] s += self.data[i] * other.data[i]
return s return s
@@ -218,15 +216,15 @@ class ndarray[T]:
result.shape[0] = cols result.shape[0] = cols
result.shape[1] = rows result.shape[1] = rows
_compute_strides(result) _compute_strides(result)
for i in range(rows): for i in range(int(rows)):
for j in range(cols): for j in range(int(cols)):
result.data[j * rows + i] = self.data[i * cols + j] result.data[j * rows + i] = self.data[i * cols + j]
return result return result
def print_arr(self): def print_arr(self):
if self.ndim == 1: if self.ndim == 1:
stdio.printf("[") stdio.printf("[")
for i in range(self.size): for i in range(int(self.size)):
if i > 0: stdio.printf(", ") if i > 0: stdio.printf(", ")
stdio.printf("%.4f", self.data[i]) stdio.printf("%.4f", self.data[i])
stdio.printf("]\n") stdio.printf("]\n")
@@ -234,10 +232,10 @@ class ndarray[T]:
rows: sizet = self.shape[0] rows: sizet = self.shape[0]
cols: sizet = self.shape[1] cols: sizet = self.shape[1]
stdio.printf("[") stdio.printf("[")
for i in range(rows): for i in range(int(rows)):
if i > 0: stdio.printf(" ") if i > 0: stdio.printf(" ")
stdio.printf("[") stdio.printf("[")
for j in range(cols): for j in range(int(cols)):
if j > 0: stdio.printf(", ") if j > 0: stdio.printf(", ")
stdio.printf("%.4f", self.data[i * cols + j]) stdio.printf("%.4f", self.data[i * cols + j])
if i < rows - 1: if i < rows - 1:
@@ -250,11 +248,11 @@ class ndarray[T]:
# Convenience aliases: typed ndarray instantiations # Convenience aliases: typed ndarray instantiations
# (must be after class ndarray[T] definition to avoid forward reference) # (must be after class ndarray[T] definition to avoid forward reference)
# ============================================================ # ============================================================
Float64Array: t.CTypedef = ndarray[double] typedef Float64Array = ndarray[double]
Float32Array: t.CTypedef = ndarray[float] typedef Float32Array = ndarray[float]
Int64Array: t.CTypedef = ndarray[int32] typedef Int64Array = ndarray[int32]
Int32Array: t.CTypedef = ndarray[int] typedef Int32Array = ndarray[int]
Uint8Array: t.CTypedef = ndarray[uchar] typedef Uint8Array = ndarray[uchar]
# ============================================================ # ============================================================
@@ -263,8 +261,7 @@ Uint8Array: t.CTypedef = ndarray[uchar]
# ============================================================ # ============================================================
def _alloc_ndarray(pool: memhub.MemManager*) -> ndarray[double]*: def _alloc_ndarray(pool: memhub.MemManager*) -> ndarray[double]*:
a: ndarray[double]* = pool.alloc(ndarray[double].__sizeof__()) a: ndarray[double]* = pool.alloc(ndarray[double].__sizeof__())
if a: if a: string.memset(a, 0, ndarray[double].__sizeof__())
string.memset(a, 0, ndarray[double].__sizeof__())
return a return a
def _compute_strides(a: ndarray[double]*): def _compute_strides(a: ndarray[double]*):
@@ -287,7 +284,7 @@ def _empty_like(a: ndarray[double]*) -> ndarray[double]*:
result.size = a.size result.size = a.size
result.owns_data = 1 result.owns_data = 1
i: int = 0 i: int = 0
for i in range(a.ndim): for i in range(int(a.ndim)):
result.shape[i] = a.shape[i] result.shape[i] = a.shape[i]
_compute_strides(result) _compute_strides(result)
return result return result
@@ -357,7 +354,7 @@ def arange(pool: memhub.MemManager*, start: double, stop: double, step: double)
a: ndarray[double]* = zeros(pool, n) a: ndarray[double]* = zeros(pool, n)
if not a: return None if not a: return None
v = start v = start
for i in range(n): for i in range(int(n)):
a.data[i] = v a.data[i] = v
v += step v += step
return a return a
@@ -370,7 +367,7 @@ def linspace(pool: memhub.MemManager*, start: double, stop: double, num: sizet)
a.data[0] = start a.data[0] = start
return a return a
step: double = (stop - start) / double(num - 1) step: double = (stop - start) / double(num - 1)
for i in range(num): for i in range(int(num)):
a.data[i] = start + double(i) * step a.data[i] = start + double(i) * step
return a return a
@@ -404,7 +401,7 @@ def ones2d(pool: memhub.MemManager*, rows: sizet, cols: sizet) -> ndarray[double
def eye(pool: memhub.MemManager*, n: sizet) -> ndarray[double]*: def eye(pool: memhub.MemManager*, n: sizet) -> ndarray[double]*:
a: ndarray[double]* = empty2d(pool, n, n) a: ndarray[double]* = empty2d(pool, n, n)
if not a: return None if not a: return None
for i in range(n): for i in range(int(n)):
a.data[i * n + i] = double(1.0) a.data[i * n + i] = double(1.0)
return a return a
@@ -412,7 +409,7 @@ def diag(pool: memhub.MemManager*, vals: ndarray[double]*) -> ndarray[double]*:
n: sizet = vals.size n: sizet = vals.size
a: ndarray[double]* = empty2d(pool, n, n) a: ndarray[double]* = empty2d(pool, n, n)
if not a: return None if not a: return None
for i in range(n): for i in range(int(n)):
a.data[i * n + i] = vals.data[i] a.data[i * n + i] = vals.data[i]
return a return a
@@ -423,7 +420,7 @@ def diag(pool: memhub.MemManager*, vals: ndarray[double]*) -> ndarray[double]*:
def np_abs(a: ndarray[double]*) -> ndarray[double]*: def np_abs(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
v: double = a.data[i] v: double = a.data[i]
result.data[i] = v if v >= double(0.0) else -v result.data[i] = v if v >= double(0.0) else -v
return result return result
@@ -431,49 +428,49 @@ def np_abs(a: ndarray[double]*) -> ndarray[double]*:
def np_sqrt(a: ndarray[double]*) -> ndarray[double]*: def np_sqrt(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.sqrt(a.data[i]) result.data[i] = math.sqrt(a.data[i])
return result return result
def np_exp(a: ndarray[double]*) -> ndarray[double]*: def np_exp(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.exp(a.data[i]) result.data[i] = math.exp(a.data[i])
return result return result
def np_log(a: ndarray[double]*) -> ndarray[double]*: def np_log(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.log(a.data[i]) result.data[i] = math.log(a.data[i])
return result return result
def np_sin(a: ndarray[double]*) -> ndarray[double]*: def np_sin(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.sin(a.data[i]) result.data[i] = math.sin(a.data[i])
return result return result
def np_cos(a: ndarray[double]*) -> ndarray[double]*: def np_cos(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.cos(a.data[i]) result.data[i] = math.cos(a.data[i])
return result return result
def np_tan(a: ndarray[double]*) -> ndarray[double]*: def np_tan(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.tan(a.data[i]) result.data[i] = math.tan(a.data[i])
return result return result
def np_pow(a: ndarray[double]*, p: double) -> ndarray[double]*: def np_pow(a: ndarray[double]*, p: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.pow(a.data[i], p) result.data[i] = math.pow(a.data[i], p)
return result return result
@@ -484,28 +481,28 @@ def np_pow(a: ndarray[double]*, p: double) -> ndarray[double]*:
def add_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*: def add_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[i] + s result.data[i] = a.data[i] + s
return result return result
def mul_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*: def mul_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[i] * s result.data[i] = a.data[i] * s
return result return result
def sub_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*: def sub_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[i] - s result.data[i] = a.data[i] - s
return result return result
def div_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*: def div_scalar(a: ndarray[double]*, s: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
if s != double(0.0): if s != double(0.0):
result.data[i] = a.data[i] / s result.data[i] = a.data[i] / s
else: else:
@@ -526,10 +523,10 @@ def matmul(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
if k != k2: return None if k != k2: return None
c: ndarray[double]* = empty2d(a.pool, m, n) c: ndarray[double]* = empty2d(a.pool, m, n)
if not c: return None if not c: return None
for i in range(m): for i in range(int(m)):
for j in range(n): for j in range(int(n)):
s: double = double(0.0) s: double = double(0.0)
for p in range(k): for p in range(int(k)):
s += a.data[i * k + p] * b.data[p * n + j] s += a.data[i * k + p] * b.data[p * n + j]
c.data[i * n + j] = s c.data[i * n + j] = s
return c return c
@@ -567,7 +564,7 @@ def var(a: ndarray[double]*) -> double:
if a.size == 0: return double(0.0) if a.size == 0: return double(0.0)
m: double = a.mean() m: double = a.mean()
s: double = double(0.0) s: double = double(0.0)
for i in range(a.size): for i in range(int(a.size)):
d: double = a.data[i] - m d: double = a.data[i] - m
s += d * d s += d * d
return s / double(a.size) return s / double(a.size)
@@ -581,7 +578,7 @@ def norm(a: ndarray[double]*) -> double:
def clip(a: ndarray[double]*, lo: double, hi: double) -> ndarray[double]*: def clip(a: ndarray[double]*, lo: double, hi: double) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
v: double = a.data[i] v: double = a.data[i]
if v < lo: v = lo if v < lo: v = lo
if v > hi: v = hi if v > hi: v = hi
@@ -620,7 +617,7 @@ def sort_arr(a: ndarray[double]*) -> ndarray[double]*:
def reverse(a: ndarray[double]*) -> ndarray[double]*: def reverse(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[a.size - 1 - i] result.data[i] = a.data[a.size - 1 - i]
return result return result
@@ -631,42 +628,42 @@ def reverse(a: ndarray[double]*) -> ndarray[double]*:
def np_log10(a: ndarray[double]*) -> ndarray[double]*: def np_log10(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.log10(a.data[i]) result.data[i] = math.log10(a.data[i])
return result return result
def np_log2(a: ndarray[double]*) -> ndarray[double]*: def np_log2(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.log2(a.data[i]) result.data[i] = math.log2(a.data[i])
return result return result
def np_floor(a: ndarray[double]*) -> ndarray[double]*: def np_floor(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.floor(a.data[i]) result.data[i] = math.floor(a.data[i])
return result return result
def np_ceil(a: ndarray[double]*) -> ndarray[double]*: def np_ceil(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.ceil(a.data[i]) result.data[i] = math.ceil(a.data[i])
return result return result
def np_round(a: ndarray[double]*) -> ndarray[double]*: def np_round(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.round(a.data[i]) result.data[i] = math.round(a.data[i])
return result return result
def np_sign(a: ndarray[double]*) -> ndarray[double]*: def np_sign(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
if a.data[i] > double(0.0): if a.data[i] > double(0.0):
result.data[i] = double(1.0) result.data[i] = double(1.0)
elif a.data[i] < double(0.0): elif a.data[i] < double(0.0):
@@ -678,91 +675,91 @@ def np_sign(a: ndarray[double]*) -> ndarray[double]*:
def np_tanh(a: ndarray[double]*) -> ndarray[double]*: def np_tanh(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.tanh(a.data[i]) result.data[i] = math.tanh(a.data[i])
return result return result
def np_sinh(a: ndarray[double]*) -> ndarray[double]*: def np_sinh(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.sinh(a.data[i]) result.data[i] = math.sinh(a.data[i])
return result return result
def np_cosh(a: ndarray[double]*) -> ndarray[double]*: def np_cosh(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.cosh(a.data[i]) result.data[i] = math.cosh(a.data[i])
return result return result
def np_arcsin(a: ndarray[double]*) -> ndarray[double]*: def np_arcsin(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.asin(a.data[i]) result.data[i] = math.asin(a.data[i])
return result return result
def np_arccos(a: ndarray[double]*) -> ndarray[double]*: def np_arccos(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.acos(a.data[i]) result.data[i] = math.acos(a.data[i])
return result return result
def np_arctan(a: ndarray[double]*) -> ndarray[double]*: def np_arctan(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.atan(a.data[i]) result.data[i] = math.atan(a.data[i])
return result return result
def np_arctan2(y: ndarray[double]*, x: ndarray[double]*) -> ndarray[double]*: def np_arctan2(y: ndarray[double]*, x: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(y) result: ndarray[double]* = _empty_like(y)
if not result: return None if not result: return None
for i in range(y.size): for i in range(int(y.size)):
result.data[i] = math.atan2(y.data[i], x.data[i]) result.data[i] = math.atan2(y.data[i], x.data[i])
return result return result
def np_degrees(a: ndarray[double]*) -> ndarray[double]*: def np_degrees(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.degrees(a.data[i]) result.data[i] = math.degrees(a.data[i])
return result return result
def np_radians(a: ndarray[double]*) -> ndarray[double]*: def np_radians(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = math.radians(a.data[i]) result.data[i] = math.radians(a.data[i])
return result return result
def np_isnan(a: ndarray[double]*) -> ndarray[double]*: def np_isnan(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(math.isnan(a.data[i])) result.data[i] = double(math.isnan(a.data[i]))
return result return result
def np_isinf(a: ndarray[double]*) -> ndarray[double]*: def np_isinf(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(math.isinf(a.data[i])) result.data[i] = double(math.isinf(a.data[i]))
return result return result
def np_maximum(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_maximum(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[i] if a.data[i] > b.data[i] else b.data[i] result.data[i] = a.data[i] if a.data[i] > b.data[i] else b.data[i]
return result return result
def np_minimum(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_minimum(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = a.data[i] if a.data[i] < b.data[i] else b.data[i] result.data[i] = a.data[i] if a.data[i] < b.data[i] else b.data[i]
return result return result
@@ -774,7 +771,7 @@ def cumsum(a: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
s: double = double(0.0) s: double = double(0.0)
for i in range(a.size): for i in range(int(a.size)):
s += a.data[i] s += a.data[i]
result.data[i] = s result.data[i] = s
return result return result
@@ -783,7 +780,7 @@ def diff(a: ndarray[double]*) -> ndarray[double]*:
if a.size < 2: return None if a.size < 2: return None
result: ndarray[double]* = zeros(a.pool, a.size - 1) result: ndarray[double]* = zeros(a.pool, a.size - 1)
if not result: return None if not result: return None
for i in range(a.size - 1): for i in range(int(a.size - 1)):
result.data[i] = a.data[i + 1] - a.data[i] result.data[i] = a.data[i + 1] - a.data[i]
return result return result
@@ -800,22 +797,22 @@ def trace(a: ndarray[double]*) -> double:
if a.ndim != 2: return double(0.0) if a.ndim != 2: return double(0.0)
n: sizet = a.shape[0] if a.shape[0] < a.shape[1] else a.shape[1] n: sizet = a.shape[0] if a.shape[0] < a.shape[1] else a.shape[1]
s: double = double(0.0) s: double = double(0.0)
for i in range(n): for i in range(int(n)):
s += a.data[i * a.shape[1] + i] s += a.data[i * a.shape[1] + i]
return s return s
def outer(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def outer(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = empty2d(a.pool, a.size, b.size) result: ndarray[double]* = empty2d(a.pool, a.size, b.size)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
for j in range(b.size): for j in range(int(b.size)):
result.data[i * b.size + j] = a.data[i] * b.data[j] result.data[i * b.size + j] = a.data[i] * b.data[j]
return result return result
def np_where(condition: ndarray[double]*, x: ndarray[double]*, y: ndarray[double]*) -> ndarray[double]*: def np_where(condition: ndarray[double]*, x: ndarray[double]*, y: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(x) result: ndarray[double]* = _empty_like(x)
if not result: return None if not result: return None
for i in range(x.size): for i in range(int(x.size)):
if condition.data[i] != double(0.0): if condition.data[i] != double(0.0):
result.data[i] = x.data[i] result.data[i] = x.data[i]
else: else:
@@ -824,19 +821,19 @@ def np_where(condition: ndarray[double]*, x: ndarray[double]*, y: ndarray[double
def np_count_nonzero(a: ndarray[double]*) -> int: def np_count_nonzero(a: ndarray[double]*) -> int:
count: int = 0 count: int = 0
for i in range(a.size): for i in range(int(a.size)):
if a.data[i] != double(0.0): if a.data[i] != double(0.0):
count += 1 count += 1
return count return count
def np_all(a: ndarray[double]*) -> int: def np_all(a: ndarray[double]*) -> int:
for i in range(a.size): for i in range(int(a.size)):
if a.data[i] == double(0.0): if a.data[i] == double(0.0):
return 0 return 0
return 1 return 1
def np_any(a: ndarray[double]*) -> int: def np_any(a: ndarray[double]*) -> int:
for i in range(a.size): for i in range(int(a.size)):
if a.data[i] != double(0.0): if a.data[i] != double(0.0):
return 1 return 1
return 0 return 0
@@ -844,42 +841,42 @@ def np_any(a: ndarray[double]*) -> int:
def np_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] == b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] == b.data[i] else double(0.0)
return result return result
def np_not_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_not_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] != b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] != b.data[i] else double(0.0)
return result return result
def np_less(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_less(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] < b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] < b.data[i] else double(0.0)
return result return result
def np_greater(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_greater(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] > b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] > b.data[i] else double(0.0)
return result return result
def np_less_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_less_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] <= b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] <= b.data[i] else double(0.0)
return result return result
def np_greater_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*: def np_greater_equal(a: ndarray[double]*, b: ndarray[double]*) -> ndarray[double]*:
result: ndarray[double]* = _empty_like(a) result: ndarray[double]* = _empty_like(a)
if not result: return None if not result: return None
for i in range(a.size): for i in range(int(a.size)):
result.data[i] = double(1.0) if a.data[i] >= b.data[i] else double(0.0) result.data[i] = double(1.0) if a.data[i] >= b.data[i] else double(0.0)
return result return result
@@ -909,7 +906,7 @@ def zeros_like(a: ndarray[double]*) -> ndarray[double]*:
result.size = a.size result.size = a.size
result.owns_data = 1 result.owns_data = 1
i: int = 0 i: int = 0
for i in range(a.ndim): for i in range(int(a.ndim)):
result.shape[i] = a.shape[i] result.shape[i] = a.shape[i]
_compute_strides(result) _compute_strides(result)
return result return result
@@ -931,8 +928,8 @@ def meshgrid(x: ndarray[double]*, y: ndarray[double]*) -> ndarray[double]*:
# Returns X grid as 2D array (each row is x) # Returns X grid as 2D array (each row is x)
result: ndarray[double]* = empty2d(x.pool, y.size, x.size) result: ndarray[double]* = empty2d(x.pool, y.size, x.size)
if not result: return None if not result: return None
for i in range(y.size): for i in range(int(y.size)):
for j in range(x.size): for j in range(int(x.size)):
result.data[i * x.size + j] = x.data[j] result.data[i * x.size + j] = x.data[j]
return result return result
@@ -972,7 +969,7 @@ def np_interp(x: double, xp: ndarray[double]*, fp: ndarray[double]*) -> double:
if xp.size == 0: return double(0.0) if xp.size == 0: return double(0.0)
if x <= xp.data[0]: return fp.data[0] if x <= xp.data[0]: return fp.data[0]
if x >= xp.data[xp.size - 1]: return fp.data[xp.size - 1] if x >= xp.data[xp.size - 1]: return fp.data[xp.size - 1]
for i in range(xp.size - 1): for i in range(int(xp.size - 1)):
if x >= xp.data[i] and x <= xp.data[i + 1]: if x >= xp.data[i] and x <= xp.data[i + 1]:
t_val: double = (x - xp.data[i]) / (xp.data[i + 1] - xp.data[i]) t_val: double = (x - xp.data[i]) / (xp.data[i + 1] - xp.data[i])
return fp.data[i] + t_val * (fp.data[i + 1] - fp.data[i]) return fp.data[i] + t_val * (fp.data[i + 1] - fp.data[i])
@@ -980,7 +977,7 @@ def np_interp(x: double, xp: ndarray[double]*, fp: ndarray[double]*) -> double:
def prod(a: ndarray[double]*) -> double: def prod(a: ndarray[double]*) -> double:
p: double = double(1.0) p: double = double(1.0)
for i in range(a.size): for i in range(int(a.size)):
p *= a.data[i] p *= a.data[i]
return p return p
-2
View File
@@ -15,8 +15,6 @@
# * Password-callback parameters are always placeholder bytes; pass t.CPtr(0)/None when unused. # * Password-callback parameters are always placeholder bytes; pass t.CPtr(0)/None when unused.
# * Linked libraries: libcrypto + libssl (on Windows with mingw, this means -lcrypto -lssl). # * Linked libraries: libcrypto + libssl (on Windows with mingw, this means -lcrypto -lssl).
# ============================================================ # ============================================================
import t
from openssl.types import * from openssl.types import *
from openssl.crypto import * from openssl.crypto import *
from openssl.err import * from openssl.err import *
-1
View File
@@ -1,4 +1,3 @@
import t, c
from openssl.types import * from openssl.types import *
# ============================================================ # ============================================================
-1
View File
@@ -1,4 +1,3 @@
import t, c
from stdint import * from stdint import *
import platmacro import platmacro
import memhub import memhub
-1
View File
@@ -1,4 +1,3 @@
import t, c
from stdint import * from stdint import *
import posix import posix
import memhub import memhub
+4 -5
View File
@@ -1,4 +1,3 @@
import t, c
from stdint import * from stdint import *
import win32.win32base import win32.win32base
import win32.win32file import win32.win32file
@@ -57,7 +56,7 @@ def getsize(path: str) -> int64:
"""Return the file size in bytes. Return -1 on failure.""" """Return the file size in bytes. Return -1 on failure."""
handle: win32.win32base.HANDLE = win32.win32file.CreateFileA( handle: win32.win32base.HANDLE = win32.win32file.CreateFileA(
path, win32.win32file.GENERIC_READ, win32.win32file.FILE_SHARE_READ, path, win32.win32file.GENERIC_READ, win32.win32file.FILE_SHARE_READ,
t.CPtr(0), win32.win32file.OPEN_EXISTING, win32.win32file.FILE_ATTRIBUTE_NORMAL, t.CPtr(0) None, win32.win32file.OPEN_EXISTING, win32.win32file.FILE_ATTRIBUTE_NORMAL, None
) )
if handle == win32.win32base.INVALID_HANDLE_VALUE: if handle == win32.win32base.INVALID_HANDLE_VALUE:
return -1 return -1
@@ -107,17 +106,17 @@ def mkdir(path: str) -> int:
"""Create a directory. Return 0 on success, -1 on failure. """Create a directory. Return 0 on success, -1 on failure.
If path is valid UTF-8, convert to UTF-16 and use CreateDirectoryW; otherwise fall back to the A-series.""" If path is valid UTF-8, convert to UTF-16 and use CreateDirectoryW; otherwise fall back to the A-series."""
if win32.win32base.utf8_is_valid(path) == 0: if win32.win32base.utf8_is_valid(path) == 0:
result: BOOL = win32.win32file.CreateDirectoryA(path, t.CPtr(0)) result: BOOL = win32.win32file.CreateDirectoryA(path, None)
if result == 0: if result == 0:
return -1 return -1
return 0 return 0
wp: win32.win32base.WCHARPTR = win32.win32base.utf8_to_utf16_dup(path) wp: win32.win32base.WCHARPTR = win32.win32base.utf8_to_utf16_dup(path)
if wp is None: if wp is None:
result_a: BOOL = win32.win32file.CreateDirectoryA(path, t.CPtr(0)) result_a: BOOL = win32.win32file.CreateDirectoryA(path, None)
if result_a == 0: if result_a == 0:
return -1 return -1
return 0 return 0
result_w: BOOL = win32.win32file.CreateDirectoryW(t.CPtr(wp), t.CPtr(0)) result_w: BOOL = win32.win32file.CreateDirectoryW(t.CPtr(wp), None)
stdlib.free(wp) stdlib.free(wp)
if result_w == 0: if result_w == 0:
return -1 return -1
-2
View File
@@ -1,6 +1,4 @@
import t, c
from stdint import * from stdint import *
import string
import memhub import memhub
import platmacro import platmacro
+6 -6
View File
@@ -134,37 +134,37 @@ def rdseed64_step(p: uint64*) -> int:
# ============================================================ # ============================================================
def random_u16() -> uint16: def random_u16() -> uint16:
r: uint16 = 0 r: uint16 = 0
for i in range(RDRAND_RETRY_LIMIT): for i in range(int(RDRAND_RETRY_LIMIT)):
if rdrand16_step(&r) == 1: return r if rdrand16_step(&r) == 1: return r
return 0 return 0
def random_u32() -> uint32: def random_u32() -> uint32:
r: uint32 = 0 r: uint32 = 0
for i in range(RDRAND_RETRY_LIMIT): for i in range(int(RDRAND_RETRY_LIMIT)):
if rdrand32_step(&r) == 1: return r if rdrand32_step(&r) == 1: return r
return 0 return 0
def random_u64() -> uint64: def random_u64() -> uint64:
r: uint64 = 0 r: uint64 = 0
for i in range(RDRAND_RETRY_LIMIT): for i in range(int(RDRAND_RETRY_LIMIT)):
if rdrand64_step(&r) == 1: return r if rdrand64_step(&r) == 1: return r
return 0 return 0
def seed_u16() -> uint16: def seed_u16() -> uint16:
r: uint16 = 0 r: uint16 = 0
for i in range(RDRAND_RETRY_LIMIT): for i in range(int(RDRAND_RETRY_LIMIT)):
if rdseed16_step(&r) == 1: return r if rdseed16_step(&r) == 1: return r
return 0 return 0
def seed_u32() -> uint32: def seed_u32() -> uint32:
r: uint32 = 0 r: uint32 = 0
for i in range(RDRAND_RETRY_LIMIT): for i in range(int(RDRAND_RETRY_LIMIT)):
if rdseed32_step(&r) == 1: return r if rdseed32_step(&r) == 1: return r
return 0 return 0
def seed_u64() -> uint64: def seed_u64() -> uint64:
r: uint64 = 0 r: uint64 = 0
for i in range(RDSEED_RETRY_LIMIT): for i in range(int(RDSEED_RETRY_LIMIT)):
if rdseed64_step(&r) == 1: return r if rdseed64_step(&r) == 1: return r
return 0 return 0
+3 -3
View File
@@ -299,7 +299,7 @@ def decode_chunked(src: str, src_len: INT, dst: str, dst_cap: INT) -> INT:
if size == 0: return w if size == 0: return w
# 4) Copy data + skip CRLF # 4) Copy data + skip CRLF
if w + size > dst_cap: return -1 if w + size > dst_cap: return -1
for k in range(size): for k in range(int(size)):
dst[w + k] = src[r + k] dst[w + k] = src[r + k]
w += size w += size
r += size r += size
@@ -555,7 +555,7 @@ def _ResolveRedirectUrl(base: str, loc: str, out: str, cap: INT) -> INT:
prefix_len: INT = INT(uint64(q) - uint64(base)) prefix_len: INT = INT(uint64(q) - uint64(base))
need: INT = prefix_len + lb + 2 need: INT = prefix_len + lb + 2
if need >= cap: return -1 if need >= cap: return -1
for i in range(prefix_len): for i in range(int(prefix_len)):
out[i] = base[i] out[i] = base[i]
if loc[0] == '/': if loc[0] == '/':
j: INT = 0 j: INT = 0
@@ -701,7 +701,7 @@ def _RawHeaderValue(buf: str, total: INT, name: str, out: str, out_size: INT) ->
vlen: INT = eol - v vlen: INT = eol - v
if vlen > out_size - 1: if vlen > out_size - 1:
vlen = out_size - 1 vlen = out_size - 1
for i in range(vlen): for i in range(int(vlen)):
out[i] = buf[v + i] out[i] = buf[v + i]
out[vlen] = 0 out[vlen] = 0
return 0 return 0
+4 -4
View File
@@ -338,7 +338,7 @@ def download(url: str, path: str, threads: INT = 1,
handles: t.CArray[VOIDPTR, DL_MAX_THREADS] handles: t.CArray[VOIDPTR, DL_MAX_THREADS]
threads_ok: INT = 0 threads_ok: INT = 0
for i in range(threads): for i in range(int(threads)):
b_start: sizet = sizet(i) * band b_start: sizet = sizet(i) * band
if b_start >= total: if b_start >= total:
break break
@@ -392,21 +392,21 @@ def download(url: str, path: str, threads: INT = 1,
return -3 return -3
# Wait for all threads to finish # Wait for all threads to finish
for j in range(threads_ok): for j in range(int(threads_ok)):
wsync.WaitForSingleObject(handles[j], 0xFFFFFFFF) wsync.WaitForSingleObject(handles[j], 0xFFFFFFFF)
wb.CloseHandle(handles[j]) wb.CloseHandle(handles[j])
# Aggregate results # Aggregate results
total_done: sizet = 0 total_done: sizet = 0
failed_job: INT = 0 failed_job: INT = 0
for k in range(threads_ok): for k in range(int(threads_ok)):
jobk: DlJob* = ctxs[k] jobk: DlJob* = ctxs[k]
total_done += jobk.done total_done += jobk.done
if jobk.status != 0: if jobk.status != 0:
failed_job = 1 failed_job = 1
# Free thread contexts # Free thread contexts
for m in range(threads_ok): for m in range(int(threads_ok)):
jobm: DlJob* = ctxs[m] jobm: DlJob* = ctxs[m]
stdlib.free(jobm.path) stdlib.free(jobm.path)
stdlib.free(jobm.url) stdlib.free(jobm.url)
+3 -3
View File
@@ -73,7 +73,7 @@ def stream_pull(out: str, cap: INT) -> INT:
avail = INT(_st_len - _st_consumed) avail = INT(_st_len - _st_consumed)
if avail > cap - 1: if avail > cap - 1:
avail = cap - 1 avail = cap - 1
for i in range(sizet(avail)): for i in range(int(sizet(avail))):
out[i] = _st_buf[_st_consumed + i] out[i] = _st_buf[_st_consumed + i]
out[sizet(avail)] = 0 out[sizet(avail)] = 0
_st_consumed += sizet(avail) _st_consumed += sizet(avail)
@@ -805,7 +805,7 @@ def post_stream(url: str, data: str, content_type: str = "application/json",
while True: while True:
# Find line end \n # Find line end \n
nl: INT = -1 nl: INT = -1
for k in range(llen): for k in range(int(llen)):
if leftover[k] == 10: if leftover[k] == 10:
nl = k nl = k
break break
@@ -846,7 +846,7 @@ def post_stream(url: str, data: str, content_type: str = "application/json",
_StreamAppendContent(dtx) _StreamAppendContent(dtx)
# 3) Remove processed line # 3) Remove processed line
rem: INT = llen - (nl + 1) rem: INT = llen - (nl + 1)
for j2 in range(rem): for j2 in range(int(rem)):
leftover[j2] = leftover[nl + 1 + j2] leftover[j2] = leftover[nl + 1 + j2]
llen = rem llen = rem
+2 -2
View File
@@ -86,7 +86,7 @@ def _init_argv_win32():
j: sizet = 0 j: sizet = 0
in_quote: bool = False in_quote: bool = False
in_arg: bool = False in_arg: bool = False
for i in range(cmdlen): for i in range(int(cmdlen)):
ch: int = cmdline[i] ch: int = cmdline[i]
if ch == 34: # Double quote if ch == 34: # Double quote
in_quote = not in_quote in_quote = not in_quote
@@ -128,7 +128,7 @@ def _init_argv_posix():
# Calculate argument count (\0 delimited) # Calculate argument count (\0 delimited)
argc: int = 0 argc: int = 0
total_size: sizet = sizet(total) total_size: sizet = sizet(total)
for i in range(total_size): for i in range(int(total_size)):
if buf[i] == 0: argc += 1 if buf[i] == 0: argc += 1
if argc == 0: return if argc == 0: return
# Allocate pointer array for argv (argc pointers) # Allocate pointer array for argv (argc pointers)
+6 -6
View File
@@ -128,7 +128,7 @@ def tar_num(p: uint8*, n: sizet) -> uint64:
if n == 0: return 0 if n == 0: return 0
if (p[0] & 128) != 0: if (p[0] & 128) != 0:
v: int64 = int64(p[0] & 127) v: int64 = int64(p[0] & 127)
for i in range(1, n): for i in range(1, int(n)):
v = (v << 8) | int64(p[i]) v = (v << 8) | int64(p[i])
if v < 0: return 0 if v < 0: return 0
return uint64(v) return uint64(v)
@@ -169,7 +169,7 @@ def tar_put_num(p: uint8*, n: sizet, v: uint64) -> int:
# Parse up to the first non-digit as a decimal value (a PAX mtime may carry a fraction). # Parse up to the first non-digit as a decimal value (a PAX mtime may carry a fraction).
def tar_dec(v: uint8*, n: sizet) -> uint64: def tar_dec(v: uint8*, n: sizet) -> uint64:
out: uint64 = 0 out: uint64 = 0
for i in range(n): for i in range(int(n)):
c: uint8 = v[i] c: uint8 = v[i]
if c < '0' or c > '9': break if c < '0' or c > '9': break
out = out * 10 + uint64(c - '0') out = out * 10 + uint64(c - '0')
@@ -208,14 +208,14 @@ def tar_pad(n: uint64) -> sizet:
# Header checksum: the unsigned byte sum with the checksum field itself read as eight spaces. # Header checksum: the unsigned byte sum with the checksum field itself read as eight spaces.
def tar_checksum(block: uint8*) -> uint32: def tar_checksum(block: uint8*) -> uint32:
sum: uint32 = 0 sum: uint32 = 0
for i in range(TAR_BLOCK): for i in range(int(TAR_BLOCK)):
if i >= TAR_OFF_CHKSUM and i < TAR_OFF_CHKSUM + 8: sum += 32 if i >= TAR_OFF_CHKSUM and i < TAR_OFF_CHKSUM + 8: sum += 32
else: sum += uint32(block[i]) else: sum += uint32(block[i])
return sum return sum
def tar_is_zero(block: uint8*) -> bool: def tar_is_zero(block: uint8*) -> bool:
for i in range(TAR_BLOCK): for i in range(int(TAR_BLOCK)):
if block[i] != 0: return False if block[i] != 0: return False
return True return True
@@ -497,7 +497,7 @@ def tar_join(dest: str, name: bytes, name_len: sizet) -> bytes:
def tar_mkdir_p(path: bytes) -> int: def tar_mkdir_p(path: bytes) -> int:
n: sizet = string.strlen(path) n: sizet = string.strlen(path)
if n == 0: return -1 if n == 0: return -1
for i in range(n): for i in range(int(n)):
c: char = path[i] c: char = path[i]
if c != '/' and c != '\\': continue if c != '/' and c != '\\': continue
path[i] = 0 path[i] = 0
@@ -971,7 +971,7 @@ class TarFile:
string.memcpy(buf, name, n) string.memcpy(buf, name, n)
buf[n] = '/' buf[n] = '/'
buf[n + 1] = 0 buf[n + 1] = 0
rc: int = self._write_entry(buf, n + 1, mode, 0, 0, uint64(0), mtime, TAR_DIR, None, 0) rc: int = int(self._write_entry(buf, n + 1, mode, 0, 0, uint64(0), mtime, TAR_DIR, None, 0))
stdlib.free(buf) stdlib.free(buf)
return rc return rc
+2 -2
View File
@@ -419,7 +419,7 @@ def _parse_inline_table(s: TomlState*) -> JsonValue*:
_advance(s) _advance(s)
val: JsonValue* = _parse_value(s) val: JsonValue* = _parse_value(s)
if val == None: return None if val == None: return None
tbl: JsonValue* = _dotted_target(s, obj, segs, n, 0) tbl: JsonValue* = _dotted_target(s, obj, segs, int(n), 0)
if tbl == None: return None if tbl == None: return None
last: str = segs[n - 1] last: str = segs[n - 1]
if tbl[last] != None: return None if tbl[last] != None: return None
@@ -669,7 +669,7 @@ def _parse_keyval(s: TomlState*) -> int:
_advance(s) _advance(s)
val: JsonValue* = _parse_value(s) val: JsonValue* = _parse_value(s)
if val == None: return -1 if val == None: return -1
tbl: JsonValue* = _dotted_target(s, s.cur, segs, n, 1) tbl: JsonValue* = _dotted_target(s, s.cur, segs, int(n), 1)
if tbl == None: return -1 if tbl == None: return -1
last: str = segs[n - 1] last: str = segs[n - 1]
if tbl[last] != None: return -1 # duplicate key if tbl[last] != None: return -1 # duplicate key
+47 -47
View File
@@ -119,7 +119,7 @@ def fill(t0: Tensor*, val: double):
return return
d: double* = t0.Data d: double* = t0.Data
n: sizet = t0.Rows * t0.Cols n: sizet = t0.Rows * t0.Cols
for i in range(n): for i in range(int(n)):
d[i] = val d[i] = val
@@ -159,7 +159,7 @@ def uniform_fill(t0: Tensor*, lo: double, hi: double):
d: double* = t0.Data d: double* = t0.Data
n: sizet = t0.Rows * t0.Cols n: sizet = t0.Rows * t0.Cols
span: double = hi - lo span: double = hi - lo
for i in range(n): for i in range(int(n)):
d[i] = lo + _next_rand() * span d[i] = lo + _next_rand() * span
@@ -237,8 +237,8 @@ def linear_fwd(w: Tensor*, b: Tensor*,
if acc_buf is None: if acc_buf is None:
return return
accp: double* = acc_buf accp: double* = acc_buf
for i in range(m): for i in range(int(m)):
for j in range(n): for j in range(int(n)):
acc: double = bd[j] acc: double = bd[j]
kk: sizet = 0 kk: sizet = 0
while kk + 4 <= k: while kk + 4 <= k:
@@ -286,12 +286,12 @@ def linear_bwd(w: Tensor*, x: Tensor*,
string.memset(db.Data, 0, n * sizet(8)) string.memset(db.Data, 0, n * sizet(8))
string.memset(dx.Data, 0, m * k * sizet(8)) string.memset(dx.Data, 0, m * k * sizet(8))
for b in range(m): for b in range(int(m)):
for j in range(n): for j in range(int(n)):
gj: double = gd[b * n + j] gj: double = gd[b * n + j]
if gj != double(0.0): if gj != double(0.0):
dbd[j] = dbd[j] + gj dbd[j] = dbd[j] + gj
for i in range(k): for i in range(int(k)):
dwd[j * k + i] = dwd[j * k + i] + gj * xd[b * k + i] dwd[j * k + i] = dwd[j * k + i] + gj * xd[b * k + i]
dxd[b * k + i] = dxd[b * k + i] + gj * wd[j * k + i] dxd[b * k + i] = dxd[b * k + i] + gj * wd[j * k + i]
@@ -334,7 +334,7 @@ def relu_backward(x: Tensor*, g: Tensor*,
xd: double* = x.Data xd: double* = x.Data
gd: double* = g.Data gd: double* = g.Data
od: double* = gx.Data od: double* = gx.Data
for i in range(n): for i in range(int(n)):
if xd[i] > double(0.0): if xd[i] > double(0.0):
od[i] = gd[i] od[i] = gd[i]
else: else:
@@ -362,7 +362,7 @@ def softmax_crossentropy(logits: Tensor*,
lb: int* = labels lb: int* = labels
total: double = double(0.0) total: double = double(0.0)
inv_m: double = double(1.0) / double(m) inv_m: double = double(1.0) / double(m)
for b in range(m): for b in range(int(m)):
# Numerically stable softmax # Numerically stable softmax
mx: double = ld[b * c] mx: double = ld[b * c]
j: sizet = 1 j: sizet = 1
@@ -399,7 +399,7 @@ def row_argmax(x: Tensor*, pred: int*):
m: sizet = x.Rows m: sizet = x.Rows
c: sizet = x.Cols c: sizet = x.Cols
d: double* = x.Data d: double* = x.Data
for i in range(m): for i in range(int(m)):
best: sizet = 0 best: sizet = 0
bv: double = d[i * c] bv: double = d[i * c]
j: sizet = 1 j: sizet = 1
@@ -420,7 +420,7 @@ def sgd_step(param: Tensor*, lr: double):
d: double* = param.Data d: double* = param.Data
g: double* = param.Grad g: double* = param.Grad
n: sizet = param.Rows * param.Cols n: sizet = param.Rows * param.Cols
for i in range(n): for i in range(int(n)):
d[i] = d[i] - lr * g[i] d[i] = d[i] - lr * g[i]
@@ -437,7 +437,7 @@ def sgd_step_momentum(param: Tensor*, vel: t.CPtr,
g: double* = param.Grad g: double* = param.Grad
v: double* = vel v: double* = vel
n: sizet = param.Rows * param.Cols n: sizet = param.Rows * param.Cols
for i in range(n): for i in range(int(n)):
vv: double = momentum * v[i] + g[i] vv: double = momentum * v[i] + g[i]
v[i] = vv v[i] = vv
d[i] = d[i] - lr * vv d[i] = d[i] - lr * vv
@@ -500,8 +500,8 @@ def conv2d_fwd(x: t.CPtr, w: t.CPtr, b: t.CPtr, out: t.CPtr,
wbd: double* = wb wbd: double* = wb
t4d: double* = t4 t4d: double* = t4
for bi in range(n): for bi in range(int(n)):
for co in range(cout): for co in range(int(cout)):
bias: double = double(0.0) bias: double = double(0.0)
if b is not None: if b is not None:
bd: double* = b bd: double* = b
@@ -509,13 +509,13 @@ def conv2d_fwd(x: t.CPtr, w: t.CPtr, b: t.CPtr, out: t.CPtr,
# Pre-broadcast weight scalars: one 4-lane copy per (ci,ky,kx), shared by all output rows/cols of this channel. # Pre-broadcast weight scalars: one 4-lane copy per (ci,ky,kx), shared by all output rows/cols of this channel.
# Otherwise vbroadcastsd (4 stores) repeats in the 4-lane loop and halves the SIMD gain. # Otherwise vbroadcastsd (4 stores) repeats in the 4-lane loop and halves the SIMD gain.
if simd_on != 0: if simd_on != 0:
for wb4 in range(kk): for wb4 in range(int(kk)):
wq: double = wd[co * kk + wb4] wq: double = wd[co * kk + wb4]
wbd[wb4 * 4] = wq wbd[wb4 * 4] = wq
wbd[wb4 * 4 + 1] = wq wbd[wb4 * 4 + 1] = wq
wbd[wb4 * 4 + 2] = wq wbd[wb4 * 4 + 2] = wq
wbd[wb4 * 4 + 3] = wq wbd[wb4 * 4 + 3] = wq
for oy in range(hout): for oy in range(int(hout)):
oy0: sizet = oy * stride oy0: sizet = oy * stride
ox: sizet = 0 ox: sizet = 0
while ox < wout: while ox < wout:
@@ -525,14 +525,14 @@ def conv2d_fwd(x: t.CPtr, w: t.CPtr, b: t.CPtr, out: t.CPtr,
t4d[1] = bias t4d[1] = bias
t4d[2] = bias t4d[2] = bias
t4d[3] = bias t4d[3] = bias
for ci in range(cin): for ci in range(int(cin)):
for ky in range(k): for ky in range(int(k)):
iy: sizet = oy0 + ky iy: sizet = oy0 + ky
if iy >= pad: if iy >= pad:
yy: sizet = iy - pad yy: sizet = iy - pad
if yy < hin: if yy < hin:
xrow: sizet = ((bi * cin + ci) * hin + yy) * win xrow: sizet = ((bi * cin + ci) * hin + yy) * win
for kx in range(k): for kx in range(int(k)):
vipersimd.simd_fma4d( vipersimd.simd_fma4d(
_dptr(x, xrow + ox + kx - pad), _dptr(x, xrow + ox + kx - pad),
_dptr(wb, ((ci * k + ky) * k + kx) * 4), _dptr(wb, ((ci * k + ky) * k + kx) * 4),
@@ -547,13 +547,13 @@ def conv2d_fwd(x: t.CPtr, w: t.CPtr, b: t.CPtr, out: t.CPtr,
# ---- scalar: boundary columns / stride != 1 ---- # ---- scalar: boundary columns / stride != 1 ----
ox0: sizet = ox * stride ox0: sizet = ox * stride
acc: double = bias acc: double = bias
for ci2 in range(cin): for ci2 in range(int(cin)):
for ky2 in range(k): for ky2 in range(int(k)):
iy2: sizet = oy0 + ky2 iy2: sizet = oy0 + ky2
if iy2 >= pad: if iy2 >= pad:
yy2: sizet = iy2 - pad yy2: sizet = iy2 - pad
if yy2 < hin: if yy2 < hin:
for kx2 in range(k): for kx2 in range(int(k)):
ix2: sizet = ox0 + kx2 ix2: sizet = ox0 + kx2
if ix2 >= pad: if ix2 >= pad:
xx2: sizet = ix2 - pad xx2: sizet = ix2 - pad
@@ -598,8 +598,8 @@ def conv2d_bwd_dw(x: t.CPtr, dout: t.CPtr, dw: t.CPtr, db: t.CPtr,
simd_on = 0 simd_on = 0
accd: double* = acc accd: double* = acc
for bi in range(n): for bi in range(int(n)):
for co in range(cout): for co in range(int(cout)):
gp: sizet = (bi * cout + co) * hout * wout gp: sizet = (bi * cout + co) * hout * wout
# ---- db[co] += sum(g): the whole channel plane of g is contiguous ---- # ---- db[co] += sum(g): the whole channel plane of g is contiguous ----
if dbd is not None: if dbd is not None:
@@ -628,16 +628,16 @@ def conv2d_bwd_dw(x: t.CPtr, dout: t.CPtr, dw: t.CPtr, db: t.CPtr,
tot0 = tot0 + accd[0] tot0 = tot0 + accd[0]
dbd[co] = dbd[co] + tot0 dbd[co] = dbd[co] + tot0
# ---- dw[co][ci][ky][kx] ---- # ---- dw[co][ci][ky][kx] ----
for ci in range(cin): for ci in range(int(cin)):
for ky in range(k): for ky in range(int(k)):
for kx in range(k): for kx in range(int(k)):
tot: double = double(0.0) tot: double = double(0.0)
if simd_on != 0: if simd_on != 0:
accd[0] = double(0.0) accd[0] = double(0.0)
accd[1] = double(0.0) accd[1] = double(0.0)
accd[2] = double(0.0) accd[2] = double(0.0)
accd[3] = double(0.0) accd[3] = double(0.0)
for oy in range(hout): for oy in range(int(hout)):
iy: sizet = oy * stride + ky iy: sizet = oy * stride + ky
if iy >= pad: if iy >= pad:
yy: sizet = iy - pad yy: sizet = iy - pad
@@ -698,17 +698,17 @@ def conv2d_bwd_dx(w: t.CPtr, dout: t.CPtr, dx: t.CPtr,
if simd_on != 0: if simd_on != 0:
# Weights are constant within one call, so pre-broadcast once (do not repeat vbroadcastsd in the 4-lane loop: 4 stores halves the SIMD gain) # Weights are constant within one call, so pre-broadcast once (do not repeat vbroadcastsd in the 4-lane loop: 4 stores halves the SIMD gain)
wjn: sizet = cout * kk wjn: sizet = cout * kk
for wj4 in range(wjn): for wj4 in range(int(wjn)):
wq2: double = wd[wj4] wq2: double = wd[wj4]
wbd[wj4 * 4] = wq2 wbd[wj4 * 4] = wq2
wbd[wj4 * 4 + 1] = wq2 wbd[wj4 * 4 + 1] = wq2
wbd[wj4 * 4 + 2] = wq2 wbd[wj4 * 4 + 2] = wq2
wbd[wj4 * 4 + 3] = wq2 wbd[wj4 * 4 + 3] = wq2
for bi in range(n): for bi in range(int(n)):
for ci in range(cin): for ci in range(int(cin)):
for ky in range(k): for ky in range(int(k)):
for oy in range(hout): for oy in range(int(hout)):
iy: sizet = oy * stride + ky iy: sizet = oy * stride + ky
if iy >= pad: if iy >= pad:
yy: sizet = iy - pad yy: sizet = iy - pad
@@ -717,8 +717,8 @@ def conv2d_bwd_dx(w: t.CPtr, dout: t.CPtr, dx: t.CPtr,
ox: sizet = 0 ox: sizet = 0
while ox < wout: while ox < wout:
if simd_on != 0 and ox >= pad and ox <= smax and ox + 4 <= wout: if simd_on != 0 and ox >= pad and ox <= smax and ox + 4 <= wout:
for co in range(cout): for co in range(int(cout)):
for kx in range(k): for kx in range(int(k)):
goff: sizet = ((bi * cout + co) * hout + oy) * wout + ox goff: sizet = ((bi * cout + co) * hout + oy) * wout + ox
doff: sizet = dxrow + ox + kx - pad doff: sizet = dxrow + ox + kx - pad
vipersimd.simd_fma4d( vipersimd.simd_fma4d(
@@ -727,8 +727,8 @@ def conv2d_bwd_dx(w: t.CPtr, dout: t.CPtr, dx: t.CPtr,
_dptr(dx, doff), _dptr(dx, doff)) _dptr(dx, doff), _dptr(dx, doff))
ox += 4 ox += 4
else: else:
for co2 in range(cout): for co2 in range(int(cout)):
for kx2 in range(k): for kx2 in range(int(k)):
ix: sizet = ox * stride + kx2 ix: sizet = ox * stride + kx2
if ix >= pad: if ix >= pad:
xx: sizet = ix - pad xx: sizet = ix - pad
@@ -756,19 +756,19 @@ def maxpool2d_fwd(x: t.CPtr, out: t.CPtr, argmax: t.CPtr,
xd: double* = x xd: double* = x
od: double* = out od: double* = out
am: int* = argmax am: int* = argmax
for bi in range(n): for bi in range(int(n)):
for ch in range(c): for ch in range(int(c)):
plano: sizet = (bi * c + ch) * hin * win plano: sizet = (bi * c + ch) * hin * win
for oy in range(hout): for oy in range(int(hout)):
oy0: sizet = oy * stride oy0: sizet = oy * stride
for ox in range(wout): for ox in range(int(wout)):
ox0: sizet = ox * stride ox0: sizet = ox * stride
best: double = double(0.0) best: double = double(0.0)
bidx: int = 0 bidx: int = 0
first: int = 1 first: int = 1
for ky in range(k): for ky in range(int(k)):
yy: sizet = oy0 + ky yy: sizet = oy0 + ky
for kx in range(k): for kx in range(int(k)):
xx: sizet = ox0 + kx xx: sizet = ox0 + kx
v: double = xd[plano + yy * win + xx] v: double = xd[plano + yy * win + xx]
if first != 0 or v > best: if first != 0 or v > best:
@@ -792,11 +792,11 @@ def maxpool2d_bwd(dout: t.CPtr, argmax: t.CPtr, dx: t.CPtr,
am: int* = argmax am: int* = argmax
dxd: double* = dx dxd: double* = dx
string.memset(dx, 0, n * c * hin * win * sizet(8)) string.memset(dx, 0, n * c * hin * win * sizet(8))
for bi in range(n): for bi in range(int(n)):
for ch in range(c): for ch in range(int(c)):
plano: sizet = (bi * c + ch) * hin * win plano: sizet = (bi * c + ch) * hin * win
for oy in range(hout): for oy in range(int(hout)):
for ox in range(wout): for ox in range(int(wout)):
oi: sizet = (bi * c + ch) * hout * wout + oy * wout + ox oi: sizet = (bi * c + ch) * hout * wout + oy * wout + ox
tgt: sizet = plano + sizet(am[oi]) tgt: sizet = plano + sizet(am[oi])
dxd[tgt] = dxd[tgt] + gd[oi] dxd[tgt] = dxd[tgt] + gd[oi]
+8 -8
View File
@@ -197,7 +197,7 @@ import win32.win32dbghelp as dbg
# a compiler intrinsic, has no exported symbol, and is not linkable). # a compiler intrinsic, has no exported symbol, and is not linkable).
# New this round (real Python traceback output): **this module's own declaration** of kernel32!GetEnvironmentVariableA # New this round (real Python traceback output): **this module's own declaration** of kernel32!GetEnvironmentVariableA
# (not declared by win32base; the repo declares it only in VET/App/lib/VDirs.vp:45; kernel32 is linked anyway, so both TPC and # (not declared by win32base; the repo declares it only in VET/App/lib/VDirs.vp:45; kernel32 is linked anyway, so both TPC and
# TPV can resolve it), used for the TRACEBACK_DIAG switch; everything else uses only existing APIs (see section 13). # VPC can resolve it), used for the TRACEBACK_DIAG switch; everything else uses only existing APIs (see section 13).
# #
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# 9. Actionable conclusions for "0 frames" (which case -> which live field to look at) # 9. Actionable conclusions for "0 frames" (which case -> which live field to look at)
@@ -333,7 +333,7 @@ import win32.win32dbghelp as dbg
# C. **Whether the frame pointer is preserved** (investigation conclusion and TPC-side fix location; this module reports only, no change): # C. **Whether the frame pointer is preserved** (investigation conclusion and TPC-side fix location; this module reports only, no change):
# * The single llc call site = `TransPyC/lib/Projectrans/Phase2Translator.py:2160 _llc_compile`, # * The single llc call site = `TransPyC/lib/Projectrans/Phase2Translator.py:2160 _llc_compile`,
# which appends `-frame-pointer=all` to the command when `--traceback` is on (2187-2193); # which appends `-frame-pointer=all` to the command when `--traceback` is on (2187-2193);
# the App module (e.g. `TransPyV/App/lib/core/Gen/Expr/ExprGenOps.vp`, module sha1 # the App module (e.g. `ViperC/App/lib/core/Gen/Expr/ExprGenOps.vp`, module sha1
# `8a96a2885bae09ca`) goes through the **same** call site (2608's _dso.ll / 2319's inlining merge), # `8a96a2885bae09ca`) goes through the **same** call site (2608's _dso.ll / 2319's inlining merge),
# so the flag's location itself is fine. # so the flag's location itself is fine.
# * Cache invalidation: App module obj = `output/{sha1}.obj`, `_compile_ll_files` skips it if it exists # * Cache invalidation: App module obj = `output/{sha1}.obj`, `_compile_ll_files` skips it if it exists
@@ -343,7 +343,7 @@ import win32.win32dbghelp as dbg
# * Delivery conclusion: if `[diag] rbp:` shows `[rbp+8]` is still not a return address (`ret_out_of_module`), # * Delivery conclusion: if `[diag] rbp:` shows `[rbp+8]` is still not a return address (`ret_out_of_module`),
# then `-frame-pointer=all` (a flag at the level of an llc default) did not take effect; the most robust fix is to annotate # then `-frame-pointer=all` (a flag at the level of an llc default) did not take effect; the most robust fix is to annotate
# `"frame-pointer"="all"` explicitly per function **in the IR** -- location # `"frame-pointer"="all"` explicitly per function **in the IR** -- location
# `TransPyV/App/lib/core/Gen/Func/FuncGenBase.vp` (attribute assembly `_append_llvm_attr`, 154-181) # `ViperC/App/lib/core/Gen/Func/FuncGenBase.vp` (attribute assembly `_append_llvm_attr`, 154-181)
# and `TransPyC/lib/core/Handles/HandlesFunctions.py:1735` (next to `func.attributes.add('noredzone')` # and `TransPyC/lib/core/Handles/HandlesFunctions.py:1735` (next to `func.attributes.add('noredzone')`
# ) -- making the frame pointer a **function attribute** rather than an llc command-line default, immune at the root to "whether llc honors the default". # ) -- making the frame pointer a **function attribute** rather than an llc command-line default, immune at the root to "whether llc honors the default".
# * [Extra measurement, Test/LLvmLiteTest Ctrl+C] `RBP=0x0`, RIP in a system DLL: the reject reason is # * [Extra measurement, Test/LLvmLiteTest Ctrl+C] `RBP=0x0`, RIP in a system DLL: the reject reason is
@@ -427,7 +427,7 @@ import win32.win32dbghelp as dbg
# This module needs kernel32!GetEnvironmentVariableA for the TRACEBACK_DIAG switch, but # This module needs kernel32!GetEnvironmentVariableA for the TRACEBACK_DIAG switch, but
# includes/win32/win32base.vp does not declare it (the repository declares it only in # includes/win32/win32base.vp does not declare it (the repository declares it only in
# VET/App/lib/VDirs.vp:45). Declared here with the same style; kernel32 is linked anyway, # VET/App/lib/VDirs.vp:45). Declared here with the same style; kernel32 is linked anyway,
# so both TPC and TPV resolve it. # so both TPC and VPC resolve it.
# DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize); # DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize);
extern def GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: CHARPTR, nSize: DWORD) -> DWORD extern def GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: CHARPTR, nSize: DWORD) -> DWORD
@@ -1431,7 +1431,7 @@ def ScanStack(StartIndex: int, Rsp: uint64, Base: uint64) -> int:
EmitLine(D1) EmitLine(D1)
Stop = "block_read_failed" Stop = "block_read_failed"
break break
for k in range(N): for k in range(int(N)):
W: uint64 = Words[int(k)] W: uint64 = Words[int(k)]
if W >= Base and W < ModEnd: if W >= Base and W < ModEnd:
# A neighbouring 8-byte word (A-8 or A+8) looks like a stack address: accept W. # A neighbouring 8-byte word (A-8 or A+8) looks like a stack address: accept W.
@@ -1446,7 +1446,7 @@ def ScanStack(StartIndex: int, Rsp: uint64, Base: uint64) -> int:
# Deduplicate by candidate value (discovered in ascending stack order, so the # Deduplicate by candidate value (discovered in ascending stack order, so the
# order is kept). # order is kept).
Dup: int = 0 Dup: int = 0
for t_i in range(Hits): for t_i in range(int(Hits)):
if Found[t_i] == W: if Found[t_i] == W:
Dup = 1 Dup = 1
break break
@@ -1758,13 +1758,13 @@ def DumpMainThread(IsBreak: int) -> int:
SavedCount: int = _State.FrameCount SavedCount: int = _State.FrameCount
SaveAddrBuf: [TRACEBACK_FRAME_BUF]uint64 SaveAddrBuf: [TRACEBACK_FRAME_BUF]uint64
SaveScanBuf: [TRACEBACK_FRAME_BUF]int32 SaveScanBuf: [TRACEBACK_FRAME_BUF]int32
for si in range(SavedCount): for si in range(int(SavedCount)):
SaveAddrBuf[si] = _State.FrameAddr[si] SaveAddrBuf[si] = _State.FrameAddr[si]
SaveScanBuf[si] = _State.FrameScan[si] SaveScanBuf[si] = _State.FrameScan[si]
_State.FrameCount = 0 _State.FrameCount = 0
F2: int = WalkViaDbgHelp(wp.GetCurrentProcess(), hThread, _State.ContextBuf, Base) F2: int = WalkViaDbgHelp(wp.GetCurrentProcess(), hThread, _State.ContextBuf, Base)
if F2 == 0: if F2 == 0:
for ri in range(SavedCount): for ri in range(int(SavedCount)):
if FramePush(SaveAddrBuf[ri], int(SaveScanBuf[ri])) == 0: if FramePush(SaveAddrBuf[ri], int(SaveScanBuf[ri])) == 0:
break break
# The count that is actually buffered is authoritative. # The count that is actually buffered is authoritative.
+1 -1
View File
@@ -21,7 +21,7 @@ class Buf:
avail: sizet = self.capacity - self.length avail: sizet = self.capacity - self.length
if avail <= 0: return 0 if avail <= 0: return 0
n: sizet = count if count < avail else avail n: sizet = count if count < avail else avail
for i in range(n): for i in range(int(n)):
self.data[self.length + i] = src[i] self.data[self.length + i] = src[i]
self.length += n self.length += n
# When exactly full (n==avail), data[capacity] would overflow by 1 byte, so guard it # When exactly full (n==avail), data[capacity] would overflow by 1 byte, so guard it
+1 -1
View File
@@ -623,7 +623,7 @@ vzeroupper""",
op=[ASM_DESCR.CLOBBER_MEMORY, 'ymm0', 'ymm2', 'rcx', 'r8', 'r10']) op=[ASM_DESCR.CLOBBER_MEMORY, 'ymm0', 'ymm2', 'rcx', 'r8', 'r10'])
def simd_dot_array(a: double*, b: double*, n: sizet, out: double): def simd_dot_array(a: double*, b: double*, n: sizet, out: double*):
"""AVX2 batch: out[0] = dot(a[0:n], b[0:n]) using FMA accumulation""" """AVX2 batch: out[0] = dot(a[0:n], b[0:n]) using FMA accumulation"""
__asm__(f"""mov rcx, {asminp(n, ASM_DESCR.REG_ANY)} __asm__(f"""mov rcx, {asminp(n, ASM_DESCR.REG_ANY)}
shr rcx, 2 shr rcx, 2
+4 -4
View File
@@ -96,10 +96,10 @@ def qtree_checked_count(tw: QtTreeWidgetPtr) -> int:
"""Count checked leaf nodes.""" """Count checked leaf nodes."""
n: int = 0 n: int = 0
top_n: int = vqt6_qtreewidget_top_level_item_count(tw) top_n: int = vqt6_qtreewidget_top_level_item_count(tw)
for gi in range(top_n): for gi in range(int(top_n)):
g: QtTreeWidgetItemPtr = vqt6_qtreewidget_top_level_item(tw, gi) g: QtTreeWidgetItemPtr = vqt6_qtreewidget_top_level_item(tw, gi)
cn: int = vqt6_qtreewidgetitem_child_count(g) cn: int = vqt6_qtreewidgetitem_child_count(g)
for ci in range(cn): for ci in range(int(cn)):
it: QtTreeWidgetItemPtr = vqt6_qtreewidgetitem_child(g, ci) it: QtTreeWidgetItemPtr = vqt6_qtreewidgetitem_child(g, ci)
if vqt6_qtreewidgetitem_check_state(it) == 2: if vqt6_qtreewidgetitem_check_state(it) == 2:
n += 1 n += 1
@@ -110,10 +110,10 @@ def qtree_checked_size(tw: QtTreeWidgetPtr) -> int:
"""Sum disk usage (MB) of checked leaf nodes.""" """Sum disk usage (MB) of checked leaf nodes."""
mb: int = 0 mb: int = 0
top_n: int = vqt6_qtreewidget_top_level_item_count(tw) top_n: int = vqt6_qtreewidget_top_level_item_count(tw)
for gi in range(top_n): for gi in range(int(top_n)):
g: QtTreeWidgetItemPtr = vqt6_qtreewidget_top_level_item(tw, gi) g: QtTreeWidgetItemPtr = vqt6_qtreewidget_top_level_item(tw, gi)
cn: int = vqt6_qtreewidgetitem_child_count(g) cn: int = vqt6_qtreewidgetitem_child_count(g)
for ci in range(cn): for ci in range(int(cn)):
it: QtTreeWidgetItemPtr = vqt6_qtreewidgetitem_child(g, ci) it: QtTreeWidgetItemPtr = vqt6_qtreewidgetitem_child(g, ci)
if vqt6_qtreewidgetitem_check_state(it) == 2: if vqt6_qtreewidgetitem_check_state(it) == 2:
mb += vqt6_qtreewidgetitem_data_int(it) mb += vqt6_qtreewidgetitem_data_int(it)
+3 -3
View File
@@ -140,7 +140,7 @@ def zf_write_all(h: wb.HANDLE, p: uint8*, n: sizet) -> int:
if chunk > 0xFFFFFF00: if chunk > 0xFFFFFF00:
chunk = 0xFFFFFF00 chunk = 0xFFFFFF00
written: ulong = 0 written: ulong = 0
ok: int = wf.WriteFile(h, p + done, ulong(chunk), &written, None) ok: int = int(wf.WriteFile(h, p + done, ulong(chunk), &written, None))
if ok == 0 or written == 0: if ok == 0 or written == 0:
global zipfile_error_code_val, zipfile_error_msg global zipfile_error_code_val, zipfile_error_msg
zipfile_error_code_val = ZIP_ERR_IO zipfile_error_code_val = ZIP_ERR_IO
@@ -157,7 +157,7 @@ def zf_read_all(h: wb.HANDLE, p: uint8*, n: sizet) -> int:
if chunk > 0xFFFFFF00: if chunk > 0xFFFFFF00:
chunk = 0xFFFFFF00 chunk = 0xFFFFFF00
bytes_read: ulong = 0 bytes_read: ulong = 0
ok: int = wf.ReadFile(h, p + done, ulong(chunk), &bytes_read, None) ok: int = int(wf.ReadFile(h, p + done, ulong(chunk), &bytes_read, None))
if ok == 0 or bytes_read == 0: if ok == 0 or bytes_read == 0:
global zipfile_error_code_val, zipfile_error_msg global zipfile_error_code_val, zipfile_error_msg
zipfile_error_code_val = ZIP_ERR_IO zipfile_error_code_val = ZIP_ERR_IO
@@ -330,7 +330,7 @@ class ZipFile:
cp: sizet = sizet(cd_off) cp: sizet = sizet(cd_off)
end: sizet = sizet(cd_off) + sizet(cd_size) end: sizet = sizet(cd_off) + sizet(cd_size)
for idx in range(total): for idx in range(int(total)):
if cp + 46 > end: if cp + 46 > end:
zipfile_set_error(ZIP_ERR_NOT_ZIP, "central directory entry truncated") zipfile_set_error(ZIP_ERR_NOT_ZIP, "central directory entry truncated")
return ZIP_ERR_NOT_ZIP return ZIP_ERR_NOT_ZIP
+2 -2
View File
@@ -8,7 +8,7 @@ def zchecksum_adler32(data: UINT8PTR, length: sizet, init: UINT32) -> uint32:
if not data or length == 0: if not data or length == 0:
return init return init
i: sizet i: sizet
for i in range(length): for i in range(int(length)):
a = (a + data[i]) % 65521 a = (a + data[i]) % 65521
b = (b + a) % 65521 b = (b + a) % 65521
return (b << 16) | a return (b << 16) | a
@@ -84,6 +84,6 @@ def zchecksum_crc32(data: UINT8PTR, length: sizet, init: uint32) -> uint32:
crc: uint32 = init ^ 0xFFFFFFFF crc: uint32 = init ^ 0xFFFFFFFF
if not data or length == 0: return init if not data or length == 0: return init
i: sizet i: sizet
for i in range(length): for i in range(int(length)):
crc = crc32_table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8) crc = crc32_table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8)
return crc ^ 0xFFFFFFFF return crc ^ 0xFFFFFFFF
+5 -5
View File
@@ -129,7 +129,7 @@ class zbit_writer:
def write_bits(self, value: UINT, nbits: int): def write_bits(self, value: UINT, nbits: int):
self.ensure((nbits + 7) / 8 + 1) self.ensure((nbits + 7) / 8 + 1)
for i in range(nbits): for i in range(int(nbits)):
if value & (UINT(1) << i): if value & (UINT(1) << i):
self.buf[self.byte_pos] |= (UINT(1) << self.bit_pos) self.buf[self.byte_pos] |= (UINT(1) << self.bit_pos)
self.bit_pos += 1 self.bit_pos += 1
@@ -139,7 +139,7 @@ class zbit_writer:
def write_bits_rev(self, code: UINT, nbits: int): def write_bits_rev(self, code: UINT, nbits: int):
self.ensure((nbits + 7) / 8 + 1) self.ensure((nbits + 7) / 8 + 1)
for i in range(nbits - 1, -1, -1): for i in range(int(nbits - 1), -1, -1):
if code & (UINT(1) << i): if code & (UINT(1) << i):
self.buf[self.byte_pos] |= (UINT(1) << self.bit_pos) self.buf[self.byte_pos] |= (UINT(1) << self.bit_pos)
self.bit_pos += 1 self.bit_pos += 1
@@ -164,7 +164,7 @@ class zbit_writer:
self.ensure(block_length) self.ensure(block_length)
i: sizet = 0 i: sizet = 0
for i in range(block_length): for i in range(int(block_length)):
self.buf[self.byte_pos] = data[pos + i] self.buf[self.byte_pos] = data[pos + i]
self.byte_pos += 1 self.byte_pos += 1
pos += block_length pos += block_length
@@ -242,7 +242,7 @@ class zbit_reader:
def read_bits(self, nbits: int, out: UINTPTR) -> int: def read_bits(self, nbits: int, out: UINTPTR) -> int:
val: UINT = 0 val: UINT = 0
for i in range(nbits): for i in range(int(nbits)):
if self.byte_pos >= self.length: return -1 if self.byte_pos >= self.length: return -1
if self.buf[self.byte_pos] & (UINT(1) << self.bit_pos): if self.buf[self.byte_pos] & (UINT(1) << self.bit_pos):
val |= (UINT(1) << i) val |= (UINT(1) << i)
@@ -255,7 +255,7 @@ class zbit_reader:
def read_bits_rev(self, nbits: int, out: UINTPTR) -> int: def read_bits_rev(self, nbits: int, out: UINTPTR) -> int:
val: UINT = 0 val: UINT = 0
for i in range(nbits - 1, 0, -1): for i in range(int(nbits - 1), 0, -1):
if self.byte_pos >= self.length: return -1 if self.byte_pos >= self.length: return -1
if self.buf[self.byte_pos] & (UINT(1) << self.bit_pos): if self.buf[self.byte_pos] & (UINT(1) << self.bit_pos):
val |= (UINT(1) << i) val |= (UINT(1) << i)
+6 -6
View File
@@ -82,7 +82,7 @@ class zdeflate_stream:
extra = zdef.zdeflate_dist_extra_bits[dist_sym] extra = zdef.zdeflate_dist_extra_bits[dist_sym]
if extra > 0: if extra > 0:
self.writer.write_bits(best_dist - zdef.zdeflate_dist_base[dist_sym], extra) self.writer.write_bits(best_dist - zdef.zdeflate_dist_base[dist_sym], extra)
for i in range(match_len): for i in range(int(match_len)):
self.update_hash(data, pos + i, length) self.update_hash(data, pos + i, length)
pos += match_len pos += match_len
else: else:
@@ -110,7 +110,7 @@ class zdeflate_stream:
extra = zdef.zdeflate_dist_extra_bits[dist_sym] extra = zdef.zdeflate_dist_extra_bits[dist_sym]
if extra > 0: if extra > 0:
self.writer.write_bits(best_dist - zdef.zdeflate_dist_base[dist_sym], extra) self.writer.write_bits(best_dist - zdef.zdeflate_dist_base[dist_sym], extra)
for j in range(match_len): for j in range(int(match_len)):
self.update_hash(data, pos + j, length) self.update_hash(data, pos + j, length)
pos += match_len pos += match_len
else: else:
@@ -133,8 +133,8 @@ class zdeflate_stream:
lit_tree.build_codes(lit_freqs, hlit, zdef.ZDEFLATE_MAX_BITS) lit_tree.build_codes(lit_freqs, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_tree.build_codes(dist_freqs, hdist, zdef.ZDEFLATE_MAX_BITS) dist_tree.build_codes(dist_freqs, hdist, zdef.ZDEFLATE_MAX_BITS)
all_lengths: t.CArray[int, 288 + 32] all_lengths: t.CArray[int, 288 + 32]
for i in range(hlit): all_lengths[i] = lit_tree.codes[i].bits for i in range(int(hlit)): all_lengths[i] = lit_tree.codes[i].bits
for i in range(hdist): all_lengths[hlit + i] = dist_tree.codes[i].bits for i in range(int(hdist)): all_lengths[hlit + i] = dist_tree.codes[i].bits
total_lengths: int = hlit + hdist total_lengths: int = hlit + hdist
cl_freqs: t.CArray[int, 19] cl_freqs: t.CArray[int, 19]
zdeflate_count_cl_freqs(all_lengths, total_lengths, cl_freqs) zdeflate_count_cl_freqs(all_lengths, total_lengths, cl_freqs)
@@ -147,7 +147,7 @@ class zdeflate_stream:
self.writer.write_bits(hlit - 257, 5) self.writer.write_bits(hlit - 257, 5)
self.writer.write_bits(hdist - 1, 5) self.writer.write_bits(hdist - 1, 5)
self.writer.write_bits(hclen - 4, 4) self.writer.write_bits(hclen - 4, 4)
for j in range(hclen): for j in range(int(hclen)):
self.writer.write_bits(cl_tree.codes[zdef.zdeflate_codelen_order[j]].bits, 3) self.writer.write_bits(cl_tree.codes[zdef.zdeflate_codelen_order[j]].bits, 3)
zdeflate_write_cl_encoded(all_lengths, total_lengths, &self.writer, &cl_tree) zdeflate_write_cl_encoded(all_lengths, total_lengths, &self.writer, &cl_tree)
self.encode_block_data(data, length, &lit_tree, &dist_tree) self.encode_block_data(data, length, &lit_tree, &dist_tree)
@@ -271,7 +271,7 @@ def zdeflate_count_freqs(lit_freqs: INTPTR, dist_freqs: INTPTR,
len_sym: int = zdeflate_len_to_symbol(match_len) len_sym: int = zdeflate_len_to_symbol(match_len)
lit_freqs[len_sym] += 1 lit_freqs[len_sym] += 1
dist_freqs[zdeflate_dist_to_symbol(best_dist)] += 1 dist_freqs[zdeflate_dist_to_symbol(best_dist)] += 1
for i in range(match_len): for i in range(int(match_len)):
zdeflate_stream.update_hash(s, data, pos + i, length) zdeflate_stream.update_hash(s, data, pos + i, length)
pos += match_len pos += match_len
else: else:
+28 -28
View File
@@ -31,15 +31,15 @@ class zhuff_tree:
self.max_bits = max_bits self.max_bits = max_bits
self.build_code_lengths(lengths, freqs, count, max_bits) self.build_code_lengths(lengths, freqs, count, max_bits)
string.memset(bl_count, 0, bl_count.__sizeof__()) string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count): for i in range(int(count)):
if lengths[i] > 0: if lengths[i] > 0:
bl_count[lengths[i]] += 1 bl_count[lengths[i]] += 1
code: uint = 0 code: uint = 0
next_code[0] = 0 next_code[0] = 0
for bits in range(1, max_bits + 1): for bits in range(1, int(max_bits + 1)):
code = (code + bl_count[bits - 1]) << 1 code = (code + bl_count[bits - 1]) << 1
next_code[bits] = code next_code[bits] = code
for i in range(count): for i in range(int(count)):
self.codes[i].bits = lengths[i] self.codes[i].bits = lengths[i]
if lengths[i] > 0: if lengths[i] > 0:
self.codes[i].code = next_code[lengths[i]] self.codes[i].code = next_code[lengths[i]]
@@ -58,7 +58,7 @@ class zhuff_tree:
bl_count[lengths[i]] += 1 bl_count[lengths[i]] += 1
code: uint = 0 code: uint = 0
next_code[0] = 0 next_code[0] = 0
for bits in range(1, 9 + 1): for bits in range(1, int(9 + 1)):
code = (code + bl_count[bits - 1]) << 1 code = (code + bl_count[bits - 1]) << 1
next_code[bits] = code next_code[bits] = code
self.count = 288 self.count = 288
@@ -82,7 +82,7 @@ class zhuff_tree:
bl_count[lengths[i]] += 1 bl_count[lengths[i]] += 1
code: uint = 0 code: uint = 0
next_code[0] = 0 next_code[0] = 0
for bits in range(1, 5 + 1): for bits in range(1, int(5 + 1)):
code = (code + bl_count[bits - 1]) << 1 code = (code + bl_count[bits - 1]) << 1
next_code[bits] = code next_code[bits] = code
self.count = 32 self.count = 32
@@ -108,24 +108,24 @@ class zhuff_tree:
sort_count: int = 0 sort_count: int = 0
string.memset(bl_count, 0, bl_count.__sizeof__()) string.memset(bl_count, 0, bl_count.__sizeof__())
string.memset(lengths, 0, int.__sizeof__() * count) string.memset(lengths, 0, int.__sizeof__() * count)
for i in range(count): for i in range(int(count)):
if freqs[i] > 0: if freqs[i] > 0:
sort_count += 1 sort_count += 1
if sort_count == 0: return if sort_count == 0: return
if sort_count == 1: if sort_count == 1:
for i in range(count): for i in range(int(count)):
if freqs[i] > 0: if freqs[i] > 0:
lengths[i] = 1 lengths[i] = 1
return return
items: int* = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__())) items: int* = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__()))
item_freqs: int* = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__())) item_freqs: int* = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__()))
idx: int = 0 idx: int = 0
for i in range(count): for i in range(int(count)):
if freqs[i] > 0: if freqs[i] > 0:
items[idx] = i items[idx] = i
item_freqs[idx] = freqs[i] item_freqs[idx] = freqs[i]
idx += 1 idx += 1
for i in range(sort_count): for i in range(int(sort_count)):
key_item: int = items[i] key_item: int = items[i]
key_freq: int = item_freqs[i] key_freq: int = item_freqs[i]
j: int = i - 1 j: int = i - 1
@@ -136,11 +136,11 @@ class zhuff_tree:
items[j + 1] = key_item items[j + 1] = key_item
item_freqs[j + 1] = key_freq item_freqs[j + 1] = key_freq
parent: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count * 2) * int.__sizeof__())) parent: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count * 2) * int.__sizeof__()))
for i in range(sort_count * 2): for i in range(int(sort_count * 2)):
parent[i] = -1 parent[i] = -1
heap: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count + 1) * int.__sizeof__())) heap: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count + 1) * int.__sizeof__()))
heap_size: int = 0 heap_size: int = 0
for i in range(sort_count): for i in range(int(sort_count)):
heap[heap_size] = i heap[heap_size] = i
heap_size += 1 heap_size += 1
pos: int = heap_size - 1 pos: int = heap_size - 1
@@ -153,7 +153,7 @@ class zhuff_tree:
pos = par pos = par
else: break else: break
combined_freq: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count * 2) * int.__sizeof__())) combined_freq: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, (sort_count * 2) * int.__sizeof__()))
for i in range(sort_count): for i in range(int(sort_count)):
combined_freq[i] = item_freqs[i] combined_freq[i] = item_freqs[i]
internal: int = sort_count internal: int = sort_count
while heap_size > 1: while heap_size > 1:
@@ -209,7 +209,7 @@ class zhuff_tree:
pos = par pos = par
else: break else: break
internal += 1 internal += 1
for i in range(sort_count): for i in range(int(sort_count)):
node: int = i node: int = i
length: int = 0 length: int = 0
while parent[node] >= 0: while parent[node] >= 0:
@@ -217,11 +217,11 @@ class zhuff_tree:
node = parent[node] node = parent[node]
lengths[items[i]] = length lengths[items[i]] = length
string.memset(bl_count, 0, bl_count.__sizeof__()) string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count): for i in range(int(count)):
if lengths[i] > 0: if lengths[i] > 0:
bl_count[lengths[i]] += 1 bl_count[lengths[i]] += 1
overflow: int = 0 overflow: int = 0
for i in range(count): for i in range(int(count)):
if lengths[i] > max_bits: if lengths[i] > max_bits:
overflow += 1 overflow += 1
bl_count[lengths[i]] -= 1 bl_count[lengths[i]] -= 1
@@ -238,18 +238,18 @@ class zhuff_tree:
overflow -= 2 overflow -= 2
sorted_items: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__())) sorted_items: INTPTR = INTPTR(zdef.zdef_alloc(self.pool, sort_count * int.__sizeof__()))
si: int = 0 si: int = 0
for i in range(count): for i in range(int(count)):
if freqs[i] > 0: if freqs[i] > 0:
sorted_items[si] = i sorted_items[si] = i
si += 1 si += 1
for i in range(si - 1): for i in range(int(si - 1)):
for j in range(i + 1, si): for j in range(int(i + 1), int(si)):
if lengths[sorted_items[i]] < lengths[sorted_items[j]]: if lengths[sorted_items[i]] < lengths[sorted_items[j]]:
tmp: int = sorted_items[i] tmp: int = sorted_items[i]
sorted_items[i] = sorted_items[j] sorted_items[i] = sorted_items[j]
sorted_items[j] = tmp sorted_items[j] = tmp
sidx: int = 0 sidx: int = 0
for bits in range(max_bits, 1, -1): for bits in range(int(max_bits), 1, -1):
n: int = bl_count[bits] n: int = bl_count[bits]
while n > 0 and sidx < si: while n > 0 and sidx < si:
lengths[sorted_items[sidx]] = bits lengths[sorted_items[sidx]] = bits
@@ -263,10 +263,10 @@ class zhuff_tree:
zdef.zdef_free(self.pool, items) zdef.zdef_free(self.pool, items)
def get_fixed_lit_lengths(self, lengths: INTPTR): def get_fixed_lit_lengths(self, lengths: INTPTR):
for i in range( 0, 143 + 1): lengths[i] = 8 for i in range( 0, int(143 + 1)): lengths[i] = 8
for i in range(143 + 1, 255 + 1): lengths[i] = 9 for i in range(int(143 + 1), int(255 + 1)): lengths[i] = 9
for i in range(255 + 1, 279 + 1): lengths[i] = 7 for i in range(int(255 + 1), int(279 + 1)): lengths[i] = 7
for i in range(279 + 1, 287 + 1): lengths[i] = 8 for i in range(int(279 + 1), int(287 + 1)): lengths[i] = 8
def get_fixed_dist_lengths(self, lengths: INTPTR): def get_fixed_dist_lengths(self, lengths: INTPTR):
for i in range(32): for i in range(32):
@@ -277,19 +277,19 @@ class zhuff_tree:
next_code: t.CArray[uint, 16] next_code: t.CArray[uint, 16]
string.memset(bl_count, 0, bl_count.__sizeof__()) string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count): for i in range(int(count)):
if lengths[i] > 0: if lengths[i] > 0:
bl_count[lengths[i]] += 1 bl_count[lengths[i]] += 1
code: UINT = 0 code: UINT = 0
next_code[0] = 0 next_code[0] = 0
for bits in range(1, max_bits + 1): for bits in range(1, int(max_bits + 1)):
code = (code + bl_count[bits - 1]) << 1 code = (code + bl_count[bits - 1]) << 1
next_code[bits] = code next_code[bits] = code
self.count = count self.count = count
self.max_bits = max_bits self.max_bits = max_bits
for i in range(count): for i in range(int(count)):
self.codes[i].bits = lengths[i] self.codes[i].bits = lengths[i]
if lengths[i] > 0: if lengths[i] > 0:
self.codes[i].code = next_code[lengths[i]] self.codes[i].code = next_code[lengths[i]]
@@ -318,10 +318,10 @@ class zhuff_decode_tree:
self.nodes[0].children[0] = -1 self.nodes[0].children[0] = -1
self.nodes[0].children[1] = -1 self.nodes[0].children[1] = -1
self.nodes[0].symbol = -1 self.nodes[0].symbol = -1
for i in range(ht.count): for i in range(int(ht.count)):
if ht.codes[i].bits == 0: continue if ht.codes[i].bits == 0: continue
node: int = 0 node: int = 0
for bit in range(ht.codes[i].bits - 1, -1, -1): for bit in range(int(ht.codes[i].bits - 1), -1, -1):
dir: int = (ht.codes[i].code >> bit) & 1 dir: int = (ht.codes[i].code >> bit) & 1
if bit > 0: if bit > 0:
# Internal node: traverse or create # Internal node: traverse or create
+11 -11
View File
@@ -96,7 +96,7 @@ class zinflate_stream:
def read_bits_from_input(self, nbits: int, out: UINTPTR) -> int: def read_bits_from_input(self, nbits: int, out: UINTPTR) -> int:
val: UINT = 0 val: UINT = 0
for i in range(nbits): for i in range(int(nbits)):
if self.input_pos >= self.input_len: return -3 if self.input_pos >= self.input_len: return -3
if self.input_data[self.input_pos] & (UINT(1) << (self.reader.bit_pos)): if self.input_data[self.input_pos] & (UINT(1) << (self.reader.bit_pos)):
val |= (UINT(1) << i) val |= (UINT(1) << i)
@@ -129,7 +129,7 @@ class zinflate_stream:
if (length ^ nlen) != 0xFFFF: return -3 if (length ^ nlen) != 0xFFFF: return -3
if self.input_pos + length > self.input_len: return -3 if self.input_pos + length > self.input_len: return -3
i: uint i: uint
for i in range(length): for i in range(int(length)):
self.output_byte(self.input_data[self.input_pos]) self.output_byte(self.input_data[self.input_pos])
self.input_pos += 1 self.input_pos += 1
return 0 return 0
@@ -190,7 +190,7 @@ class zinflate_stream:
break break
dist += extra_val dist += extra_val
for i in range(length): for i in range(int(length)):
if self.max_length > 0 and self.output_len >= self.max_length: break if self.max_length > 0 and self.output_len >= self.max_length: break
src_pos: int = (self.window_pos - dist) & (zdef.ZDEFLATE_WINDOW_SIZE - 1) src_pos: int = (self.window_pos - dist) & (zdef.ZDEFLATE_WINDOW_SIZE - 1)
byte: BYTE = self.window[src_pos] byte: BYTE = self.window[src_pos]
@@ -215,7 +215,7 @@ class zinflate_stream:
cl_lengths: t.CArray[int, 19] cl_lengths: t.CArray[int, 19]
string.memset(cl_lengths, 0, cl_lengths.__sizeof__()) string.memset(cl_lengths, 0, cl_lengths.__sizeof__())
for i in range(hclen): for i in range(int(hclen)):
len_val: UINT len_val: UINT
if self.read_bits_from_input(3, &len_val) != 0: return -3 if self.read_bits_from_input(3, &len_val) != 0: return -3
cl_lengths[zdef.zdeflate_codelen_order[i]] = int(len_val) cl_lengths[zdef.zdeflate_codelen_order[i]] = int(len_val)
@@ -250,7 +250,7 @@ class zinflate_stream:
return -3 return -3
prev: int = all_lengths[idx - 1] prev: int = all_lengths[idx - 1]
i: uint i: uint
for i in range(rep): for i in range(int(rep)):
all_lengths[idx] = prev all_lengths[idx] = prev
idx += 1 idx += 1
elif sym == 17: elif sym == 17:
@@ -263,7 +263,7 @@ class zinflate_stream:
zdef.zdef_free(self.pool, all_lengths) zdef.zdef_free(self.pool, all_lengths)
return -3 return -3
i: uint i: uint
for i in range(rep): for i in range(int(rep)):
all_lengths[idx] = 0 all_lengths[idx] = 0
idx += 1 idx += 1
else: else:
@@ -276,7 +276,7 @@ class zinflate_stream:
zdef.zdef_free(self.pool, all_lengths) zdef.zdef_free(self.pool, all_lengths)
return -3 return -3
i: uint i: uint
for i in range(rep): for i in range(int(rep)):
all_lengths[idx] = 0 all_lengths[idx] = 0
idx += 1 idx += 1
@@ -285,13 +285,13 @@ class zinflate_stream:
lit_lengths: t.CArray[int, 288] lit_lengths: t.CArray[int, 288]
string.memset(lit_lengths, 0, lit_lengths.__sizeof__()) string.memset(lit_lengths, 0, lit_lengths.__sizeof__())
for i in range(hlit): for i in range(int(hlit)):
lit_lengths[i] = all_lengths[i] lit_lengths[i] = all_lengths[i]
lit_tree.build_tree_from_lengths(lit_lengths, hlit, zdef.ZDEFLATE_MAX_BITS) lit_tree.build_tree_from_lengths(lit_lengths, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_lengths: t.CArray[int, 32] dist_lengths: t.CArray[int, 32]
string.memset(dist_lengths, 0, dist_lengths.__sizeof__()) string.memset(dist_lengths, 0, dist_lengths.__sizeof__())
for i in range(hdist): for i in range(int(hdist)):
dist_lengths[i] = all_lengths[hlit + i] dist_lengths[i] = all_lengths[hlit + i]
dist_tree.build_tree_from_lengths(dist_lengths, hdist, zdef.ZDEFLATE_MAX_BITS) dist_tree.build_tree_from_lengths(dist_lengths, hdist, zdef.ZDEFLATE_MAX_BITS)
@@ -326,7 +326,7 @@ class zinflate_stream:
extra_val: UINT extra_val: UINT
if self.read_bits_from_input(extra, &extra_val) != 0: return -3 if self.read_bits_from_input(extra, &extra_val) != 0: return -3
dist += extra_val dist += extra_val
for i in range(length): for i in range(int(length)):
if self.max_length > 0 and self.output_len >= self.max_length: return 0 if self.max_length > 0 and self.output_len >= self.max_length: return 0
src_pos: int = (self.window_pos - dist) & (zdef.ZDEFLATE_WINDOW_SIZE - 1) src_pos: int = (self.window_pos - dist) & (zdef.ZDEFLATE_WINDOW_SIZE - 1)
byte: BYTE = self.window[src_pos] byte: BYTE = self.window[src_pos]
@@ -435,7 +435,7 @@ def zinflate_one_shot(pool: memhub.MemManager*, data: UINT8PTR, length: sizet,
s: zinflate_stream* = zinflate_create(pool, wbits) s: zinflate_stream* = zinflate_create(pool, wbits)
if not s: return None if not s: return None
out: uint8* = None out: uint8* = None
err: int = s.decompress(data, length, 0, &out, out_len) err: int = int(s.decompress(data, length, 0, &out, out_len))
s.destroy() s.destroy()
if err != 0: return None if err != 0: return None
return out return out