@@ -1,41 +1,5 @@
11{ lib }:
2−let
3− zipIntBits = f: x: y:
4− let
5− # (intToBits 6) -> [ 0 1 1 ]
6− intToBits = x:
7− if x == 0 || x == -1 then
8− []
9− else
10− let
11− headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1
12− tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1
13− in
14− [headbit] ++ (intToBits tailbits);
15−
16− # (bitsToInt [ 0 1 1 ] 0) -> 6
17− # (bitsToInt [ 0 1 0 ] 1) -> -6
18− bitsToInt = l: signum:
19− if l == [] then
20− (if signum == 0 then 0 else -1)
21− else
22− (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum));
23−
24− xsignum = if x < 0 then 1 else 0;
25− ysignum = if y < 0 then 1 else 0;
26− zipListsWith' = fst: snd:
27− if fst==[] && snd==[] then
28− []
29− else if fst==[] then
30− [(f xsignum (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd))
31− else if snd==[] then
32− [(f (builtins.head fst) ysignum )] ++ (zipListsWith' (builtins.tail fst) [] )
33− else
34− [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
35− in
36− assert (builtins.isInt x) && (builtins.isInt y);
37− bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum);
38−in
2+
393rec {
404
415 ## Simple (higher order) functions
@@ -71,13 +35,19 @@ rec {
7135 and = x: y: x && y;
7236
7337 /* bitwise “and” */
74− bitAnd = builtins.bitAnd or zipIntBits (a: b: if a==1 && b==1 then 1 else 0);
38+ bitAnd = builtins.bitAnd
39+ or import ./zip-int-bits.nix
40+ (a: b: if a==1 && b==1 then 1 else 0);
7541
7642 /* bitwise “or” */
77− bitOr = builtins.bitOr or zipIntBits (a: b: if a==1 || b==1 then 1 else 0);
43+ bitOr = builtins.bitOr
44+ or import ./zip-int-bits.nix
45+ (a: b: if a==1 || b==1 then 1 else 0);
7846
7947 /* bitwise “xor” */
80− bitXor = builtins.bitXor or zipIntBits (a: b: if a!=b then 1 else 0);
48+ bitXor = builtins.bitXor
49+ or import ./zip-int-bits.nix
50+ (a: b: if a!=b then 1 else 0);
8151
8252 /* bitwise “not” */
8353 bitNot = builtins.sub (-1);