Files
waozi ab5f39b9f1 Drop default checks on ARMv7, SIMD gathers and plain std readers
ARMv7 native code gets the unchecked forms AArch64 has: frame words on
the arena mark, a raw resize, and no index compare for a member whose
bound marks the no-check form, so lowering no longer forces ARMv7
programs to checked pointers; default bundles pass the 72-program
parity on the Moto. Generated C checks a SIMD gather's indexes and Go
uses its _checked SIMD helpers only where checks are asked for.

The plain ReadU16LE, ReadU32LE, ReadS32LE and ReadF32LE read the bytes
at their offset at any alignment, as C does, instead of returning 0 for
a field they refuse, and GetHttps passes its URL straight to curl; the
TryRead forms and GetHttpsCapped keep the validation. The Android parity
driver builds its trap cases with --checks.
2026-11-16 04:51:27 -03:00

135 lines
4.3 KiB
Ziran

// Little-endian numbers in byte text, such as file or asset contents, read
// the same way on every native target and in the engine. Floats are
// decoded from their IEEE 754 bits with exact arithmetic, so no target
// reinterprets memory. TryRead names invalid offsets; the plain Read helpers
// read the bytes at their offset, as in C, for layouts the caller validated.
#import "math"
#import "result"
BinaryReadFailure :: enum {
None
NegativeOffset
Misaligned
ShortField
}
#scope_export
// The value of an IEEE 754 single from its bits.
#program_export
Float32FromBits :: (bits: u32) -> float64 {
exponent := cast(s32)((bits >> cast(u32)23) & cast(u32)255)
mantissa := cast(float64)(bits & cast(u32)8388607)
negative := (bits & cast(u32)2147483648) != cast(u32)0
value: float64 = 0.0
if exponent == 255 {
value = ifx mantissa == 0.0 then Infinity() else NotANumber()
} else if exponent == 0 {
value = ScaleByPowerOfTwo(mantissa, -149)
} else {
value = ScaleByPowerOfTwo(mantissa + 8388608.0, exponent - 150)
}
if negative {
return -value
}
return value
}
// The value of an IEEE 754 double from its bits.
#program_export
Float64FromBits :: (bits: u64) -> float64 {
exponent := cast(s32)((bits >> cast(u64)52) & cast(u64)2047)
mantissa := cast(float64)(bits & cast(u64)4503599627370495)
negative := (bits >> cast(u64)63) != cast(u64)0
value: float64 = 0.0
if exponent == 2047 {
value = ifx mantissa == 0.0 then Infinity() else NotANumber()
} else if exponent == 0 {
value = ScaleByPowerOfTwo(mantissa, -1074)
} else {
value = ScaleByPowerOfTwo(mantissa + 4503599627370496.0, exponent - 1075)
}
if negative {
return -value
}
return value
}
#program_export
TryReadU16LE :: (bytes: string, at: s32) -> Result(u32, BinaryReadFailure) {
result: Result(u32, BinaryReadFailure)
if at < 0 { result.error = .NegativeOffset; return result }
if at % 2 != 0 { result.error = .Misaligned; return result }
if at > cast(s32)bytes.count || 2 > cast(s32)bytes.count - at {
result.error = .ShortField
return result
}
result.is_ok = true
result.value = cast(u32)bytes[at] | (cast(u32)bytes[at + 1] << cast(u32)8)
return result
}
#program_export
TryReadU32LE :: (bytes: string, at: s32) -> Result(u32, BinaryReadFailure) {
result: Result(u32, BinaryReadFailure)
if at < 0 { result.error = .NegativeOffset; return result }
if at % 4 != 0 { result.error = .Misaligned; return result }
if at > cast(s32)bytes.count || 4 > cast(s32)bytes.count - at {
result.error = .ShortField
return result
}
value: u32 = 0
index: s32 = 3
while index >= 0 {
value = (value << cast(u32)8) | cast(u32)bytes[at + index]
index -= 1
}
result.is_ok = true
result.value = value
return result
}
#program_export
TryReadS32LE :: (bytes: string, at: s32) -> Result(s32, BinaryReadFailure) {
result: Result(s32, BinaryReadFailure)
value := TryReadU32LE(bytes, at)
if !value.is_ok { result.error = value.error; return result }
result.is_ok = true
result.value = cast(s32)value.value
return result
}
#program_export
TryReadF32LE :: (bytes: string, at: s32) -> Result(float64, BinaryReadFailure) {
result: Result(float64, BinaryReadFailure)
value := TryReadU32LE(bytes, at)
if !value.is_ok { result.error = value.error; return result }
result.is_ok = true
result.value = Float32FromBits(value.value)
return result
}
// Plain readers for layouts the caller has validated: the field's bytes at
// AT, at any alignment. The caller keeps AT and the field inside BYTES, as
// in C; untrusted boundaries use the TryRead forms above, which name
// malformed input.
#program_export
ReadU16LE :: (bytes: string, at: s32) -> u32 {
return cast(u32)bytes[at] | (cast(u32)bytes[at + 1] << cast(u32)8)
}
#program_export
ReadU32LE :: (bytes: string, at: s32) -> u32 {
return cast(u32)bytes[at] | (cast(u32)bytes[at + 1] << cast(u32)8) |
(cast(u32)bytes[at + 2] << cast(u32)16) | (cast(u32)bytes[at + 3] << cast(u32)24)
}
#program_export
ReadS32LE :: (bytes: string, at: s32) -> s32 {
return cast(s32)ReadU32LE(bytes, at)
}
#program_export
ReadF32LE :: (bytes: string, at: s32) -> float64 {
return Float32FromBits(ReadU32LE(bytes, at))
}