lib/ruby-vpi/integer.rb in ruby-vpi-15.0.2 vs lib/ruby-vpi/integer.rb in ruby-vpi-16.0.0
- old
+ new
@@ -1,26 +1,13 @@
-=begin
- Copyright 2006 Suraj N. Kurapati
+# Library for hardware-related integer operations.
+#--
+# Copyright 2006-2007 Suraj N. Kurapati
+# See the file named LICENSE for details.
- This file is part of Ruby-VPI.
-
- Ruby-VPI is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
-
- Ruby-VPI is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Ruby-VPI; if not, write to the Free Software Foundation,
- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-=end
-
-# 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.
+# 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.
class Integer
# Returns the ceiling of the logarithm (base 2) of this positive integer.
def log2
raise "integer must be positive" if self < 0
bin = to_s(2)
@@ -35,11 +22,12 @@
# Returns the minimum number of bits necessary to represent this integer.
def length
to_s(2).length
end
- # Returns the lowest upper-bound of this integer. This integer cannot reach the limit without occupying more bits in its binary representation.
+ # Returns the lowest upper-bound of this integer. This integer cannot reach
+ # the limit without occupying more bits in its binary representation.
def limit
length.to_limit
end
# Returns the lowest upper-bound of an integer with *this* number of bits.
@@ -57,37 +45,22 @@
def to_mask
to_limit - 1
end
- # Returns the maximum value representable by this integer without occupying more bits in its binary representation.
+ # Returns the maximum value representable by this integer without occupying
+ # more bits in its binary representation.
alias max mask
- # Returns the maximum value representable by an integer with *this* number of bits.
+ # Returns the maximum value representable by an integer with *this* number of
+ # bits.
alias to_max to_mask
- # Ensures that this integer is no less than the given limit.
- def ensure_min aLimit
- if self < aLimit
- aLimit
- else
- self
- end
- end
-
- # Ensures that this integer is no greater than the given limit.
- def ensure_max aLimit
- if self > aLimit
- aLimit
- else
- self
- end
- end
-
-
- # 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).
+ # 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).
def pack aPackedWidth
bits = length
bits += 1 if self > 0 # positive integers also have a sign bit (zero)
unless aPackedWidth >= bits
@@ -95,11 +68,13 @@
end
extend_sign(bits, aPackedWidth)
end
- # 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.
+ # 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.
def unpack aPackedWidth
bits = length
unless aPackedWidth >= bits
raise ArgumentError, "packed width #{aPackedWidth} must be at least #{bits} for integer #{self}"
@@ -114,11 +89,13 @@
result
end
end
- # 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).
+ # 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).
def extend_sign aOrigWidth, aExtWidth
result = self
maskWidth = aExtWidth - aOrigWidth
if maskWidth > 0 && result[aOrigWidth - 1] == 1
@@ -126,10 +103,13 @@
end
result & aExtWidth.to_mask
end
- # 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:
+ # 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"]