summary refs log tree commit diff
path: root/lib/trivial.nix
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2018-07-26 00:10:53 +0200
committerProfpatsch <mail@profpatsch.de>2018-07-26 20:36:45 +0200
commit62dca7c9ab08ee5cc2043d6d374013b8041a3f21 (patch)
treeb4041c3a529d15825a305d5c0b9ee30d054118fc /lib/trivial.nix
parentaf108429408b1b220fab4048ff44d674f52fdee6 (diff)
downloadnixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar.gz
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar.bz2
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar.lz
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar.xz
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.tar.zst
nixpkgs-62dca7c9ab08ee5cc2043d6d374013b8041a3f21.zip
lib/trivial: move zipIntBits to its own file
The amount of implementation detail really should not be the first thing in a
prominent file called `trivial.nix`.
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r--lib/trivial.nix50
1 files changed, 10 insertions, 40 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix
index a36056253d7..b75e81eb735 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -1,41 +1,5 @@
 { 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 {
 
   ## Simple (higher order) functions
@@ -71,13 +35,19 @@ rec {
   and = x: y: x && y;
 
   /* bitwise “and” */
-  bitAnd = builtins.bitAnd or zipIntBits (a: b: if a==1 && b==1 then 1 else 0);
+  bitAnd = builtins.bitAnd
+    or import ./zip-int-bits.nix
+      (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);
+  bitOr = builtins.bitOr
+    or import ./zip-int-bits.nix
+      (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);
+  bitXor = builtins.bitXor
+    or import ./zip-int-bits.nix
+      (a: b: if a!=b then 1 else 0);
 
   /* bitwise “not” */
   bitNot = builtins.sub (-1);