Class: Hornetseye::GCCValue
Overview
Class for generating code handling C values
Instance Attribute Summary collapse
-
#function ⇒ GCCFunction
readonly
Get current function context.
Class Method Summary collapse
-
.define_binary_method(mod, op, opcode = op) ⇒ Proc
Meta-programming method used to define binary methods at the beginning.
-
.define_binary_op(op, opcode = op) ⇒ Proc
Meta-programming method used to define unary methods at the beginning.
-
.define_unary_method(mod, op, opcode = op) ⇒ Proc
Meta-programming method used to define unary methods at the beginning.
-
.define_unary_op(op, opcode = op) ⇒ Proc
Meta-programming method used to define unary operations at the beginning.
-
.generic?(value) ⇒ Boolean
Check compatibility of other type.
Instance Method Summary collapse
-
#**(other) ⇒ GCCValue
Generate code for computing exponentiation.
-
#abs ⇒ GCCValue
Generate code for computing absolute value.
-
#arg ⇒ GCCValue
Generate code for computing complex argument of real value.
-
#assign(value) ⇒ Object
Store new value in this object.
-
#b ⇒ GCCValue
Blue colour component of real value.
-
#ceil ⇒ GCCValue
Generate code for computing smallest integer value not less than this value.
-
#coerce(other) ⇒ Array<GCCValue>
Type coercion for GCC values.
-
#compilable? ⇒ Boolean
Indicate whether this object can be compiled.
-
#conditional(a, b) ⇒ GCCValue
Create code for conditional selection of value.
-
#conditional_with_complex(a, b) ⇒ GCCValue
Create code for conditional selection of complex value.
-
#conditional_with_rgb(a, b) ⇒ GCCValue
Create code for conditional selection of RGB value.
-
#conj ⇒ GCCValue
Complex conjugate of real value.
-
#drand ⇒ GCCValue
Generate floating point random number.
-
#floor ⇒ GCCValue
Generate code for computing largest integer value not greater than this value.
- #fmod(other) ⇒ Object
-
#g ⇒ GCCValue
Green colour component of real value.
-
#if(&action) ⇒ Object
Generate code for conditional statement.
-
#if_else(action1, action2) ⇒ Object
Generate code for conditional statement.
-
#imag ⇒ Integer
Imaginary component of real value.
-
#initialize(function, descriptor) ⇒ GCCValue
constructor
Constructor for GCC value.
-
#inspect ⇒ String
Display descriptor of this object.
-
#load(typecode) ⇒ Array<GCCValue>
Add code to read all components of a typed value from memory.
-
#lrand ⇒ GCCValue
Generate integer random number.
-
#major(other) ⇒ GCCValue
Generate code for selecting larger value.
- #major_with_rgb(other) ⇒ Object
-
#minor(other) ⇒ GCCValue
Generate code for selecting smaller value.
- #minor_with_rgb(other) ⇒ Object
-
#nonzero? ⇒ GCCValue
Generate code for checking whether value is not equal to zero.
-
#r ⇒ GCCValue
Red colour component of real value.
-
#real ⇒ GCCValue
Real component of real value.
-
#round ⇒ GCCValue
Generate code for rounding to nearest integer.
-
#save(value) ⇒ Object
Add code to write all components of a typed value to memory.
-
#times(&action) ⇒ GCCValue
Generate a
for
loop in C. -
#to_s ⇒ String
Get descriptor of this object.
-
#upto(other, &action) ⇒ GCCValue
Generate a
for
loop in C. -
#zero? ⇒ GCCValue
Generate code for checking whether value is equal to zero.
Constructor Details
#initialize(function, descriptor) ⇒ GCCValue
Constructor for GCC value
136 137 138 139 |
# File 'lib/multiarray/gccvalue.rb', line 136 def initialize( function, descriptor ) @function = function @descriptor = descriptor end |
Instance Attribute Details
#function ⇒ GCCFunction (readonly)
Get current function context
128 129 130 |
# File 'lib/multiarray/gccvalue.rb', line 128 def function @function end |
Class Method Details
.define_binary_method(mod, op, opcode = op) ⇒ Proc
Meta-programming method used to define binary methods at the beginning
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/multiarray/gccvalue.rb', line 105 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) ⇒ Proc
Meta-programming method used to define unary methods at the beginning
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/multiarray/gccvalue.rb', line 86 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) ⇒ Proc
Meta-programming method used to define unary methods at the beginning
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/multiarray/gccvalue.rb', line 63 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) ⇒ Proc
Meta-programming method used to define unary operations at the beginning
49 50 51 52 53 |
# File 'lib/multiarray/gccvalue.rb', line 49 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.
36 37 38 39 |
# File 'lib/multiarray/gccvalue.rb', line 36 def generic?( value ) value.is_a?( GCCValue ) or value.is_a?( Fixnum ) or value.is_a?( Float ) end |
Instance Method Details
#**(other) ⇒ GCCValue
Generate code for computing exponentiation
437 438 439 440 441 442 443 444 |
# File 'lib/multiarray/gccvalue.rb', line 437 def **( other ) if GCCValue.generic? other GCCValue.new @function, "pow( #{self}, #{other} )" else x, y = other.coerce self x ** y end end |
#abs ⇒ GCCValue
Generate code for computing absolute value
224 225 226 |
# File 'lib/multiarray/gccvalue.rb', line 224 def abs ( self >= 0 ).conditional self, -self end |
#arg ⇒ GCCValue
Generate code for computing complex argument of real value
233 234 235 |
# File 'lib/multiarray/gccvalue.rb', line 233 def arg ( self >= 0 ).conditional 0, Math::PI end |
#assign(value) ⇒ Object
Store new value in this object
166 167 168 169 |
# File 'lib/multiarray/gccvalue.rb', line 166 def assign( value ) @function << "#{@function.indent}#{self} = #{value};\n" value end |
#b ⇒ GCCValue
Blue colour component of real value
260 261 262 |
# File 'lib/multiarray/gccvalue.rb', line 260 def b self end |
#ceil ⇒ GCCValue
Generate code for computing smallest integer value not less than this value
417 418 419 |
# File 'lib/multiarray/gccvalue.rb', line 417 def ceil GCCValue.new @function, "ceil( #{self} )" end |
#coerce(other) ⇒ Array<GCCValue>
Type coercion for GCC values
589 590 591 592 593 594 595 |
# File 'lib/multiarray/gccvalue.rb', line 589 def coerce( other ) if other.is_a? GCCValue return other, self else return GCCValue.new( @function, "( #{other} )" ), self end end |
#compilable? ⇒ Boolean
Indicate whether this object can be compiled
176 177 178 |
# File 'lib/multiarray/gccvalue.rb', line 176 def compilable? false end |
#conditional(a, b) ⇒ GCCValue
Create code for conditional selection of value
299 300 301 302 303 304 305 |
# File 'lib/multiarray/gccvalue.rb', line 299 def conditional( a, b ) if a.is_a?( Proc ) and b.is_a?( Proc ) conditional a.call, b.call else GCCValue.new @function, "( #{self} ) ? ( #{a} ) : ( #{b} )" end end |
#conditional_with_complex(a, b) ⇒ GCCValue
Create code for conditional selection of complex value
514 515 516 517 518 519 520 521 |
# File 'lib/multiarray/gccvalue.rb', line 514 def conditional_with_complex( a, b ) if a.is_a?( InternalComplex ) or b.is_a?( InternalComplex ) InternalComplex.new conditional( a.real, b.real ), conditional( a.imag, b.imag ) else conditional_without_complex a, b end end |
#conditional_with_rgb(a, b) ⇒ GCCValue
Create code for conditional selection of RGB value
496 497 498 499 500 501 502 |
# File 'lib/multiarray/gccvalue.rb', line 496 def conditional_with_rgb( a, b ) if a.is_a?(RGB) or b.is_a?(RGB) Hornetseye::RGB conditional(a.r, b.r), conditional(a.g, b.g), conditional(a.b, b.b) else conditional_without_rgb a, b end end |
#conj ⇒ GCCValue
Complex conjugate of real value
215 216 217 |
# File 'lib/multiarray/gccvalue.rb', line 215 def conj self end |
#drand ⇒ GCCValue
Generate floating point random number
451 452 453 |
# File 'lib/multiarray/gccvalue.rb', line 451 def drand GCCValue.new @function, "( rb_genrand_real() * ( #{self} ) )" end |
#floor ⇒ GCCValue
Generate code for computing largest integer value not greater than this value
408 409 410 |
# File 'lib/multiarray/gccvalue.rb', line 408 def floor GCCValue.new @function, "floor( #{self} )" end |
#fmod(other) ⇒ Object
282 283 284 285 286 287 288 289 |
# File 'lib/multiarray/gccvalue.rb', line 282 def fmod( other ) if GCCValue.generic? other GCCValue.new @function, "fmod( #{self}, #{other} )" else x, y = other.coerce self x.send op, y end end |
#g ⇒ GCCValue
Green colour component of real value
251 252 253 |
# File 'lib/multiarray/gccvalue.rb', line 251 def g self end |
#if(&action) ⇒ Object
Generate code for conditional statement
314 315 316 317 318 319 320 321 |
# File 'lib/multiarray/gccvalue.rb', line 314 def if( &action ) @function << "#{@function.indent}if ( #{self} ) {\n" @function.indent_offset +1 action.call @function.indent_offset -1 @function << "#{@function.indent}};\n" self end |
#if_else(action1, action2) ⇒ Object
Generate code for conditional statement
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/multiarray/gccvalue.rb', line 331 def if_else( action1, action2 ) @function << "#{@function.indent}if ( #{self} ) {\n" @function.indent_offset +1 action1.call @function.indent_offset -1 @function << "#{@function.indent}} else {\n" @function.indent_offset +1 action2.call @function.indent_offset -1 @function << "#{@function.indent}};\n" self end |
#imag ⇒ Integer
Imaginary component of real value
278 279 280 |
# File 'lib/multiarray/gccvalue.rb', line 278 def imag 0 end |
#inspect ⇒ String
Display descriptor of this object
146 147 148 |
# File 'lib/multiarray/gccvalue.rb', line 146 def inspect @descriptor end |
#load(typecode) ⇒ Array<GCCValue>
Add code to read all components of a typed value from memory
185 186 187 188 189 190 191 192 193 |
# File 'lib/multiarray/gccvalue.rb', line 185 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 |
#lrand ⇒ GCCValue
Generate integer random number
460 461 462 |
# File 'lib/multiarray/gccvalue.rb', line 460 def lrand GCCValue.new @function, "limited_rand( #{self} )" end |
#major(other) ⇒ GCCValue
Generate code for selecting larger value
471 472 473 474 |
# File 'lib/multiarray/gccvalue.rb', line 471 def major( other ) GCCValue.new @function, "( ( #{self} ) >= ( #{other} ) ) ? ( #{self} ) : ( #{other} )" end |
#major_with_rgb(other) ⇒ Object
525 526 527 528 529 530 531 |
# File 'lib/multiarray/gccvalue.rb', line 525 def major_with_rgb(other) if other.is_a?(RGB) Hornetseye::RGB r.major(other.r), g.major(other.g), b.major(other.b) else major_without_rgb other end end |
#minor(other) ⇒ GCCValue
Generate code for selecting smaller value
483 484 485 486 |
# File 'lib/multiarray/gccvalue.rb', line 483 def minor( other ) GCCValue.new @function, "( ( #{self} ) <= ( #{other} ) ) ? ( #{self} ) : ( #{other} )" end |
#minor_with_rgb(other) ⇒ Object
535 536 537 538 539 540 541 |
# File 'lib/multiarray/gccvalue.rb', line 535 def minor_with_rgb(other) if other.is_a?(RGB) Hornetseye::RGB r.minor(other.r), g.minor(other.g), b.minor(other.b) else minor_without_rgb other end end |
#nonzero? ⇒ GCCValue
Generate code for checking whether value is not equal to zero
399 400 401 |
# File 'lib/multiarray/gccvalue.rb', line 399 def nonzero? GCCValue.new @function, "( #{self} ) != 0" end |
#r ⇒ GCCValue
Red colour component of real value
242 243 244 |
# File 'lib/multiarray/gccvalue.rb', line 242 def r self end |
#real ⇒ GCCValue
Real component of real value
269 270 271 |
# File 'lib/multiarray/gccvalue.rb', line 269 def real self end |
#round ⇒ GCCValue
Generate code for rounding to nearest integer
426 427 428 |
# File 'lib/multiarray/gccvalue.rb', line 426 def round GCCValue.new @function, "round( #{self} )" end |
#save(value) ⇒ Object
Add code to write all components of a typed value to memory
202 203 204 205 206 207 208 |
# File 'lib/multiarray/gccvalue.rb', line 202 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 |
#times(&action) ⇒ GCCValue
Generate a for
loop in C
552 553 554 555 556 557 558 559 560 561 |
# File 'lib/multiarray/gccvalue.rb', line 552 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_s ⇒ String
Get descriptor of this object
155 156 157 |
# File 'lib/multiarray/gccvalue.rb', line 155 def to_s @descriptor end |
#upto(other, &action) ⇒ GCCValue
Generate a for
loop in C
571 572 573 574 575 576 577 578 579 580 |
# File 'lib/multiarray/gccvalue.rb', line 571 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 |