Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/ruby-vpi/integer.rb
Overview
NOTE: Because integers are immediate values in Ruby, these methods cannot modify the value of the integer upon which they are invoked. Instead, they return the new value as their result.
Instance Method Summary collapse
-
#extend_sign(aOrigWidth, aExtWidth) ⇒ Object
Performs sign extension on this integer, which has the given width (number of bits), so that the result will have the given extended width (number of bits).
-
#length ⇒ Object
Returns the minimum number of bits necessary to represent this integer.
-
#limit ⇒ Object
Returns the lowest upper-bound of this integer.
-
#log2 ⇒ Object
Returns the ceiling of the logarithm (base 2) of this positive integer.
-
#mask ⇒ Object
(also: #max)
Returns a bit-mask capable of masking this integer.
-
#pack(aPackedWidth) ⇒ Object
Transforms this infinite-length Ruby integer into a fixed-length integer (represented in two’s complement form) that has the given width (number of bits).
-
#split(aWidth = 8) ⇒ Object
Splits this integer into an array of smaller integers, each of which have the given positive, non-zero width (number of bits).
-
#to_limit ⇒ Object
Returns the lowest upper-bound of an integer with this number of bits.
-
#to_mask ⇒ Object
(also: #to_max)
Returns a bit-mask capable of masking an integer with this number of bits.
-
#unpack(aPackedWidth) ⇒ Object
Transforms this fixed-length integer (represented in two’s complement form) that has the given width (number of bits) into an infinite-length Ruby integer.
Instance Method Details
#extend_sign(aOrigWidth, aExtWidth) ⇒ Object
Performs sign extension on this integer, which has the given width (number of bits), so that the result will have the given extended width (number of bits).
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby-vpi/integer.rb', line 97 def extend_sign aOrigWidth, aExtWidth result = self maskWidth = aExtWidth - aOrigWidth if maskWidth > 0 && result[aOrigWidth - 1] == 1 result |= (maskWidth.to_mask << aOrigWidth) end result & aExtWidth.to_mask end |
#length ⇒ Object
Returns the minimum number of bits necessary to represent this integer.
23 24 25 |
# File 'lib/ruby-vpi/integer.rb', line 23 def length to_s(2).length end |
#limit ⇒ Object
Returns the lowest upper-bound of this integer. This integer cannot reach the limit without occupying more bits in its binary representation.
29 30 31 |
# File 'lib/ruby-vpi/integer.rb', line 29 def limit length.to_limit end |
#log2 ⇒ Object
Returns the ceiling of the logarithm (base 2) of this positive integer.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby-vpi/integer.rb', line 11 def log2 raise "integer must be positive" if self < 0 bin = to_s(2) if bin =~ /^10+$/ bin.length - 1 else bin.length end end |
#mask ⇒ Object Also known as: max
Returns a bit-mask capable of masking this integer.
40 41 42 |
# File 'lib/ruby-vpi/integer.rb', line 40 def mask length.to_mask end |
#pack(aPackedWidth) ⇒ Object
Transforms this infinite-length Ruby integer into a fixed-length integer (represented in two’s complement form) that has the given width (number of bits).
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby-vpi/integer.rb', line 62 def pack aPackedWidth bits = length bits += 1 if self > 0 # positive integers also have a sign bit (zero) unless aPackedWidth >= bits raise ArgumentError, "packed width #{aPackedWidth} must be at least #{bits} for integer #{self}" end extend_sign(bits, aPackedWidth) end |
#split(aWidth = 8) ⇒ Object
Splits this integer into an array of smaller integers, each of which have the given positive, non-zero width (number of bits). These smaller integers are ordered from left to right, in the same way that humans write unsigned binary numbers; for example:
>> 6.split 1
> [1, 1, 0]
>> 6.split(1).map {|i| i.to_s 2}
> [“1”, “1”, “0”]
>> 6.split 2
> [1, 2]
>> 6.split(2).map {|i| i.to_s 2}
> [“1”, “10”]
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ruby-vpi/integer.rb', line 121 def split aWidth = 8 raise ArgumentError, "width must be positive and non-zero" unless aWidth > 0 int, bits = self, length mask = aWidth.to_mask words = [] while bits > 0 words.unshift int & mask int >>= aWidth bits -= aWidth end words end |
#to_limit ⇒ Object
Returns the lowest upper-bound of an integer with this number of bits.
34 35 36 |
# File 'lib/ruby-vpi/integer.rb', line 34 def to_limit 2 ** self end |
#to_mask ⇒ Object Also known as: to_max
Returns a bit-mask capable of masking an integer with this number of bits.
45 46 47 |
# File 'lib/ruby-vpi/integer.rb', line 45 def to_mask to_limit - 1 end |
#unpack(aPackedWidth) ⇒ Object
Transforms this fixed-length integer (represented in two’s complement form) that has the given width (number of bits) into an infinite-length Ruby integer.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby-vpi/integer.rb', line 76 def unpack aPackedWidth bits = length unless aPackedWidth >= bits raise ArgumentError, "packed width #{aPackedWidth} must be at least #{bits} for integer #{self}" end mask = aPackedWidth.to_mask result = self & mask if result[aPackedWidth - 1] == 1 -((-result) & mask) else result end end |