snapshot before regression test

This commit is contained in:
t
2026-07-18 19:25:40 +08:00
commit 796222a300
2295 changed files with 206453 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
from stdint import *
import w32.win32console
import config
import hashlib
import binascii
import zlib.pyzlib as zp
import t, c
import testcheck
def main() -> t.CInt | t.CExport:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("TestProject2: 综合库测试")
testcheck.section("zlib runtime version")
print(zp.runtime_version())
testcheck.ok("zlib runtime version printed")
testcheck.section("HASHLIB")
md5_buf: t.CArray[t.CUInt8T, hashlib.MD5_DIGEST_LEN]
mctx = hashlib.md5()
mctx.update("hello")
mctx.final(md5_buf)
testcheck.ok("HASHLIB md5 completed")
testcheck.section("BINASCII")
binascii.init_binascii()
crc_val: t.CUnsignedInt = binascii.crc32("hello", 5, 0)
testcheck.ok("BINASCII crc32 completed")
testcheck.info("END")
return testcheck.end()

View File

@@ -0,0 +1,245 @@
import stdio
import stdint
import t, c
class SimpleData(t.Object):
x: t.CInt
y: t.CInt
class Point3D(t.Object):
x: t.CFloat
y: t.CFloat
z: t.CFloat
class Student(t.Object):
name: t.CChar | t.CPtr
age: t.CInt
score: t.CInt
def __init__(self, n: t.CChar | t.CPtr, a: t.CInt, s: t.CInt):
self.name = n
self.age = a
self.score = s
def get_age(self) -> t.CInt:
return self.age
def get_score(self) -> t.CInt:
return self.score
class Counter(t.Object):
value: t.CInt
def __init__(self, initial: t.CInt):
self.value = initial
def increment(self):
self.value += 1
def get_value(self) -> t.CInt:
return self.value
class StackObj(t.Object):
data: t.CInt
next_ptr: t.CVoid | t.CPtr
def TestBasicDataClass():
stdio.printf("=== Test Basic Data Class ===\n")
p: SimpleData = SimpleData()
p.x = 10
p.y = 20
stdio.printf("SimpleData: x=%d, y=%d\n", p.x, p.y)
def TestPoint3DClass():
stdio.printf("=== Test Point3D Class ===\n")
pt: Point3D = Point3D()
pt.x = 1.5
pt.y = 2.5
pt.z = 3.5
stdio.printf("Point3D: x=%.1f, y=%.1f, z=%.1f\n", pt.x, pt.y, pt.z)
def TestStudentClass():
stdio.printf("=== Test Student Class ===\n")
s: Student = Student("Alice", 20, 95)
stdio.printf("Student: name=%s, age=%d, score=%d\n", s.name, s.age, s.score)
stdio.printf(" get_age()=%d, get_score()=%d\n", s.get_age(), s.get_score())
def TestCounterClass():
stdio.printf("=== Test Counter Class ===\n")
cnt: Counter = Counter(0)
stdio.printf("Initial value: %d\n", cnt.get_value())
cnt.increment()
cnt.increment()
cnt.increment()
stdio.printf("After 3 increments: %d\n", cnt.get_value())
def TestStackAllocation():
stdio.printf("=== Test Stack Allocation ===\n")
obj1: SimpleData = SimpleData()
obj1.x = 100
obj1.y = 200
stdio.printf("Stack obj1: x=%d, y=%d\n", obj1.x, obj1.y)
obj2: Point3D = Point3D()
obj2.x = 10.0
obj2.y = 20.0
obj2.z = 30.0
stdio.printf("Stack obj2: x=%.1f, y=%.1f, z=%.1f\n", obj2.x, obj2.y, obj2.z)
def TestHeapAllocation():
stdio.printf("=== Test Heap Allocation ===\n")
ptr: t.CVoid | t.CPtr = c.malloc(1024)
if ptr != 0:
stdio.printf("Allocated 1024 bytes at %p\n", ptr)
c.free(ptr)
stdio.printf("Freed memory\n")
else:
stdio.printf("malloc failed\n")
def TestHeapObjectWithInit():
stdio.printf("=== Test Heap Object with Init ===\n")
mem_size: t.CSizeT = 256
ptr: t.CVoid | t.CPtr = c.malloc(mem_size)
if ptr != 0:
stdio.printf("Allocated %d bytes\n", mem_size)
c.free(ptr)
else:
stdio.printf("malloc failed\n")
def TestMultipleStackObjects():
stdio.printf("=== Test Multiple Stack Objects ===\n")
objs: SimpleData = SimpleData()
objs.x = 1
objs.y = 2
obj2: Point3D = Point3D()
obj2.x = 5.0
obj2.y = 10.0
obj2.z = 15.0
obj3: Counter = Counter(42)
stdio.printf("objs: x=%d, y=%d\n", objs.x, objs.y)
stdio.printf("obj2: x=%.1f, y=%.1f, z=%.1f\n", obj2.x, obj2.y, obj2.z)
stdio.printf("obj3 initial: %d\n", obj3.get_value())
obj3.increment()
obj3.increment()
stdio.printf("obj3 after 2 increments: %d\n", obj3.get_value())
def TestImplicitInitCall():
stdio.printf("=== Test Implicit Init Call ===\n")
stu1: Student = Student("Bob", 22, 88)
stu2: Student = Student("Charlie", 19, 77)
stdio.printf("stu1: name=%s, age=%d, score=%d\n", stu1.name, stu1.age, stu1.score)
stdio.printf("stu2: name=%s, age=%d, score=%d\n", stu2.name, stu2.age, stu2.score)
def TestClassWithBitFields():
stdio.printf("=== Test Class with Bit Fields ===\n")
flags: StackObj = StackObj()
flags.data = 0
flags.next_ptr = 0
stdio.printf("StackObj: data=%d, next_ptr=%p\n", flags.data, flags.next_ptr)
def TestIntArray():
stdio.printf("=== Test Int Array ===\n")
arr: t.CArray[t.CInt, 5] = t.CArray[t.CInt, 5]()
i: t.CInt = 0
while i < 5:
arr[i] = i * 10
i += 1
i = 0
while i < 5:
stdio.printf("arr[%d]=%d\n", i, arr[i])
i += 1
def TestFloatArray():
stdio.printf("=== Test Float Array ===\n")
farr: t.CArray[t.CFloat, 4] = t.CArray[t.CFloat, 4]()
farr[0] = 1.5
farr[1] = 2.5
farr[2] = 3.5
farr[3] = 4.5
i: t.CInt = 0
while i < 4:
stdio.printf("farr[%d]=%.1f\n", i, farr[i])
i += 1
def TestCharArray():
stdio.printf("=== Test Char Array (String) ===\n")
str_arr: t.CArray[t.CChar, 32] = t.CArray[t.CChar, 32]()
str_arr[0] = 'H'
str_arr[1] = 'e'
str_arr[2] = 'l'
str_arr[3] = 'l'
str_arr[4] = 'o'
str_arr[5] = '\0'
stdio.printf("String: %s\n", str_arr)
def TestNestedArray():
stdio.printf("=== Test Nested Array (Array of Arrays) ===\n")
row0: t.CArray[t.CInt, 3] = t.CArray[t.CInt, 3]()
row1: t.CArray[t.CInt, 3] = t.CArray[t.CInt, 3]()
row0[0] = 0
row0[1] = 1
row0[2] = 2
row1[0] = 10
row1[1] = 11
row1[2] = 12
i: t.CInt = 0
while i < 3:
stdio.printf("row0[%d]=%d ", i, row0[i])
i += 1
stdio.printf("\n")
i = 0
while i < 3:
stdio.printf("row1[%d]=%d ", i, row1[i])
i += 1
stdio.printf("\n")
def TestArrayWithStruct():
stdio.printf("=== Test Array with Struct ===\n")
points: t.CArray[SimpleData, 3] = t.CArray[SimpleData, 3]()
i: t.CInt = 0
while i < 3:
points[i].x = i * 10
points[i].y = i * 100
i += 1
i = 0
while i < 3:
stdio.printf("Point[%d]: x=%d, y=%d\n", i, points[i].x, points[i].y)
i += 1
def TestArrayPointer():
stdio.printf("=== Test Array Pointer ===\n")
data: t.CArray[t.CInt, 8] = t.CArray[t.CInt, 8]()
data[0] = 100
data[1] = 200
data[2] = 300
ptr: t.CInt | t.CPtr = data
stdio.printf("ptr[0]=%d\n", ptr[0])
stdio.printf("ptr[1]=%d\n", ptr[1])
stdio.printf("ptr[2]=%d\n", ptr[2])
def TestDynamicString():
stdio.printf("=== Test Dynamic String ===\n")
name: str = "TransPyC"
stdio.printf("Hello %s!\n", name)

