Skip to content

Instantly share code, notes, and snippets.

@thundergnat
Created August 19, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thundergnat/76b406a52ed92a3478d008a99b80715a to your computer and use it in GitHub Desktop.
Save thundergnat/76b406a52ed92a3478d008a99b80715a to your computer and use it in GitHub Desktop.
This fills me with nearly equal parts pleasure and horror
# Implementing a fixed bit, nominally unsigned Integer class
# Major bummer; can not instantiate usefully in a $ sigiled scalar
class Fixed-Int {
has $!var handles <FETCH Str Numeric> = 0;
has $!bits;
has $!mask;
submethod BUILD (Int :bits(:$bit)) { $!bits = $bit; $!mask = 2**$!bits - 1 }
method STORE (Int \val) { $!var = val +& $!mask }
# rotate right
method ror ($bits) { $!var +> $bits +| ($!var +& (2**$bits - 1) +< ($!bits - $bits)) }
# rotate left
method rol ($bits) { (($!var +< $bits) +& $!mask ) +| ($!var +> ($!bits - $bits)) }
# ones complement
method C1 { $!mask +^ $!var }
# twos complement
method C2 { $!var ?? $!mask +^ $!var + 1 !! 0 }
method signed {
($!var +& 2**($!bits - 1)) ??
-(($!var +& ($!mask +> 1)) +^ ($!mask +> 1) + 1) !!
$!var
}
# just for messing around - display as binary
method gist { $!var.Numeric.fmt('%0' ~ $!bits ~ 'b') }
}
# foolin' around with various tests
# arbitrary sized UInts, test a few
for 4, 8, 19
-> $c {
sub f ($n) { $n.Str.fmt('%0' ~ $c ~ 'b') }
my \foo = Fixed-Int.new(:bits($c));
say '=' x 60;
say "Testing $c bit {foo.^name}";
say foo;
say "\nMultiple subtract 1";
for ^10 {
say foo -= 1;
put foo;
put foo.signed;
}
say "\nBit shift left 1";
for ^$c {
say foo +<= 1;
#say foo.signed;
}
say "\nA constant";
say 0x9e3779b97f4a7c15;
say "\nset equal to constant";
say foo = 0x9e3779b97f4a7c15;
say foo += 2**($c - 3);
say "\ntimes a constant";
say foo * 73235217;
say "\ntimes equals a constant";
say foo *= 73235217;
say foo += 123;
say "\nAdd an offset";
for ^$c {
say foo += 2**($c div 3 * 2 - 1);
#say foo.signed;
}
say "\nBit shift left 2";
say foo +<= 2;
say "\nRotate right 1";
for ^$c { say foo.=ror(1) }
say "\nRotate left 3";
for ^$c { say foo.=rol(3) }
say "\nOnes complement";
say foo.C1.&f;
say "Twos complement";
say foo.C2.&f;
my $m = foo;
say $m.&f;
say ($m *= 6543).&f;
say foo = $m;
}
Testing 4 bit Fixed-Int
0000
Multiple subtract 1
1111
15
-1
1110
14
-2
1101
13
-3
1100
12
-4
1011
11
-5
1010
10
-6
1001
9
-7
1000
8
-8
0111
7
7
0110
6
6
Bit shift left 1
1100
1000
0000
0000
A constant
11400714819323198485
set equal to constant
0101
0111
times a constant
512646519
times equals a constant
0111
0010
Add an offset
0100
0110
1000
1010
Bit shift left 2
1000
Rotate right 1
0100
0010
0001
1000
Rotate left 3
0100
0010
0001
1000
Ones complement
0111
Twos complement
1000
1000
1100110001111000
1000
============================================================
Testing 8 bit Fixed-Int
00000000
Multiple subtract 1
11111111
255
-1
11111110
254
-2
11111101
253
-3
11111100
252
-4
11111011
251
-5
11111010
250
-6
11111001
249
-7
11111000
248
-8
11110111
247
-9
11110110
246
-10
Bit shift left 1
11101100
11011000
10110000
01100000
11000000
10000000
00000000
00000000
A constant
11400714819323198485
set equal to constant
00010101
00110101
times a constant
3881466501
times equals a constant
10000101
00000000
Add an offset
00001000
00010000
00011000
00100000
00101000
00110000
00111000
01000000
Bit shift left 2
00000000
Rotate right 1
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
Rotate left 3
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
Ones complement
11111111
Twos complement
00000000
00000000
00000000
00000000
============================================================
Testing 19 bit Fixed-Int
0000000000000000000
Multiple subtract 1
1111111111111111111
524287
-1
1111111111111111110
524286
-2
1111111111111111101
524285
-3
1111111111111111100
524284
-4
1111111111111111011
524283
-5
1111111111111111010
524282
-6
1111111111111111001
524281
-7
1111111111111111000
524280
-8
1111111111111110111
524279
-9
1111111111111110110
524278
-10
Bit shift left 1
1111111111111101100
1111111111111011000
1111111111110110000
1111111111101100000
1111111111011000000
1111111110110000000
1111111101100000000
1111111011000000000
1111110110000000000
1111101100000000000
1111011000000000000
1110110000000000000
1101100000000000000
1011000000000000000
0110000000000000000
1100000000000000000
1000000000000000000
0000000000000000000
0000000000000000000
A constant
11400714819323198485
set equal to constant
0100111110000010101
0110111110000010101
times a constant
16724946211941
times equals a constant
0100101010001100101
0100101010011100000
Add an offset
0100101110011100000
0100110010011100000
0100110110011100000
0100111010011100000
0100111110011100000
0101000010011100000
0101000110011100000
0101001010011100000
0101001110011100000
0101010010011100000
0101010110011100000
0101011010011100000
0101011110011100000
0101100010011100000
0101100110011100000
0101101010011100000
0101101110011100000
0101110010011100000
0101110110011100000
Bit shift left 2
0111011001110000000
Rotate right 1
0011101100111000000
0001110110011100000
0000111011001110000
0000011101100111000
0000001110110011100
0000000111011001110
0000000011101100111
1000000001110110011
1100000000111011001
1110000000011101100
0111000000001110110
0011100000000111011
1001110000000011101
1100111000000001110
0110011100000000111
1011001110000000011
1101100111000000001
1110110011100000000
0111011001110000000
Rotate left 3
1011001110000000011
1001110000000011101
1110000000011101100
0000000011101100111
0000011101100111000
0011101100111000000
1101100111000000001
1100111000000001110
0111000000001110110
1000000001110110011
0000001110110011100
0001110110011100000
1110110011100000000
0110011100000000111
0011100000000111011
1100000000111011001
0000000111011001110
0000111011001110000
0111011001110000000
Ones complement
1000100110001111111
Twos complement
1000100110010000000
0111011001110000000
1011110100110001100010010000000
0001100010010000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment