From 0decfd4edd7483f2bb8d5bb489af5a74f1bf097a Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 3 May 2017 19:53:32 -0400 Subject: - Small refactorings. - Added the width of bit-patterns. --- stdlib/source/lux/data/bit.lux | 26 ++++++++++---------------- stdlib/test/test/lux/data/bit.lux | 10 ++++------ 2 files changed, 14 insertions(+), 22 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/lux/data/bit.lux b/stdlib/source/lux/data/bit.lux index 4e87a23dc..29b01e370 100644 --- a/stdlib/source/lux/data/bit.lux +++ b/stdlib/source/lux/data/bit.lux @@ -1,5 +1,7 @@ (;module: [lux #- and or not]) +(def: #export width Nat +64) + ## [Values] (do-template [ ] [(def: #export ( param subject) @@ -7,18 +9,12 @@ (-> Nat ) (_lux_proc ["bit" ] [subject param]))] - [and "and" - "Bitwise and." Nat] - [or "or" - "Bitwise or." Nat] - [xor "xor" - "Bitwise xor." Nat] - [shift-left "shift-left" - "Bitwise shift-left." Nat] - [shift-right "shift-right" - "Bitwise shift-right." Int] - [unsigned-shift-right "unsigned-shift-right" - "Bitwise unsigned-shift-right." Nat] + [and "and" "Bitwise and." Nat] + [or "or" "Bitwise or." Nat] + [xor "xor" "Bitwise xor." Nat] + [shift-left "shift-left" "Bitwise shift-left." Nat] + [shift-right "shift-right" "Bitwise shift-right." Int] + [unsigned-shift-right "unsigned-shift-right" "Bitwise unsigned-shift-right." Nat] ) (def: #export (count subject) @@ -52,14 +48,12 @@ (-> Nat Nat Bool) (|> input (;;and (shift-left idx +1)) (n.= +0) ;not)) -(def: rot-top Nat +64) - (do-template [
] [(def: #export ( distance input) (-> Nat Nat Nat) (;;or (
distance input) - ( (n.- (n.% rot-top distance) - rot-top) + ( (n.- (n.% width distance) + width) input)))] [rotate-left shift-left unsigned-shift-right] diff --git a/stdlib/test/test/lux/data/bit.lux b/stdlib/test/test/lux/data/bit.lux index 2ba2c8e57..fe04806cd 100644 --- a/stdlib/test/test/lux/data/bit.lux +++ b/stdlib/test/test/lux/data/bit.lux @@ -7,11 +7,9 @@ ["R" math/random]) lux/test) -(def: width Nat +64) - (test: "Bitwise operations." [pattern R;nat - idx (:: @ map (n.% width) R;nat)] + idx (:: @ map (n.% &;width) R;nat)] ($_ seq (assert "Clearing and settings bits should alter the count." (and (n.< (&;count (&;set idx pattern)) @@ -31,7 +29,7 @@ (and (not (&;set? idx pattern)) (&;set? idx (&;flip idx pattern)))))) (assert "The negation of a bit pattern should have a complementary bit count." - (n.= width + (n.= &;width (n.+ (&;count pattern) (&;count (&;not pattern))))) (assert "Can do simple binary boolean logic." @@ -58,10 +56,10 @@ (n.= pattern)))) (assert "Rotate as many spaces as the bit-pattern's width leaves the pattern unchanged." (and (|> pattern - (&;rotate-left width) + (&;rotate-left &;width) (n.= pattern)) (|> pattern - (&;rotate-right width) + (&;rotate-right &;width) (n.= pattern)))) (assert "Shift right respect the sign of ints." (let [value (nat-to-int pattern)] -- cgit v1.2.3