View File

@@ -0,0 +1,24 @@
import t, c
POSMAX: t.CDefine = 100
MAX_RETRY: t.CDefine = 5
BUFFER_SIZE: t.CDefine = 256
PI: t.CDefine = 3.14159
EULER: t.CDefine = 2.71828
TRUE: t.CDefine = 1
FALSE: t.CDefine = 0
NULL: t.CDefine = 0
MATH_OFFSET: t.CDefine = 10
MATH_SCALE: t.CDefine = 2
THRESHOLD: t.CDefine = 50
LOOP_LIMIT: t.CDefine = 20
SHIFT_AMOUNT: t.CDefine = 2
MASK_VALUE: t.CDefine = 0xFF
BIT_RANGE: t.CDefine = 4
EXPONENT_BASE: t.CDefine = 3
MODULO_DIVISOR: t.CDefine = 7
SCALE_SHIFT: t.CDefine = 4
HALF_SCALE: t.CDefine = 0.5

View File

@@ -0,0 +1,372 @@
import config
import t, c
def ForRangeBasicTest(stop: t.CInt):
i: t.CInt = 0
for i in range(stop):
print("For range: ", i)
print("Done")
def ForRangeWithStartStop(start: t.CInt, stop: t.CInt, step: t.CInt):
i: t.CInt = 0
for i in range(start, stop, step):
print("Range: ", i)
print("Done")
def NestedForRangeTest(rows: t.CInt, cols: t.CInt):
r: t.CInt = 0
c_val: t.CInt = 0
for r in range(rows):
for c_val in range(cols):
print("Cell: ", r, c_val)
print("Grid done")
def ForWithIfBreakTest(data_size: t.CInt):
i: t.CInt = 0
found: t.CInt = 0
for i in range(data_size):
if i == 5:
print("Found at: ", i)
found = 1
break
print("Checking: ", i)
if found == 0:
print("Not found")
def ForWithIfContinueTest(n: t.CInt):
i: t.CInt = 0
count: t.CInt = 0
for i in range(n):
if i % 2 == 0:
continue
print("Odd: ", i)
count += 1
print("Total odd: ", count)
def ForWithElifBranchTest(value: t.CInt):
i: t.CInt = 0
result: t.CInt = 0
for i in range(value):
if i < 3:
result += 1
elif i < 6:
result += 2
elif i < 9:
result += 3
else:
result += 4
print("Elif branch result: ", result)
def WhileWithIfElifElseTest(iterations: t.CInt):
counter: t.CInt = 0
while counter < iterations:
if counter < 2:
print("Phase 1: ", counter)
elif counter < 4:
print("Phase 2: ", counter)
elif counter < 6:
print("Phase 3: ", counter)
else:
print("Phase 4: ", counter)
counter += 1
print("While done")
def WhileWithNestedIfTest(limit: t.CInt):
outer: t.CInt = 0
while outer < limit:
inner: t.CInt = 0
while inner < limit:
if inner == 2 and outer == 1:
print("Target: ", outer, inner)
inner += 1
continue
if inner > 3:
break
print("Inner: ", inner)
inner += 1
outer += 1
def WhileWithMatchCaseTest(value: t.CInt):
result: t.CInt = 0
match value:
case 1:
result = 100
print("Case 1")
case 2:
result = 200
print("Case 2")
case 3:
result = 300
print("Case 3")
case _:
result = 999
print("Default case")
print("Match result: ", result)
def WhileWithMatchCaseNoBreakTest(value: t.CInt):
result: t.CInt = 0
match value:
case 1:
result = 10
print("Case 1: ", result)
case 2:
result = 20
print("Case 2: ", result)
result += 5
case 3:
result = 30
print("Case 3: ", result)
case _:
result = 0
print("No break match result: ", result)
def ComplexForWhileMatchTest(outer_limit: t.CInt, inner_limit: t.CInt):
i: t.CInt = 0
j: t.CInt = 0
result: t.CInt = 0
for i in range(outer_limit):
j = 0
while j < inner_limit:
match (i, j):
case (0, 0):
result += 1
case (1, 1):
result += 10
case (2, _):
result += 100
case (_, 0):
result += 1000
case _:
result += 10000
j += 1
print("Complex match result: ", result)
def ForWithCaseAndNoBreakTest(n: t.CInt):
i: t.CInt = 0
result: t.CInt = 0
for i in range(n):
match i:
case 0:
result += 1
case 1:
result += 2
case 2:
result += 3
result += 10
case _:
result += i
c.NoBreak
print("NoBreak result: ", result)
def WhileWithBreakConditionTest(limit: t.CInt):
i: t.CInt = 0
early_exit: t.CInt = 0
while i < limit:
if i == 3:
early_exit = 1
break
if i == 2:
i += 1
continue
print("While i: ", i)
i += 1
def NestedWhileWithMultipleBreakPoints(depth: t.CInt, width: t.CInt):
d: t.CInt = 0
w: t.CInt = 0
count: t.CInt = 0
while d < depth:
w = 0
while w < width:
if w == 2:
break
if d == 1 and w == 1:
c.Break
count += 1
w += 1
d += 1
print("Nested break count: ", count)
def ForWithMatchCaseInLoopTest(iterations: t.CInt):
i: t.CInt = 0
sum_even: t.CInt = 0
sum_odd: t.CInt = 0
for i in range(iterations):
match i % 3:
case 0:
sum_even += i
case 1:
sum_odd += i
case 2:
sum_even += i * 2
case _:
pass
print("Sum even: ", sum_even)
print("Sum odd: ", sum_odd)
def ComplexLogicWithElifChains(a: t.CInt, b: t.CInt, c_val: t.CInt):
i: t.CInt = 0
result: t.CInt = 0
while i < 10:
if i < a:
if i < b:
result += 1
elif i < c_val:
result += 2
else:
result += 3
elif i < b:
if i < c_val:
result += 4
else:
result += 5
elif i < c_val:
result += 6
else:
result += 7
i += 1
print("Complex elif chain result: ", result)
def ForRangeWithStepAndCondition(n: t.CInt, step: t.CInt):
i: t.CInt = 0
count: t.CInt = 0
for i in range(0, n, step):
if i % 2 == 0:
print("Even step: ", i)
count += 1
else:
if i > 5:
print("Large odd: ", i)
else:
print("Small odd: ", i)
print("Total steps: ", count)
def MatchCaseWithGuardConditions(value: t.CInt):
result: t.CInt = 0
match value:
case 1 if config.TRUE == 1:
result = 100
case 2 if config.FALSE == 0:
result = 200
case 3:
result = 300
case _:
result = 0
print("Guard match result: ", result)
def WhileWithMultipleMatchCases(iterations: t.CInt):
i: t.CInt = 0
state: t.CInt = 0
while i < iterations:
match state:
case 0:
if i > 3:
state = 1
case 1:
if i > 6:
state = 2
case 2:
if i > 9:
state = 0
print("State at i=", i, ": ", state)
i += 1
def ForWhileMixWithBreakAndContinue(n: t.CInt):
i: t.CInt = 0
j: t.CInt = 0
count: t.CInt = 0
for i in range(n):
j = 0
while j < n:
if j == 2:
j += 1
continue
if i == 3 and j == 3:
break
count += 1
j += 1
print("Mix break continue count: ", count)
def ComplexCaseMatchingWithRanges(start: t.CInt, end: t.CInt):
i: t.CInt = 0
bucket0: t.CInt = 0
bucket1: t.CInt = 0
bucket2: t.CInt = 0
bucket3: t.CInt = 0
for i in range(start, end):
match i:
case _ if i < 10:
bucket0 += 1
case _ if i < 20:
bucket1 += 1
case _ if i < 30:
bucket2 += 1
case _:
bucket3 += 1
print("Bucket0 (<10): ", bucket0)
print("Bucket1 (10-19): ", bucket1)
print("Bucket2 (20-29): ", bucket2)
print("Bucket3 (>=30): ", bucket3)
def WhileWithElifInElifChain(limit: t.CInt):
i: t.CInt = 0
result: t.CInt = 0
while i < limit:
if i < 2:
if i == 0:
result = 1
else:
result = 2
elif i < 4:
if i == 2:
result = 3
else:
result = 4
elif i < 6:
if i == 4:
result = 5
else:
result = 6
else:
result = 7
print("Chain result[", i, "]: ", result)
i += 1
def ForWithCaseNoBreakNested(inner_limit: t.CInt):
i: t.CInt = 0
j: t.CInt = 0
result: t.CInt = 0
for i in range(3):
for j in range(inner_limit):
match j:
case 0:
result += 1
case 1:
result += 2
case _:
result += 3
c.NoBreak
print("Nested NoBreak result: ", result)

