Module: Carbon::Core::Integer::Pole Private

Included in:
Carbon::Core::Integer
Defined in:
lib/carbon/core/integer/pole.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

  • .positive?(self): Carbon::Boolean
  • .negative?(self): Carbon::Boolean
  • .even?(self): Carbon::Boolean
  • .odd?(self): Carbon::Boolean

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).

Instance Method Summary collapse

Instance Method Details

#define_even_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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).

Parameters:



66
67
68
69
70
71
72
# 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) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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).

Parameters:



50
51
52
53
54
55
56
# 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) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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).

Parameters:



82
83
84
85
86
87
88
# 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) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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)).

Parameters:



32
33
34
35
36
37
38
# 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