module Carbon::Core::Integer::Pole

Defines “polarity” functions. This means positive/negative or odd/even. This defines the following functions:

These functions are defined on all integer types except boolean. That includes `.positive?` and `.negative?`, which has no meaning on an unisgned integer (see {#define_positive_function} and {#define_negative_function} for how unsigned integers are handled).

@api private

Public Instance Methods

define_even_function(int) click to toggle source

Defines the even function. The function (named `even?`) returns a boolean value; if the receiver is even (i.e. evenly divisible by two), the function returns `true`; otherwise, the function returns `false`. It performs the check by checking the least significant bit (`trunc %i to i1`).

@param int [Core::Int] The (receiver) value. @return [void]

# File lib/carbon/core/integer/pole.rb, line 66
def define_even_function(int)
  function_name = int.name.call("even?", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_even_definition(int, function[:definition])
  end
end
define_negative_function(int) click to toggle source

Defines the negative function. The function (named `negative?`) returns a boolean value; if the receiver is less than zero, the function returns `true`; otherwise, the function returns `false`. This provides a special case for unsigned integers: all unsigned integers are always positive, so this function returns `false` for unsigned integers. For signed integers, it checks the sign bit (`xor (lshr %i, (sub %i.size, 1)), 1`).

@param int [Core::Int] The (receiver) value. @return [void]

# File lib/carbon/core/integer/pole.rb, line 50
def define_negative_function(int)
  function_name = int.name.call("negative?", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_negative_definition(int, function[:definition])
  end
end
define_odd_function(int) click to toggle source

Defines the odd function. The function (named `odd?`) returns a boolean value; if the receiver is odd (i.e. not evenly divisible by two), the function returns `true`; otherwise, the function returns `false`. It performs the check by checking the least significant bit (`xor (trunc %i to i1), 1`).

@param int [Core::Int] The (receiver) value. @return [void]

# File lib/carbon/core/integer/pole.rb, line 82
def define_odd_function(int)
  function_name = int.name.call("odd?", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_odd_definition(int, function[:definition])
  end
end
define_positive_function(int) click to toggle source

Defines the positive function. The function (named `positive?`) returns a boolean value; if the receiver is greater than or equal to zero, the function returns `true`; otherwise, the function returns `false`. This provides a special case for unsigned integers: all unsigned integers are always positive, so this function returns `true` for unsigned integers. For signed integers, it checks the sign bit (`lshr %i, (sub %i.size, 1)`).

@param int [Core::Int] The (receiver) value. @return [void]

# File lib/carbon/core/integer/pole.rb, line 32
def define_positive_function(int)
  function_name = int.name.call("positive?", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_positive_definition(int, function[:definition])
  end
end

Private Instance Methods

define_even_definition(_int, definition) click to toggle source
# File lib/carbon/core/integer/pole.rb, line 115
def define_even_definition(_int, definition)
  entry = definition.add("entry").build
  this = definition.params[0]
  this.name = "self"

  trunc = entry.trunc(this, Carbon::Boolean).as(Carbon::Boolean)
  xor = entry.xor(trunc, 1).as(Carbon::Boolean)
  entry.ret(xor)
end
define_negative_definition(int, definition) click to toggle source
# File lib/carbon/core/integer/pole.rb, line 103
def define_negative_definition(int, definition)
  entry = definition.add("entry").build
  this = definition.params[0]
  this.name = "self"

  return entry.ret(0) if int.sign == :unsigned
  sign = entry.lshr(this, int.size - 1).as(this.type)
  trunc = entry.trunc(sign, Carbon::Boolean).as(Carbon::Boolean)
  negate = entry.xor(trunc, 1).as(Carbon::Boolean)
  entry.ret(negate)
end
define_odd_definition(_int, definition) click to toggle source
# File lib/carbon/core/integer/pole.rb, line 125
def define_odd_definition(_int, definition)
  entry = definition.add("entry").build
  this = definition.params[0]
  this.name = "self"

  entry.ret(entry.trunc(this, Carbon::Boolean).as(Carbon::Boolean))
end
define_positive_definition(int, definition) click to toggle source
# File lib/carbon/core/integer/pole.rb, line 92
def define_positive_definition(int, definition)
  entry = definition.add("entry").build
  this = definition.params[0]
  this.name = "self"

  return entry.ret(1) if int.sign == :unsigned
  sign = entry.lshr(this, int.size - 1).as(this.type)
  trunc = entry.trunc(sign, Carbon::Boolean).as(Carbon::Boolean)
  entry.ret(trunc)
end