View File

@@ -0,0 +1,298 @@
import config
import t, c
def AboutSizeTWhileTest(length: t.CSizeT):
pos: t.CSizeT = 0
while pos < length:
print("Pos: ", pos)
if pos > config.POSMAX: break
pos += 1
while pos < length:
if pos > config.POSMAX:
print("Pos: ", pos)
pos += 1
else:
print("Pos: ", pos)
pos += 1
if pos > config.POSMAX: break
else:
pos += 1
print("Pos: ", pos)
while pos < length:
print("Pos: ", pos)
pos += 1
def MathBasicTest(x: t.CInt, y: t.CInt) -> t.CInt:
result: t.CInt = 0
result = x + y
print("Add: ", result)
result = x - y
print("Sub: ", result)
result = x * y
print("Mul: ", result)
if y != 0:
result = x / y
print("Div: ", result)
result = x % y if y != 0 else 0
print("Mod: ", result)
return result
def MathAdvancedTest(value: t.CInt) -> t.CInt:
counter: t.CInt = 0
scaled: t.CInt = 0
offset: t.CInt = 0
scaled = value * config.MATH_SCALE
print("Scaled: ", scaled)
offset = scaled + config.MATH_OFFSET
print("Offset: ", offset)
threshold: t.CInt = config.THRESHOLD
if offset > threshold:
print("Above threshold")
counter = 1
elif offset == threshold:
print("At threshold")
counter = 0
else:
print("Below threshold")
counter = -1
return counter
def WhileWithElifElseTest(start: t.CInt, end: t.CInt):
idx: t.CInt = start
while idx < end:
if idx < 0:
print("Negative: ", idx)
elif idx == 0:
print("Zero: ", idx)
elif idx > 0 and idx < 5:
print("Small positive: ", idx)
elif idx >= 5 and idx < 10:
print("Medium positive: ", idx)
else:
print("Large: ", idx)
idx += 1
def NestedWhileWithMacrosTest(outer_limit: t.CInt, inner_limit: t.CInt):
outer: t.CInt = 0
inner: t.CInt = 0
total_ops: t.CInt = 0
while outer < outer_limit:
inner = 0
while inner < inner_limit:
if inner == config.TRUE:
print("Inner loop break condition at: ", inner)
total_ops += 1
inner += 1
outer += 1
print("Total operations: ", total_ops)
def ComplexConditionTest(a: t.CInt, b: t.CInt, c_val: t.CInt):
x: t.CInt = a
y: t.CInt = b
z: t.CInt = c_val
while x < config.LOOP_LIMIT:
if x > 0 and y > 0 and z > 0:
print("All positive: ", x, y, z)
elif x <= 0 and y <= 0 and z <= 0:
print("All non-positive: ", x, y, z)
else:
if x > 0:
print("X positive")
elif y > 0:
print("Y positive")
else:
print("Z positive")
x += 1
y += 1
z += 1
def RetryLoopTest(retry_count: t.CInt) -> t.CInt:
attempt: t.CInt = 0
success: t.CInt = 0
while attempt < retry_count:
if attempt < config.MAX_RETRY:
print("Attempt: ", attempt, " less than max")
success = attempt * 2
elif attempt == config.MAX_RETRY:
print("At max retry")
success = -1
else:
print("Beyond max retry")
success = -2
attempt += 1
return success
def BitwiseOperationsTest(value: t.CInt) -> t.CInt:
shifted: t.CInt = 0
masked: t.CInt = 0
xored: t.CInt = 0
anded: t.CInt = 0
ored: t.CInt = 0
shifted = value << config.SHIFT_AMOUNT
print("Left shift: ", shifted)
shifted = value >> config.SHIFT_AMOUNT
print("Right shift: ", shifted)
masked = value & config.MASK_VALUE
print("Bitwise AND mask: ", masked)
xored = value ^ config.MASK_VALUE
print("Bitwise XOR: ", xored)
anded = value & (config.MASK_VALUE >> config.SHIFT_AMOUNT)
print("Bitwise AND shifted mask: ", anded)
ored = value | config.SHIFT_AMOUNT
print("Bitwise OR shift: ", ored)
return masked
def CompoundAssignmentMathTest(a: t.CInt, b: t.CInt) -> t.CInt:
x: t.CInt = a
y: t.CInt = b
result: t.CInt = 0
x += y
print("x += y: ", x)
x -= y
print("x -= y: ", x)
x *= y
print("x *= y: ", x)
x = a
y = b
if b != 0:
x /= b
print("x /= y: ", x)
x = a
if b != 0:
x %= b
print("x %%= y: ", x)
x = a
x <<= config.SHIFT_AMOUNT
print("x <<= shift: ", x)
x >>= config.SHIFT_AMOUNT
print("x >>= shift: ", x)
x &= config.MASK_VALUE
print("x &= mask: ", x)
x |= config.SHIFT_AMOUNT
print("x |= shift: ", x)
result = (a + b) * config.MATH_SCALE - config.MATH_OFFSET
print("Compound: (a+b)*scale-offset: ", result)
result = (a * b) + (config.MATH_OFFSET / 2)
print("Compound: a*b+offset/2: ", result)
return result
def PowerAndModuloTest(base: t.CInt, exponent: t.CInt) -> t.CInt:
result: t.CInt = 1
temp: t.CInt = 0
i: t.CInt = 0
while i < exponent:
result *= base
i += 1
print("Power: ", result)
temp = result % config.MODULO_DIVISOR
print("Power mod: ", temp)
temp = (base * config.EXPONENT_BASE) % config.MODULO_DIVISOR
print("base*exp %% divisor: ", temp)
temp = ((base + config.MATH_OFFSET) * config.MATH_SCALE) % config.MODULO_DIVISOR
print("(base+offset)*scale %% divisor: ", temp)
return result
def MixedArithmeticTest(x: t.CInt, y: t.CInt, z: t.CInt) -> t.CInt:
result: t.CInt = 0
result = (x + y) * z
print("(x+y)*z: ", result)
result = x + (y * z)
print("x+(y*z): ", result)
result = ((x + y) * config.MATH_SCALE) - config.MATH_OFFSET
print("((x+y)*scale)-offset: ", result)
result = (x * config.MATH_SCALE) + (y * config.MATH_SCALE)
print("x*scale + y*scale: ", result)
result = ((x % config.MODULO_DIVISOR) + (y % config.MODULO_DIVISOR)) * config.MATH_SCALE
print("(x%%mod + y%%mod)*scale: ", result)
temp: t.CInt = 0
temp = x | y & z
print("x | y & z: ", temp)
temp = (x | y) & z
print("(x | y) & z: ", temp)
temp = x ^ y ^ z
print("x ^ y ^ z: ", temp)
return result
def MathWithWhileLoopTest(start: t.CInt, count: t.CInt) -> t.CInt:
idx: t.CInt = 0
accumulator: t.CInt = 0
multiplier: t.CInt = config.MATH_SCALE
while idx < count:
temp: t.CInt = (start + idx) * multiplier
temp = temp + config.MATH_OFFSET
if temp > config.THRESHOLD:
temp = config.THRESHOLD
accumulator += temp
multiplier += 1
idx += 1
print("Accumulator: ", accumulator)
return accumulator
def ComplexNestedMathTest(a: t.CInt, b: t.CInt) -> t.CInt:
layer1: t.CInt = 0
layer2: t.CInt = 0
layer3: t.CInt = 0
i: t.CInt = 0
layer1 = (a + b) * config.MATH_SCALE
print("Layer1: ", layer1)
while i < config.BIT_RANGE:
layer2 += layer1 >> 1
i += 1
print("Layer2: ", layer2)
layer3 = layer1 + layer2 - config.MATH_OFFSET
print("Layer3: ", layer3)
layer3 = layer3 * config.MATH_SCALE / 2 if config.MATH_SCALE != 0 else layer3
print("Layer3 scaled: ", layer3)
return layer3
def ConditionalMathChainTest(value: t.CInt) -> t.CInt:
result: t.CInt = 0
step: t.CInt = 0
while step < config.LOOP_LIMIT:
if step < 5:
result += step * config.MATH_SCALE
print("step<5: ", result)
elif step < 10:
result -= step - config.MATH_OFFSET
print("step<10: ", result)
elif step < 15:
result += (step * config.MATH_OFFSET) / config.MATH_SCALE
print("step<15: ", result)
else:
result = result * config.EXPONENT_BASE % config.MODULO_DIVISOR
print("step>=15: ", result)
step += 1
return result
def BitManipulationMathTest(value: t.CInt) -> t.CInt:
original: t.CInt = value
nibble: t.CInt = 0
result: t.CInt = 0
result = (value << config.SHIFT_AMOUNT) | (value >> (8 - config.SHIFT_AMOUNT))
print("Rotate left: ", result)
result = (value >> config.SHIFT_AMOUNT) | (value << (8 - config.SHIFT_AMOUNT))
print("Rotate right: ", result)
nibble = value & 0xF
print("Lower nibble: ", nibble)
nibble = (value >> config.BIT_RANGE) & 0xF
print("Upper nibble: ", nibble)
result = ((value & config.MASK_VALUE) + config.MATH_OFFSET) * config.MATH_SCALE
print("Masked and scaled: ", result)
result = ((original << config.SHIFT_AMOUNT) ^ original) & config.MASK_VALUE
print("XOR shifted: ", result)
return result