/
Bits.pm6
188 lines (151 loc) · 5.74 KB
/
Bits.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use v6.c;
module Bits:ver<0.0.2>:auth<cpan:ELIZABETH> {
use nqp;
my constant $nibble2pos = nqp::list(
nqp::list_i(), # 0
nqp::list_i(0), # 1
nqp::list_i(1), # 2
nqp::list_i(0,1), # 3
nqp::list_i(2), # 4
nqp::list_i(0,2), # 5
nqp::list_i(1,2), # 6
nqp::list_i(0,1,2), # 7
nqp::list_i(3), # 8
nqp::list_i(0,3), # 9
nqp::list_i(1,3), # 10
nqp::list_i(0,1,3), # 11
nqp::list_i(2,3), # 12
nqp::list_i(0,2,3), # 13
nqp::list_i(1,2,3), # 14
nqp::list_i(0,1,2,3) # 15
);
my class IterateBits does Iterator {
has Int $!bitmap; # the bitmap we're looking at
has int $!offset; # the current offset towards nibbles
has $!list; # list of positions for current nibble
method !SET-SELF(\bitmap) {
my $bitmap := nqp::decont(bitmap);
nqp::if(
$bitmap && nqp::isne_I($bitmap,-1),
nqp::stmts(
($!bitmap := nqp::if(
nqp::islt_I($bitmap,0),
nqp::mul_I(-2,nqp::add_I($bitmap,1,Int),Int),
$bitmap
)),
($!offset = -4),
($!list := nqp::atpos($nibble2pos,0)),
self
),
Rakudo::Iterator.Empty
)
}
method new(\bitmap) { nqp::create(self)!SET-SELF(bitmap) }
method pull-one() {
nqp::if(
nqp::elems($!list),
nqp::add_i($!offset,nqp::shift_i($!list)), # value ready
nqp::if( # value NOT ready
$!bitmap,
nqp::stmts( # not done yet
nqp::while(
$!bitmap && nqp::isfalse(
my int $index = nqp::bitand_I($!bitmap,15,Int)
),
nqp::stmts( # next nibble
($!offset = $!offset + 4),
($!bitmap := nqp::bitshiftr_I($!bitmap,4,Int))
)
),
nqp::if( # done searching
$!bitmap,
nqp::stmts( # found nibble
(my int $pos = nqp::add_i( # convert index
($!offset = nqp::add_i($!offset,4)), # to position by
nqp::shift_i( # fetching value
($!list := nqp::clone( # from the right
nqp::atpos($nibble2pos,$index) # list
))
)
)),
($!bitmap := nqp::bitshiftr_I($!bitmap,4,Int)),
$pos
),
IterationEnd # done now
)
),
IterationEnd # already done
)
)
}
}
sub bit(Int:D \bitmap, UInt:D \offset --> Bool:D) is export {
nqp::hllbool(bitmap +& (1 +< offset))
}
sub bits(Int:D \bitmap --> Seq:D) is export {
Seq.new( IterateBits.new(bitmap))
}
# nibble -> number of bits conversion
my constant $nibble2bits = nqp::list_i(0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
sub bitcnt(Int:D \bitmap --> Int:D) is export {
my $bitmap := nqp::decont(bitmap);
nqp::if(
$bitmap && nqp::isne_I($bitmap,-1),
nqp::stmts( # has significant bits
($bitmap := nqp::if(
nqp::isle_I($bitmap,0),
nqp::mul_I(-2,nqp::add_I($bitmap,1,Int),Int),
$bitmap
)),
(my int $bits = 0),
nqp::while(
$bitmap,
nqp::stmts(
($bits = $bits + nqp::atpos_i(
$nibble2bits,
nqp::bitand_I($bitmap,0x0f,Int)
)),
($bitmap := nqp::bitshiftr_I($bitmap,4,Int)),
)
),
$bits
),
0 # no significant bits
)
}
}
=begin pod
=head1 NAME
Bits - provide bit related functions
=head1 SYNOPSIS
use Bits; # exports "bit", "bits", "bitcnt"
say bit(8, 3); # 1000 -> True
say bit(7, 3); # 0111 -> False
say bits(8); # 1000 -> (3,).Seq
say bits(7); # 0111 -> (0,1,2).Seq
say bitcnt(8); # 1000 -> 1
say bitcnt(7); # 0111 -> 3
=head1 DESCRIPTION
This module exports a number of function to handle bits in unsigned integer
values.
=head1 SUBROUTINES
=head2 bit
sub bit(Int:D value, UInt:D bit --> Bool:D)
Takes a integer value and a bit number and returns whether that bit is set.
=head2 bits
sub bits(Int:D value --> Seq:D)
Takes a integer value and returns a C<Seq>uence of the bit numbers that are
significant in the value. For negative values, these are the bits that are 0.
=head2 bitcnt
sub bitcnt(Int:D value --> Int:D)
Takes a integer value and returns the number of significant bits that are set
in the value. For negative values, this is the number of bits that are 0.
=head1 AUTHOR
Elizabeth Mattijsen <liz@wenzperl.nl>
Source can be located at: https://github.com/lizmat/Bits . Comments and Pull
Requests are welcome.
=head1 COPYRIGHT AND LICENSE
Copyright 2019 Elizabeth Mattijsen
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod
# vim: ft=perl6 expandtab sw=4