at 23.11-beta 1.4 kB view raw
1/* Helper function to implement a fallback for the bit operators 2 `bitAnd`, `bitOr` and `bitXor` on older nix version. 3 See ./trivial.nix 4*/ 5f: x: y: 6 let 7 # (intToBits 6) -> [ 0 1 1 ] 8 intToBits = x: 9 if x == 0 || x == -1 then 10 [] 11 else 12 let 13 headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1 14 tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1 15 in 16 [headbit] ++ (intToBits tailbits); 17 18 # (bitsToInt [ 0 1 1 ] 0) -> 6 19 # (bitsToInt [ 0 1 0 ] 1) -> -6 20 bitsToInt = l: signum: 21 if l == [] then 22 (if signum == 0 then 0 else -1) 23 else 24 (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum)); 25 26 xsignum = if x < 0 then 1 else 0; 27 ysignum = if y < 0 then 1 else 0; 28 zipListsWith' = fst: snd: 29 if fst==[] && snd==[] then 30 [] 31 else if fst==[] then 32 [(f xsignum (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd)) 33 else if snd==[] then 34 [(f (builtins.head fst) ysignum )] ++ (zipListsWith' (builtins.tail fst) [] ) 35 else 36 [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd)); 37 in 38 assert (builtins.isInt x) && (builtins.isInt y); 39 bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum)