code wiki / (root) / _bmi_intrinsics_smoke.nx

_bmi_intrinsics_smoke.nx source

↩ module page · 34 lines · 910 B

1// Smoke that exercises __clz32 / __ctz32 / __popcnt64 builtins. 2// Expected exit code: 0 if all three return the right answers. 3 4import "syscalls.nx" 5 6func main() -> i64 { 7 // popcount(0xFFFF) = 16 8 let p: i64 = __popcnt64(65535) 9 if p != 16 { return 1 } 10 11 // popcount(0x8000000000000001) = 2 12 let q: i64 = __popcnt64(-9223372036854775807 - 1 + 1) // = 0x8000_0000_0000_0001 ... reframe: 13 // Simpler check: popcount(0xFFFFFFFFFFFFFFFF) = 64 14 let r: i64 = __popcnt64(-1) 15 if r != 64 { return 2 } 16 17 // clz32 of 0x00000001 = 31 18 let c1: i64 = __clz32(1) 19 if c1 != 31 { return 3 } 20 21 // clz32 of 0x80000000 = 0 22 let c2: i64 = __clz32(2147483648) 23 if c2 != 0 { return 4 } 24 25 // ctz32 of 0x80000000 = 31 26 let t1: i64 = __ctz32(2147483648) 27 if t1 != 31 { return 5 } 28 29 // ctz32 of 0x10 = 4 30 let t2: i64 = __ctz32(16) 31 if t2 != 4 { return 6 } 32 33 return 0 34}