Class: Hornetseye::GCCValue

Inherits:
Object show all
Defined in:
lib/multiarray/gccvalue.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, descriptor) ⇒ GCCValue

Returns a new instance of GCCValue.



89
90
91
92
# File 'lib/multiarray/gccvalue.rb', line 89

def initialize( function, descriptor ) 
  @function = function
  @descriptor = descriptor
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



87
88
89
# File 'lib/multiarray/gccvalue.rb', line 87

def function
  @function
end

Class Method Details

.define_binary_method(mod, op, opcode = op) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/multiarray/gccvalue.rb', line 69

def define_binary_method( mod, op, opcode = op )
  mod.module_eval do
    define_method( "#{op}_with_gcc" ) do |a,b|
      if a.is_a? GCCValue or b.is_a? GCCValue
        function = a.is_a?( GCCValue ) ? a.function : b.function
        GCCValue.new function, "#{opcode}( #{a}, #{b} )"
      else
        send "#{op}_without_gcc", a, b
      end
    end
    alias_method_chain op, :gcc
    module_function "#{op}_without_gcc"
    module_function op
  end
end

.define_binary_op(op, opcode = op) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/multiarray/gccvalue.rb', line 58

def define_binary_op( op, opcode = op )
  define_method( op ) do |other|
    if GCCValue.generic? other
      GCCValue.new @function, "( #{self} ) #{opcode} ( #{other} )"
    else
      x, y = other.coerce self
      x.send op, y
    end
  end
end

.define_unary_method(mod, op, opcode = op) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/multiarray/gccvalue.rb', line 43

def define_unary_method( mod, op, opcode = op )
  mod.module_eval do
    define_method( "#{op}_with_gcc" ) do |a|
      if a.is_a? GCCValue
        GCCValue.new a.function, "#{opcode}( #{a} )"
      else
        send "#{op}_without_gcc", a
      end
    end
    alias_method_chain op, :gcc
    module_function "#{op}_without_gcc"
    module_function op
  end
end

.define_unary_op(op, opcode = op) ⇒ Object



37
38
39
40
41
# File 'lib/multiarray/gccvalue.rb', line 37

def define_unary_op( op, opcode = op )
  define_method( op ) do
    GCCValue.new @function, "#{opcode}( #{self} )"
  end
end

.generic?(value) ⇒ Boolean

Check compatibility of other type.

This method checks whether binary operations with the other Ruby object can be performed without requiring coercion.

Parameters:

  • value (Object)

    The other Ruby object.

Returns:

  • (Boolean)

    Returns false if Ruby object requires coercion.



32
33
34
35
# File 'lib/multiarray/gccvalue.rb', line 32

def generic?( value )
  value.is_a?( GCCValue ) or value.is_a?( Fixnum ) or
    value.is_a?( Float )
end

Instance Method Details

#**(other) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/multiarray/gccvalue.rb', line 236

def **( other )
  if GCCValue.generic? other
    GCCValue.new @function, "pow( #{self}, #{other} )"
  else
    x, y = other.coerce self
    x ** y
  end
end

#absObject



144
145
146
# File 'lib/multiarray/gccvalue.rb', line 144

def abs
  ( self >= 0 ).conditional self, -self
end

#argObject



148
149
150
# File 'lib/multiarray/gccvalue.rb', line 148

def arg
  ( self >= 0 ).conditional 0, Math::PI
end

#bObject



160
161
162
# File 'lib/multiarray/gccvalue.rb', line 160

def b
  self
end

#ceilObject



228
229
230
# File 'lib/multiarray/gccvalue.rb', line 228

def ceil
  GCCValue.new @function, "ceil( #{self} )"
end

#coerce(other) ⇒ Object



277
278
279
280
281
282
283
# File 'lib/multiarray/gccvalue.rb', line 277

def coerce( other )
  if other.is_a? GCCValue
    return other, self
  else
    return GCCValue.new( @function, "( #{other} )" ), self
  end
end

#compilable?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/multiarray/gccvalue.rb', line 118

def compilable?
  false
end

#conditional(a, b) ⇒ Object



172
173
174
# File 'lib/multiarray/gccvalue.rb', line 172

