summary refs log tree commit diff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorvolth <volth@webmaster.ms>2018-06-10 19:25:48 +0000
committerxeji <36407913+xeji@users.noreply.github.com>2018-06-10 21:25:48 +0200
commit3c2bbe217c4bbc67cfdf3912317d5a19b3994f69 (patch)
tree49b7e480abe63303733e6f284fe350bb4acb74ae /lib/trivial.nix
parente544e4f564f69c89fc38fbd161648084b4f57c4f (diff)
downloadnixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar.gz
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar.bz2
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar.lz
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar.xz
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.tar.zst
nixpkgs-3c2bbe217c4bbc67cfdf3912317d5a19b3994f69.zip
lib: bitAnd, bitOr, bitXor (bitsize-agnostic fallback function) (#41491)
* lib: bitAnd, bitOr, bitXor

* lib: test for bitAnd, bitOr, bitXor

* lib: bitsize-agnostic zipIntBits

* lib: bitNot

* lib: bitNot
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index 251cb796db0..0bcefcbc28d 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -1,4 +1,41 @@
 { lib }:
+let
+  zipIntBits = f: x: y:
+    let
+      # (intToBits 6) -> [ 0 1 1 ]
+      intToBits = x:
+        if x == 0 || x == -1 then
+          []
+        else
+          let
+            headbit  = if (x / 2) * 2 != x then 1 else 0;          # x & 1
+            tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1
+          in
+            [headbit] ++ (intToBits tailbits);
+
+      # (bitsToInt [ 0 1 1 ] 0) -> 6
+      # (bitsToInt [ 0 1 0 ] 1) -> -6
+      bitsToInt = l: signum:
+        if l == [] then
+          (if signum == 0 then 0 else -1)
+        else
+          (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum));
+
+      xsignum = if x < 0 then 1 else 0;
+      ysignum = if y < 0 then 1 else 0;
+      zipListsWith' = fst: snd:
+        if fst==[] && snd==[] then
+          []
+        else if fst==[] then
+          [(f xsignum             (builtins.head snd))] ++ (zipListsWith' []                  (builtins.tail snd))
+        else if snd==[] then
+          [(f (builtins.head fst) ysignum            )] ++ (zipListsWith' (builtins.tail fst) []                 )
+        else
+          [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
+    in
+      assert (builtins.isInt x) && (builtins.isInt y);
+      bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum);
+in
 rec {
 
   /* The identity function
@@ -31,6 +68,18 @@ rec {
   /* boolean “and” */
   and = x: y: x && y;
 
+  /* bitwise “and” */
+  bitAnd = builtins.bitAnd or zipIntBits (a: b: if a==1 && b==1 then 1 else 0);
+
+  /* bitwise “or” */
+  bitOr = builtins.bitOr or zipIntBits (a: b: if a==1 || b==1 then 1 else 0);
+
+  /* bitwise “xor” */
+  bitXor = builtins.bitXor or zipIntBits (a: b: if a!=b then 1 else 0);
+
+  /* bitwise “not” */
+  bitNot = builtins.sub (-1);
+
   /* Convert a boolean to a string.
      Note that toString on a bool returns "1" and "".
   */