mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
dbb4f19969
```
$ benchmark-driver -v --rbenv 'before --jit;after --jit' benchmark/mjit_integer.yml --filter '(comp|uminus)'
before --jit: ruby 3.0.0dev (2020-12-23T05:41:44Z master 0dd4896175
) +JIT [x86_64-linux]
after --jit: ruby 3.0.0dev (2020-12-23T06:25:41Z master 8887d78992) +JIT [x86_64-linux]
last_commit=Allow inlining Integer#-@ and #~
Calculating -------------------------------------
before --jit after --jit
mjit_comp(1) 44.006M 70.417M i/s - 40.000M times in 0.908967s 0.568042s
mjit_uminus(1) 44.333M 68.422M i/s - 40.000M times in 0.902255s 0.584603s
Comparison:
mjit_comp(1)
after --jit: 70417331.4 i/s
before --jit: 44005980.4 i/s - 1.60x slower
mjit_uminus(1)
after --jit: 68422468.8 i/s
before --jit: 44333371.0 i/s - 1.54x slower
```
150 lines
3.7 KiB
Ruby
150 lines
3.7 KiB
Ruby
class Integer
|
|
# call-seq:
|
|
# -int -> integer
|
|
#
|
|
# Returns +int+, negated.
|
|
def -@
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_uminus(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# ~int -> integer
|
|
#
|
|
# One's complement: returns a number where each bit is flipped.
|
|
#
|
|
# Inverts the bits in an Integer. As integers are conceptually of
|
|
# infinite length, the result acts as if it had an infinite number of
|
|
# one bits to the left. In hex representations, this is displayed
|
|
# as two periods to the left of the digits.
|
|
#
|
|
# sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"
|
|
def ~
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_comp(self)'
|
|
end
|
|
|
|
def abs
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_abs(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# int.bit_length -> integer
|
|
#
|
|
# Returns the number of bits of the value of +int+.
|
|
#
|
|
# "Number of bits" means the bit position of the highest bit
|
|
# which is different from the sign bit
|
|
# (where the least significant bit has bit position 1).
|
|
# If there is no such bit (zero or minus one), zero is returned.
|
|
#
|
|
# I.e. this method returns <i>ceil(log2(int < 0 ? -int : int+1))</i>.
|
|
#
|
|
# (-2**1000-1).bit_length #=> 1001
|
|
# (-2**1000).bit_length #=> 1000
|
|
# (-2**1000+1).bit_length #=> 1000
|
|
# (-2**12-1).bit_length #=> 13
|
|
# (-2**12).bit_length #=> 12
|
|
# (-2**12+1).bit_length #=> 12
|
|
# -0x101.bit_length #=> 9
|
|
# -0x100.bit_length #=> 8
|
|
# -0xff.bit_length #=> 8
|
|
# -2.bit_length #=> 1
|
|
# -1.bit_length #=> 0
|
|
# 0.bit_length #=> 0
|
|
# 1.bit_length #=> 1
|
|
# 0xff.bit_length #=> 8
|
|
# 0x100.bit_length #=> 9
|
|
# (2**12-1).bit_length #=> 12
|
|
# (2**12).bit_length #=> 13
|
|
# (2**12+1).bit_length #=> 13
|
|
# (2**1000-1).bit_length #=> 1000
|
|
# (2**1000).bit_length #=> 1001
|
|
# (2**1000+1).bit_length #=> 1001
|
|
#
|
|
# This method can be used to detect overflow in Array#pack as follows:
|
|
#
|
|
# if n.bit_length < 32
|
|
# [n].pack("l") # no overflow
|
|
# else
|
|
# raise "overflow"
|
|
# end
|
|
def bit_length
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_bit_length(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# int.even? -> true or false
|
|
#
|
|
# Returns +true+ if +int+ is an even number.
|
|
def even?
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_even_p(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# int.integer? -> true
|
|
#
|
|
# Since +int+ is already an Integer, this always returns +true+.
|
|
def integer?
|
|
return true
|
|
end
|
|
|
|
def magnitude
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_abs(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# int.odd? -> true or false
|
|
#
|
|
# Returns +true+ if +int+ is an odd number.
|
|
def odd?
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_odd_p(self)'
|
|
end
|
|
|
|
# call-seq:
|
|
# int.ord -> self
|
|
#
|
|
# Returns the +int+ itself.
|
|
#
|
|
# 97.ord #=> 97
|
|
#
|
|
# This method is intended for compatibility to character literals
|
|
# in Ruby 1.9.
|
|
#
|
|
# For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
|
|
def ord
|
|
return self
|
|
end
|
|
|
|
# call-seq:
|
|
# int.to_i -> integer
|
|
#
|
|
# Since +int+ is already an Integer, returns +self+.
|
|
#
|
|
# #to_int is an alias for #to_i.
|
|
def to_i
|
|
return self
|
|
end
|
|
|
|
# call-seq:
|
|
# int.to_int -> integer
|
|
#
|
|
# Since +int+ is already an Integer, returns +self+.
|
|
def to_int
|
|
return self
|
|
end
|
|
|
|
# call-seq:
|
|
# int.zero? -> true or false
|
|
#
|
|
# Returns +true+ if +int+ has a zero value.
|
|
def zero?
|
|
Primitive.attr! 'inline'
|
|
Primitive.cexpr! 'rb_int_zero_p(self)'
|
|
end
|
|
end
|