def conditional( a, b )
  GCCValue.new @function, "( #{self} ) ? ( #{a} ) : ( #{b} )"
end

#conjObject



140
141
142
# File 'lib/multiarray/gccvalue.rb', line 140

def conj
  self
end

#floorObject



224
225
226
# File 'lib/multiarray/gccvalue.rb', line 224

def floor
  GCCValue.new @function, "floor( #{self} )"
end

#gObject



156
157
158
# File 'lib/multiarray/gccvalue.rb', line 156

def g
  self
end

#imagObject



168
169
170
# File 'lib/multiarray/gccvalue.rb', line 168

def imag
  0
end

#inspectString

Display descriptor of this object

Returns:

  • (String)

    Returns the descriptor of this object.



97
98
99
# File 'lib/multiarray/gccvalue.rb', line 97

def inspect
  @descriptor
end

#load(typecode) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/multiarray/gccvalue.rb', line 122

def load( typecode )
  offset = 0
  typecode.typecodes.collect do |t|
    value = GCCValue.new @function,
      "*(#{GCCType.new( t ).identifier} *)( #{self} + #{offset} )"
    offset += t.storage_size
    value
  end
end

#major(other) ⇒ Object



245
246
247
248
# File 'lib/multiarray/gccvalue.rb', line 245

def major( other )
  GCCValue.new @function,
    "( ( #{self} ) >= ( #{other} ) ) ? ( #{self} ) : ( #{other} )"
end

#minor(other) ⇒ Object



250
251
252
253
# File 'lib/multiarray/gccvalue.rb', line 250

def minor( other )
  GCCValue.new @function,
    "( ( #{self} ) <= ( #{other} ) ) ? ( #{self} ) : ( #{other} )"
end

#nonzero?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/multiarray/gccvalue.rb', line 220

def nonzero?
  GCCValue.new @function, "( #{self} ) != 0"
end

#rObject



152
153
154
# File 'lib/multiarray/gccvalue.rb', line 152

def r
  self
end

#realObject



164
165
166
# File 'lib/multiarray/gccvalue.rb', line 164

def real
  self
end

#roundObject



232
233
234
# File 'lib/multiarray/gccvalue.rb', line 232

def round
  GCCValue.new @function, "round( #{self} )"
end

#save(value) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/multiarray/gccvalue.rb', line 132

def save( value )
  offset = 0
  value.class.typecodes.zip( value.values ).each do |t,v|
    @function << "#{@function.indent}*(#{GCCType.new( t ).identifier} *)( #{self} + #{offset} ) = #{v};\n"
    offset += t.storage_size
  end
end

#store(value) ⇒ Object

Store new value in this object

Parameters:

  • value (Object)

    The new value.

Returns:



113
114
115
116
# File 'lib/multiarray/gccvalue.rb', line 113

def store( value )
  @function << "#{@function.indent}#{self} = #{value};\n"
  value
end

#times(&action) ⇒ Object



255
256
257
258
259
260
261
262
263
264
# File 'lib/multiarray/gccvalue.rb', line 255

def times( &action )
  i = @function.variable INT, 'i'
  @function << "#{@function.indent}for ( #{i} = 0; " +
               "#{i} != #{self}; #{i}++ ) {\n"
  @function.indent_offset +1
  action.call i
  @function.indent_offset -1
  @function << "#{@function.indent}};\n"
  self
end

#to_sString

Get descriptor of this object

Returns:

  • (String)

    Returns the descriptor of this object.



104
105
106
# File 'lib/multiarray/gccvalue.rb', line 104

def to_s
  @descriptor
end

#upto(other, &action) ⇒ Object



266
267
268
269
270
271
272
273
274
275
# File 'lib/multiarray/gccvalue.rb', line 266

def upto( other, &action )
  i = @function.variable INT, 'i'
  @function << "#{@function.indent}for ( #{i} = #{self}; " +
               "#{i} != #{ other + 1 }; #{i}++ ) {\n"
  @function.indent_offset +1
  action.call i
  @function.indent_offset -1
  @function << "#{@function.indent}};\n"
  self
end

#zero?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/multiarray/gccvalue.rb', line 216

def zero?
  GCCValue.new @function, "( #{self} ) == 0"
end