Module: Carbon::Core::Integer::Math Private

Included in:
Carbon::Core::Integer
Defined in:
lib/carbon/core/integer/math.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 math operations on almost all integers except booleans. Provides the following methods on all integer types except booleans:

  • .+<T: Carbon::Numeric>(self, T: other): self
  • .-<T: Carbon::Numeric>(self, T: other): self
  • .*<T: Carbon::Numeric>(self, T: other): self
  • ./<T: Carbon::Numeric>(self, T: other): self
  • .%<T: Carbon::Numeric>(self, T: other): self
  • .|<T: Carbon::Numeric>(self, T: other): self
  • .&<T: Carbon::Numeric>(self, T: other): self
  • .<<<T: Carbon::Numeric>(self, T: other): self
  • .>><T: Carbon::Numeric>(self, T: other): self
  • .^<T: Carbon::Numeric>(self, T: other): self
  • .==<T: Carbon::Numeric>(self, T: other): Carbon::Boolean
  • .===<T: Carbon::Numeric>(self, T: other): Carbon::Boolean
  • .<<T: Carbon::Numeric>(self, T: other): Carbon::Boolean
  • .><T: Carbon::Numeric>(self, T: other): Carbon::Boolean
  • .<=<T: Carbon::Numeric>(self, T: other): Carbon::Boolean
  • .>=<T: Carbon::Numeric>(self, T: other): Carbon::Boolean

Note that the core library never actually uses generics. Instead, all math operations are defined relative to another integer, and overloading is used to match to the right function. For example, Int32.+(Int32, Int8) is a different function than Int32.+(Int32, UInt8); the functions use no actual generics.

Constant Summary collapse

MATH_OPERATIONS =

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

The math operations that can be performed. Note that for LLVM, :/, :%, and :>> are sign-dependant.

Returns:

  • (<::Symbol>)
%i(+ - * / % | & << >> ^).freeze
COMP_OPERATIONS =

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

The comparison operations that can be performed. Note that for LLVM, :<, :>, :<=, and :>= are sign-dependant.

Returns:

  • (<::Symbol>)
%i(== === < > <= >=).freeze
COMP_OPERATIONS_MAP =

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

A mapping of comparison operations to their icmp equivalents. Since most of the icmp values are sign-dependant, the keys also contain sign information.

Returns:

  • ({(::Symbol, ::Symbol) => ::Symbol})
{
  [:unsigned, :==] => :eq, [:signed, :==] => :eq,
  [:unsigned, :===] => :eq, [:signed, :===] => :eq,
  [:unsigned, :<] => :ult, [:signed, :<] => :slt,
  [:unsigned, :>] => :ugt, [:signed, :>] => :sgt,
  [:unsigned, :<=] => :ule, [:signed, :<=] => :sle,
  [:unsigned, :>=] => :uge, [:signed, :>=] => :sge
}.deep_freeze!

Instance Method Summary collapse

Instance Method Details

#define_comp_function(left, right, op) ⇒ 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 a compare function. The left and right integer types are given, as well as the specific operation.

Parameters:



108
109
110
111
112
113
114
# File 'lib/carbon/core/integer/math.rb', line 108

def define_comp_function(left, right, op)
  function_name = left.name.call(op, [left.name, right.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_comp_definition(left, right, op, function[:definition])
  end
end

#define_comp_functions(left, right) ⇒ 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 all of the comparison functinos. The left and right integer types are given, and the COMP_OPERATIONS are iterated over, providing the operator for the #define_comp_function method.

Parameters:



80
81
82
# File 'lib/carbon/core/integer/math.rb', line 80

def define_comp_functions(left, right)
  COMP_OPERATIONS.each { |op| define_comp_function(left, right, op) }
end

#define_math_function(left, right, op) ⇒ 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 a math function. The left and right integer types are given, as well as the specific operation.

Parameters:



92
93
94
95
96
97
98
# File 'lib/carbon/core/integer/math.rb', line 92

def define_math_function(left, right, op)
  function_name = left.name.call(op, [left.name, right.name])
  Core.define(function: function_name) do |function|
    function[:return] = left.name
    define_math_definition(left, right, op, function[:definition])
  end
end

#define_math_functions(left, right) ⇒ 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 all of the math functions. The left and right integer types are given, and the MATH_OPERATIONS are iterated over, providing the operator for the #define_math_function method.

Parameters:



69
70
71
# File 'lib/carbon/core/integer/math.rb', line 69

def define_math_functions(left, right)
  MATH_OPERATIONS.each { |op| define_math_function(left, right, op) }
end