Module: NArrayType

Defined in:
ext/cumo/narray/gen/narray_def.rb

Overview


Constant Summary collapse

UPCAST_SOURCES =

Each entry is [class name, C type, the macro that converts one element of that type to this one]. It mirrors the store_from list in spec.rb, whose macros every type defines. A class the two lists disagree on is simply left out below, which costs the caller the cast it would have saved and nothing else.

[
  ["HFloat",   "cumo_half",     "m_from_half"],
  ["BFloat",   "cumo_bfloat",   "m_from_bfloat"],
  ["DFloat",   "double",        "m_from_real"],
  ["SFloat",   "float",         "m_from_real"],
  ["Int64",    "int64_t",       "m_from_int64"],
  ["Int32",    "int32_t",       "m_from_int32"],
  ["Int16",    "int16_t",       "m_from_sint"],
  ["Int8",     "int8_t",        "m_from_sint"],
  ["UInt64",   "u_int64_t",     "m_from_uint64"],
  ["UInt32",   "u_int32_t",     "m_from_uint32"],
  ["UInt16",   "u_int16_t",     "m_from_sint"],
  ["UInt8",    "u_int8_t",      "m_from_sint"],
  ["SComplex", "cumo_scomplex", "m_from_scomplex"],
  ["DComplex", "cumo_dcomplex", "m_from_dcomplex"],
].freeze

Instance Method Summary collapse

Instance Method Details

#absorbed_typesObject

The types this one absorbs, as UPCAST_SOURCES triples. An operand of such a type is what a binary method would otherwise have ndloop cast to this one.



198
199
200
201
202
# File 'ext/cumo/narray/gen/narray_def.rb', line 198

def absorbed_types
  names = @opts[:absorbed] || []
  UPCAST_SOURCES.select { |name,| names.include?(name) }
    .map { |name, ctype, macro| { name: name, ctype: ctype, macro: macro, id: name.downcase } }
end

#class_alias(*args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'ext/cumo/narray/gen/narray_def.rb', line 145

def class_alias(*args)
  case a = @opts[:class_alias]
  when Array
  when nil
    a = @opts[:class_alias] = []
  else
    a = @opts[:class_alias] = [a]
  end
  a.concat(args)
end

#math_varObject



117
118
119
# File 'ext/cumo/narray/gen/narray_def.rb', line 117

def math_var
  @opts[:math_var] ||= "cumo_m" + class_name + "Math"
end

#real_class_name(arg = nil) ⇒ Object



121
122
123
124
125
126
127
# File 'ext/cumo/narray/gen/narray_def.rb', line 121

def real_class_name(arg=nil)
  if arg.nil?
    @opts[:real_class_name] ||= class_name
  else
    @opts[:real_class_name] = arg
  end
end

#real_ctype(arg = nil) ⇒ Object



129
130
131
132
133
134
135
# File 'ext/cumo/narray/gen/narray_def.rb', line 129

def real_ctype(arg=nil)
  if arg.nil?
    @opts[:real_ctype] ||= ctype
  else
    @opts[:real_ctype] = arg
  end
end

#real_type_nameObject



141
142
143
# File 'ext/cumo/narray/gen/narray_def.rb', line 141

def real_type_name
  @opts[:real_type_name] ||= real_class_name.downcase
end

#real_type_varObject



137
138
139
# File 'ext/cumo/narray/gen/narray_def.rb', line 137

def real_type_var
  @opts[:real_type_var] ||= "cumo_c" + real_class_name
end

#type_nameObject Also known as: tp



108
109
110
# File 'ext/cumo/narray/gen/narray_def.rb', line 108

def type_name
  @opts[:type_name] ||= class_name.downcase
end

#type_varObject



113
114
115
# File 'ext/cumo/narray/gen/narray_def.rb', line 113

def type_var
  @opts[:type_var] ||= "cumo_c" + class_name
end

#upcast(c = nil, t = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/cumo/narray/gen/narray_def.rb', line 178

def upcast(c=nil, t=nil)
  @opts[:upcast] ||= []
  if c
    if t
      t = "cumo_c#{t}"
    else
      t = "cT"
    end
    # The def files spell the receiver either way, as cT or by its own name.
    if c != class_name && (t == "cT" || t == "cumo_c#{class_name}")
      (@opts[:absorbed] ||= []) << c
    end
    @opts[:upcast] << "rb_hash_aset(hCast, cumo_c#{c}, #{t});"
  else
    @opts[:upcast]
  end
end

#upcast_rb(c, t = nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/cumo/narray/gen/narray_def.rb', line 204

def upcast_rb(c, t=nil)
  @opts[:upcast] ||= []
  if t
    t = "cumo_c#{t}"
  else
    t = "cT"
  end
  if c == "Integer"
    @opts[:upcast] << "#ifdef RUBY_INTEGER_UNIFICATION"
    @opts[:upcast] << "rb_hash_aset(hCast, rb_cInteger, #{t});"
    @opts[:upcast] << "#else"
    @opts[:upcast] << "rb_hash_aset(hCast, rb_cFixnum, #{t});"
    @opts[:upcast] << "rb_hash_aset(hCast, rb_cBignum, #{t});"
    @opts[:upcast] << "#endif"
  else
    @opts[:upcast] << "rb_hash_aset(hCast, rb_c#{c}, #{t});"
  end
end