Class: Integer
- Defined in:
- lib/amp/dependencies/amp_support/ruby_amp_support.rb,
lib/amp/support/support.rb
Overview
Ruby versions of slow functions we’ve implemented in C
Instance Method Summary collapse
-
#byte_swap_64 ⇒ Integer
Used for byte-swapping a 64-bit double long.
-
#bytes ⇒ Object
(also: #byte, #b)
methods for converting between file sizes.
-
#gigabytes ⇒ Object
(also: #gigabyte, #gb)
methods for converting between file sizes.
-
#kilobytes ⇒ Object
(also: #kilobyte, #kb)
methods for converting between file sizes.
-
#megabytes ⇒ Object
(also: #megabyte, #mb)
methods for converting between file sizes.
-
#to_dirstate_symbol ⇒ Object
Converts an ascii value to a dirstate status symbol.
-
#to_signed(bits) ⇒ Object
Forces this integer to be negative if it’s supposed to be!.
-
#to_signed_16 ⇒ Integer
Returns the number as if it were a signed 16-bit integer.
-
#to_signed_32 ⇒ Integer
Returns the number as if it were a signed 32-bit integer.
Instance Method Details
#byte_swap_64 ⇒ Integer
Used for byte-swapping a 64-bit double long. Unfortuantely, this will invoke bignum logic, which is ridiculously slow. That’s why we have a C extension.
If the system is little endian, we work some magic. If the system is big endian, we just return self.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 14 def byte_swap_64 if Amp::Support::SYSTEM[:endian] == :little ((self >> 56)) | ((self & 0x00FF000000000000) >> 40) | ((self & 0x0000FF0000000000) >> 24) | ((self & 0x000000FF00000000) >> 8 ) | ((self & 0x00000000FF000000) << 8 ) | ((self & 0x0000000000FF0000) << 24) | ((self & 0x000000000000FF00) << 40) | ((self & 0x00000000000000FF) << 56) else self end end |
#bytes ⇒ Object Also known as: byte, b
methods for converting between file sizes
601 602 603 |
# File 'lib/amp/support/support.rb', line 601 def bytes self end |
#gigabytes ⇒ Object Also known as: gigabyte, gb
methods for converting between file sizes
622 623 624 |
# File 'lib/amp/support/support.rb', line 622 def gigabytes 1024 * megabytes end |
#kilobytes ⇒ Object Also known as: kilobyte, kb
methods for converting between file sizes
608 609 610 |
# File 'lib/amp/support/support.rb', line 608 def kilobytes 1024 * bytes end |
#megabytes ⇒ Object Also known as: megabyte, mb
methods for converting between file sizes
615 616 617 |
# File 'lib/amp/support/support.rb', line 615 def megabytes 1024 * kilobytes end |
#to_dirstate_symbol ⇒ Object
Converts an ascii value to a dirstate status symbol. Converts a fixnum, which is an ascii value, to a symbol representing a dirstate entry’s status. Since we don’t like passing around ‘n’, and want to pass around :normal, we need a fast lookup for ascii value -> symbol. The price we pay.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 52 def to_dirstate_symbol case self when 110 # "n".ord :normal when 63 # "?".ord :untracked when 97 # "a".ord :added when 109 # "m".ord :merged when 114 # "r".ord :removed else raise "No known hg value for #{self}" end end |
#to_signed(bits) ⇒ Object
Forces this integer to be negative if it’s supposed to be!
634 635 636 637 638 |
# File 'lib/amp/support/support.rb', line 634 def to_signed(bits) return to_signed_16 if bits == 16 return to_signed_32 if bits == 32 raise "Unexpected number of bits: #{bits}" end |
#to_signed_16 ⇒ Integer
Returns the number as if it were a signed 16-bit integer. Since unpack() always returns unsigned integers, we have to sign them here.
30 31 32 33 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 30 def to_signed_16 return self if self < 32785 return self - 65536 end |
#to_signed_32 ⇒ Integer
Returns the number as if it were a signed 32-bit integer. Since unpack() always returns unsigned integers, we have to sign them here.
40 41 42 43 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 40 def to_signed_32 return self if self < 2147483648 return self - 4294967296 end |