uCBQN/test/bit.bqn
2022-10-17 09:39:38 -04:00

64 lines
2.4 KiB
BQN
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

u 100×(•UnixTime+1|100וMonoTime)@
r •MakeRand •Show u
RandVals, casts r •Import "utils.bqn"
widths ¨casts
# Test •bit._cast first
# Convert unsigned width 𝕗 to 𝕘
_cvtu_ {
𝕗=𝕘 ? 𝕩 ;
𝕗<𝕘 ? b2𝕗 +(b×)˝(𝕘÷𝕗)𝕩 ;
b2𝕘 >1(--b×)÷b(1+𝕗÷𝕘)𝕩
}
_bitcast { frto _𝕣 𝕩:
S {𝕊:𝕩+𝕨×𝕩<0; 𝕊:𝕩-𝕨×𝕩𝕨÷2}
_off {("uci"1𝕗),-@,(2𝕗)S}
ft ¨𝕗
to _off f _cvtu_ t fr _off 𝕩
}
TestCast { ft 𝕊 len:
c 𝕨 casts
len + (1÷˜´¨c) | -len # Round up to exact conversion
(c _bitcast c •bit._cast) len RandVals f
}
cs / 64>widths # TODO floats
cx (32'c'¨casts)/ cs # TODO 32-bit char output
ls (100) r.Range¨+ 100 × (1.5) 100
{cs ( !TestCast 𝕩˙) cx}¨ ls
# Now other •bit operations
OpArgs {_bFa:
m(1<a) {𝔽1}{𝔽´} # Adjust to act on argument list 𝕨‿𝕩
{𝕗 _b _m}(F _m)a
}
ops ¯1 (¨ OpArgs¨) # Group by args then int-ness
# op fn args int
•bit._not¬10
•bit._and20
•bit._or 20
•bit._xor20
•bit._neg-11
•bit._add+21
•bit._sub-21
•bit._mul×21
cres (widths 32) 'c' 1¨casts # Possible results; TODO floats
TestOps { 𝕊 len: # Length in bits
tz +´¬`2|÷2(7) len # Trailing zeros
wxc casts ˜ wx (2 r.Range ) / cf widths2tz
orc casts ˜ or (2 r.Range ) / cfcres
wd (orwx) widths
args (len÷¯2wd) RandVals¨ wx
_bc •bit._cast
Test { c 𝕊 ops:
cargs wxc {𝕨c _bc 𝕩}¨ args
mod {1: ; m2𝕩 m|((m÷2)+)} c
Test1 { _bFna: ! (((2+a)wd)_b args) c,1orc_bc Mod Fn cargs }
Test1¨ ops
}
DropMul ¯1(32=wd) # Multiplication test may need 64 bits
1'u',orc Test¨ 0(1=wd)DropMul(1) ops
}
(203456) TestOps× ls