diff options
author | sterni <sternenseemann@systemli.org> | 2023-08-05T13·39+0200 |
---|---|---|
committer | sterni <sternenseemann@systemli.org> | 2023-08-09T12·12+0000 |
commit | 984ea69386a47e753c4129e2a6230aa60b4584cd (patch) | |
tree | bab3642e070d41d5901c1c73a839ff1c03b7313a /users/sterni/nix/int | |
parent | dbdb2575cfd12b7b41fbf12547592d00819c7546 (diff) |
refactor(users/sterni/nix): move generic number operation into num r/6477
We omit type checks for performance reasons in most places currently, so the library grouping is important in showing people what to use for what sort of input. The moved functions make sense to use with floats as well, so we'll move them to the num library. Some of the remaining functions could theoretically be adapted and moved, but aren't for now. Change-Id: Ifdecaa60be594f4438b2a58b9ea6445e2da080e3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9007 Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'users/sterni/nix/int')
-rw-r--r-- | users/sterni/nix/int/default.nix | 28 | ||||
-rw-r--r-- | users/sterni/nix/int/tests/default.nix | 6 |
2 files changed, 5 insertions, 29 deletions
diff --git a/users/sterni/nix/int/default.nix b/users/sterni/nix/int/default.nix index a39a9477e211..e1d7e1fe7e4f 100644 --- a/users/sterni/nix/int/default.nix +++ b/users/sterni/nix/int/default.nix @@ -2,31 +2,22 @@ let - # TODO(sterni): implement nix.float and figure out which of these - # functions can be split out into a common nix.num - # library. - inherit (depot.users.sterni.nix) string + num ; inherit (builtins) bitOr bitAnd bitXor - mul - div - add - sub ; - abs = i: if i < 0 then -i else i; - exp = base: pow: if pow > 0 then base * (exp base (pow - 1)) else if pow < 0 - then 1.0 / exp base (abs pow) + then 1.0 / exp base (num.abs pow) else 1; bitShiftR = bit: count: @@ -52,7 +43,7 @@ let in if int == 0 then "0" - else "${sign}${go (abs int)}"; + else "${sign}${go (num.abs int)}"; fromHexMap = builtins.listToAttrs (lib.imap0 (i: c: { name = c; value = i; }) @@ -94,26 +85,17 @@ let odd = x: bitAnd x 1 == 1; even = x: bitAnd x 1 == 0; - # div and mod behave like quot and rem in Haskell, - # i. e. they truncate towards 0 + inherit (builtins) div; mod = a: b: let res = a / b; in a - (res * b); - inRange = a: b: x: x >= a && x <= b; - - sum = builtins.foldl' (a: b: a + b) 0; - in { inherit maxBound minBound - abs exp odd even - add - sub - mul div mod bitShiftR @@ -123,7 +105,5 @@ in bitXor toHex fromHex - inRange - sum ; } diff --git a/users/sterni/nix/int/tests/default.nix b/users/sterni/nix/int/tests/default.nix index 8d2263b42117..bc694ac90a02 100644 --- a/users/sterni/nix/int/tests/default.nix +++ b/users/sterni/nix/int/tests/default.nix @@ -15,9 +15,6 @@ let ; testBounds = it "checks minBound and maxBound" [ - # this is gonna blow up in my face because - # integer overflow is undefined behavior in - # C++, so most likely anything could happen? (assertEq "maxBound is the maxBound" true (int.maxBound + 1 < int.maxBound)) (assertEq "minBound is the minBound" true @@ -321,7 +318,6 @@ let testBasic = it "checks basic int operations" [ (assertEq "122 is even" (int.even 122 && !(int.odd 122)) true) (assertEq "123 is odd" (int.odd 123 && !(int.even 123)) true) - (assertEq "abs -4959" (int.abs (-4959)) 4959) ]; expNumbers = [ @@ -374,7 +370,7 @@ let checkShiftLMulExp = n: assertEq "${toString n} >> 6 == ${toString n} * 2 ^ 6" (int.bitShiftL n 5) - (int.mul n (int.exp 2 5)); + (n * (int.exp 2 5)); testBit = it "checks bitwise operations" (lib.flatten [ (builtins.map checkShift shifts) |