BOAST 0.995 → 0.996

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ module BOAST
2
+
3
+ class FuncCall
4
+ include BOAST::Arithmetic
5
+
6
+ @return_type
7
+ @options
8
+ def self.parens(*args,&block)
9
+ return self::new(*args,&block)
10
+ end
11
+
12
+ attr_reader :func_name
13
+ attr_reader :args
14
+ attr_accessor :prefix
15
+
16
+ def initialize(func_name, *args)
17
+ @func_name = func_name
18
+ if args.last.kind_of?(Hash) then
19
+ @options = args.last
20
+ @args = args[0..-2]
21
+ else
22
+ @args = args
23
+ end
24
+ @return_type = @options[:returns] if @options
25
+ end
26
+
27
+ def to_var
28
+ if @return_type then
29
+ if @return_type.kind_of?(Variable)
30
+ return @return_type.copy("#{self}")
31
+ else
32
+ return Variable::new("#{self}", @return_type)
33
+ end
34
+ end
35
+ return nil
36
+ end
37
+
38
+ def to_s
39
+ self.to_str
40
+ end
41
+
42
+ def to_str
43
+ return self.to_str_fortran if BOAST::get_lang == FORTRAN
44
+ return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
45
+ end
46
+ def to_str_fortran
47
+ s = ""
48
+ s += @prefix if @prefix
49
+ s += "#{func_name}(#{@args.join(", ")})"
50
+ end
51
+ def to_str_c
52
+ s = ""
53
+ s += @prefix if @prefix
54
+ s += "#{func_name}(#{@args.join(", ")})"
55
+ end
56
+ def print(final=true)
57
+ s=""
58
+ s += " "*BOAST::get_indent_level if final
59
+ s += self.to_str
60
+ s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
61
+ BOAST::get_output.puts s if final
62
+ return s
63
+ end
64
+ end
65
+
66
+ end
data/lib/BOAST/If.rb ADDED
@@ -0,0 +1,109 @@
1
+ module BOAST
2
+
3
+ class If
4
+ def self.parens(*args,&block)
5
+ return self::new(*args,&block)
6
+ end
7
+
8
+ attr_reader :conditions
9
+ def initialize(*conditions, &block)
10
+ @conditions = []
11
+ @blocks = []
12
+ if conditions.size == 0 then
13
+ raise "Illegal if construct!"
14
+ elsif conditions.size == 1 then
15
+ @conditions.push(conditions[0])
16
+ @blocks.push(block)
17
+ elsif conditions.size.even? then
18
+ (0..conditions.size-1).step(2) { |i|
19
+ @conditions[i/2] = conditions[i]
20
+ @blocks[i/2] = conditions[i+1]
21
+ }
22
+ else
23
+ (0..conditions.size-2).step(2) { |i|
24
+ @conditions[i/2] = conditions[i]
25
+ @blocks[i/2] = conditions[i+1]
26
+ }
27
+ @blocks.push(conditions.last)
28
+ end
29
+ end
30
+ def to_s(*args)
31
+ self.to_str(*args)
32
+ end
33
+ def to_str(condition, first= true)
34
+ return self.to_str_fortran(condition, first) if BOAST::get_lang == FORTRAN
35
+ return self.to_str_c(condition, first) if [C, CL, CUDA].include?( BOAST::get_lang )
36
+ end
37
+ def to_str_fortran(condition, first)
38
+ s = ""
39
+ if first then
40
+ s += "if ( #{condition} ) then"
41
+ else
42
+ if condition then
43
+ s += "else if ( #{condition} ) then"
44
+ else
45
+ s += "else"
46
+ end
47
+ end
48
+ return s
49
+ end
50
+ def to_str_c(condition, first)
51
+ s = ""
52
+ if first then
53
+ s += "if(#{condition}){"
54
+ else
55
+ if condition then
56
+ s += "} else if(#{condition}){"
57
+ else
58
+ s += "} else {"
59
+ end
60
+ end
61
+ return s
62
+ end
63
+ def print(*args)
64
+ s=""
65
+ s += " "*BOAST::get_indent_level
66
+ s += self.to_str(@conditions.first)
67
+ BOAST::increment_indent_level
68
+ BOAST::get_output.puts s
69
+ if @blocks.size > 0 then
70
+ if @blocks[0] then
71
+ @blocks[0].call(*args)
72
+ end
73
+ @blocks[1..-1].each_index { |indx|
74
+ BOAST::decrement_indent_level
75
+ s=""
76
+ s += " "*BOAST::get_indent_level
77
+ s += self.to_str(@conditions[1..-1][indx],false)
78
+ BOAST::increment_indent_level
79
+ BOAST::get_output.puts s
80
+ @blocks[1..-1][indx].call(*args)
81
+ }
82
+ self.close
83
+ end
84
+ return self
85
+ end
86
+ def close(final=true)
87
+ return self.close_fortran(final) if BOAST::get_lang == FORTRAN
88
+ return self.close_c(final) if [C, CL, CUDA].include?( BOAST::get_lang )
89
+ end
90
+ def close_c(final=true)
91
+ s = ""
92
+ BOAST::decrement_indent_level
93
+ s += " "*BOAST::get_indent_level if final
94
+ s += "}"
95
+ BOAST::get_output.puts s if final
96
+ return s
97
+ end
98
+ def close_fortran(final=true)
99
+ s = ""
100
+ BOAST::decrement_indent_level
101
+ s += " "*BOAST::get_indent_level if final
102
+ s += "end if"
103
+ BOAST::get_output.puts s if final
104
+ return s
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,123 @@
1
+ module BOAST
2
+ class Index < Expression
3
+ attr_reader :source
4
+ attr_reader :indexes
5
+ def initialize(source, indexes)
6
+ @source = source
7
+ @indexes = indexes
8
+ end
9
+
10
+ def to_var
11
+ var = @source.copy("#{self}", :const => nil, :constant => nil, :dim => nil, :dimension => nil, :direction => nil, :dir => nil)
12
+ return var
13
+ end
14
+
15
+ def to_s
16
+ self.to_str
17
+ end
18
+ def to_str
19
+ return self.to_str_fortran if BOAST::get_lang == FORTRAN
20
+ return self.to_str_c if [C, CL, CUDA].include?( BOAST::get_lang )
21
+ end
22
+ def to_str_fortran
23
+ s = ""
24
+ s += "#{@source}(#{@indexes.join(", ")})"
25
+ return s
26
+ end
27
+ def to_str_texture
28
+ raise "Unsupported language #{BOAST::get_lang} for texture!" if not [CL, CUDA].include?( BOAST::get_lang )
29
+ raise "Write is unsupported for textures!" if not ( @source.constant or @source.direction == :in )
30
+ dim_number = 1
31
+ if @source.dimension then
32
+ dim_number == @source.dimension.size
33
+ end
34
+ raise "Unsupported number of dimension: #{dim_number}!" if dim_number > 3
35
+ s = ""
36
+ if BOAST::get_lang == CL then
37
+ s += "as_#{@source.type.decl}("
38
+ s += "read_imageui(#{@source}, #{@source.sampler}, "
39
+ if dim_number == 1 then
40
+ s += "int2(#{@indexes[0]},0)"
41
+ else
42
+ if dim_number == 2 then
43
+ s += "int2("
44
+ else
45
+ s += "int3("
46
+ end
47
+ s += "#{@indexes.join(", ")})"
48
+ end
49
+ s += ")"
50
+ if @source.type.size == 4 then
51
+ s += ".x"
52
+ elsif @source.type.size == 8 then
53
+ s += ".xy"
54
+ end
55
+ s += ")"
56
+ else
57
+ s += "tex#{dim_number}Dfetch(#{@source},"
58
+ if dim_number == 1 then
59
+ s += "#{@indexes[0]}"
60
+ else
61
+ if dim_number == 2 then
62
+ s += "int2("
63
+ else
64
+ s += "int3("
65
+ end
66
+ s += "#{@indexes.join(", ")})"
67
+ end
68
+ s += ")"
69
+ end
70
+ return s
71
+ end
72
+ def to_str_c
73
+ return to_str_texture if @source.texture
74
+ dim = @source.dimension.first
75
+ if dim.val2 then
76
+ start = dim.val1
77
+ else
78
+ start = BOAST::get_array_start
79
+ end
80
+ sub = "#{@indexes.first} - (#{start})"
81
+ i=1
82
+ ss = ""
83
+ @source.dimension[0..-2].each{ |d|
84
+ if d.size then
85
+ ss += " * (#{d.size})"
86
+ elsif d.val2 then
87
+ ss += " * (#{d.val2} - (#{d.val1}) + 1)"
88
+ else
89
+ raise "Unkwown dimension size!"
90
+ end
91
+ dim = @source.dimension[i]
92
+ if dim.val2 then
93
+ start = dim.val1
94
+ else
95
+ start = BOAST::get_array_start
96
+ end
97
+ sub += " + (#{@indexes[i]} - (#{start}))"+ss
98
+ i+=1
99
+ }
100
+ if BOAST::get_replace_constants then
101
+ begin
102
+ # puts sub
103
+ indx = eval(sub)
104
+ indx = indx.to_i
105
+ # puts indx
106
+ return "#{@source.constant[indx]}"
107
+ rescue Exception => e
108
+ end
109
+ end
110
+ s = "#{@source}[" + sub + "]"
111
+ return s
112
+ end
113
+ def print(final=true)
114
+ s=""
115
+ s += " "*BOAST::get_indent_level if final
116
+ s += self.to_str
117
+ s += ";" if final and [C, CL, CUDA].include?( BOAST::get_lang )
118
+ BOAST::get_output.puts s if final
119
+ return s
120
+ end
121
+ end
122
+
123
+ end
@@ -5,7 +5,7 @@ module BOAST
5
5
  case BOAST::get_architecture
6
6
  when X86
7
7
  case type
8
- when Int
8
+ when BOAST::Int
9
9
  size = "#{type.size*8}"
10
10
  name = ""
11
11
  if type.total_size*8 > 64
@@ -22,7 +22,7 @@ module BOAST
22
22
  name += "u"
23
23
  end
24
24
  return name += size
25
- when Real
25
+ when BOAST::Real
26
26
  case type.size
27
27
  when 4
28
28
  return "ps" if type.vector_length > 1
@@ -34,13 +34,13 @@ module BOAST
34
34
  else
35
35
  raise "Undefined vector type!"
36
36
  end
37
- when ARM
37
+ when BOAST::ARM
38
38
  case type
39
- when Int
39
+ when BOAST::Int
40
40
  name = "#{ type.signed ? "s" : "u" }"
41
41
  name += "#{ type.size * 8}"
42
42
  return name
43
- when Real
43
+ when BOAST::Real
44
44
  return "f#{type.size*8}"
45
45
  else
46
46
  raise "Undefined vector type!"
@@ -52,7 +52,7 @@ module BOAST
52
52
 
53
53
  def Operator.convert(arg, type)
54
54
  case BOAST::get_architecture
55
- when X86
55
+ when BOAST::X86
56
56
  s1 = arg.type.total_size*8
57
57
  s2 = type.total_size*8
58
58
  n1 = get_vector_name(arg.type)
@@ -64,14 +64,14 @@ module BOAST
64
64
  elsif [s1, s2].max <= 512 then
65
65
  return "_mm512_cvt#{n1}_#{n2}( #{arg} )"
66
66
  end
67
- when ARM
67
+ when BOAST::ARM
68
68
  if type.class != arg.type.class then
69
69
  if type.size == arg.type.size then
70
70
  s = type.total_size*8
71
71
  n1 = get_vector_name(arg.type)
72
72
  n2 = get_vector_name(type)
73
73
  return "vcvt#{ s == 128 ? "q" : "" }_#{n2}_#{n1}( #{arg} )"
74
- elsif type.class == Real then
74
+ elsif type.class == BOAST::Real then
75
75
  intr = convert(arg, arg.type.copy(:size=>type.size))
76
76
  return convert(arg.copy(intr, :size => type.size ), type)
77
77
  else
@@ -80,9 +80,9 @@ module BOAST
80
80
  t2 = type.copy(:size => arg.type.size)
81
81
  n2 = get_vector_name( t2 )
82
82
  intr = "vcvt#{ s == 128 ? "q" : "" }_#{n2}_#{n1}( #{arg} )"
83
- return convert(Variable::from_type(intr, t2), type)
83
+ return convert(BOAST::Variable::from_type(intr, t2), type)
84
84
  end
85
- elsif type.class != Real then
85
+ elsif type.class != BOAST::Real then
86
86
  n = get_vector_name(arg.type)
87
87
  if type.size == arg.type.size then
88
88
  if type.signed == arg.type.signed then
@@ -106,17 +106,17 @@ module BOAST
106
106
  end
107
107
  end
108
108
 
109
- class BasicBinaryOperator < Operator
109
+ class BasicBinaryOperator < BOAST::Operator
110
110
 
111
111
  def BasicBinaryOperator.to_s(arg1, arg2, return_type)
112
112
  #puts "#{arg1.class} * #{arg2.class} : #{arg1} * #{arg2}"
113
- if BOAST::get_lang == C and (arg1.class == Variable and arg2.class == Variable) and (arg1.type.vector_length > 1 or arg2.type.vector_length > 1) then
113
+ if BOAST::get_lang == BOAST::C and (arg1.class == BOAST::Variable and arg2.class == BOAST::Variable) and (arg1.type.vector_length > 1 or arg2.type.vector_length > 1) then
114
114
  raise "Vectors have different length: #{arg1} #{arg1.type.vector_length}, #{arg2} #{arg2.type.vector_length}" if arg1.type.vector_length != arg2.type.vector_length
115
115
  #puts "#{arg1.type.signed} #{arg2.type.signed} #{return_type.type.signed}"
116
116
  return_name = get_vector_name(return_type.type)
117
117
  size = return_type.type.total_size * 8
118
118
  case BOAST::get_architecture
119
- when X86
119
+ when BOAST::X86
120
120
  if arg1.type != return_type.type
121
121
  a1 = convert(arg1, return_type.type)
122
122
  else
@@ -133,7 +133,7 @@ module BOAST
133
133
  end
134
134
  intr_name += "_#{intr_name_X86}_#{return_name}"
135
135
  return "#{intr_name}( #{a1}, #{a2} )"
136
- when ARM
136
+ when BOAST::ARM
137
137
  if arg1.type.class != return_type.type.class then
138
138
  a1 = convert(arg1, return_type.type)
139
139
  else
@@ -157,11 +157,11 @@ module BOAST
157
157
  end
158
158
  end
159
159
 
160
- class Set < Operator
160
+ class Set < BOAST::Operator
161
161
 
162
162
  def Set.to_s(arg1, arg2, return_type)
163
- if BOAST::get_lang == C then
164
- if arg1.class == Variable and arg1.type.vector_length > 1 then
163
+ if BOAST::get_lang == BOAST::C then
164
+ if arg1.class == BOAST::Variable and arg1.type.vector_length > 1 then
165
165
  if arg1.type == arg2.type then
166
166
  return basic_usage(arg1, arg2)
167
167
  elsif arg1.type.vector_length == arg2.type.vector_length then
@@ -169,18 +169,18 @@ module BOAST
169
169
  elsif arg2.type.vector_length == 1 then
170
170
  size = arg1.type.total_size*8
171
171
  case BOAST::get_architecture
172
- when ARM
172
+ when BOAST::ARM
173
173
  intr_name = "vmov"
174
174
  intr_name += "q" if size == 128
175
175
  intr_name += "_n_#{get_vector_name(arg1.type)}"
176
- when X86
177
- return "(#{arg1} = _m_from_int64( #{a2} ))" if arg1.type.class == Int and arg1.type.size == 8 and size == 64
176
+ when BOAST::X86
177
+ return "(#{arg1} = _m_from_int64( #{a2} ))" if arg1.type.class == BOAST::Int and arg1.type.size == 8 and size == 64
178
178
  intr_name = "_mm"
179
179
  if size > 128 then
180
180
  intr_name += "#{size}"
181
181
  end
182
182
  intr_name += "_set1_#{get_vector_name(arg1.type).gsub("u","")}"
183
- intr_name += "x" if arg1.type.class == Int and arg1.type.size == 8
183
+ intr_name += "x" if arg1.type.class == BOAST::Int and arg1.type.size == 8
184
184
  else
185
185
  raise "Unsupported architecture!"
186
186
  end
@@ -201,21 +201,21 @@ module BOAST
201
201
  end
202
202
  end
203
203
 
204
- class Different < Operator
204
+ class Different < BOAST::Operator
205
205
  def Different.to_s(arg1, arg2, return_type)
206
206
  return basic_usage(arg1, arg2)
207
207
  end
208
208
 
209
209
  def Different.basic_usage(arg1, arg2)
210
- return "#{arg1} /= #{arg2}" if BOAST::get_lang == FORTRAN
210
+ return "#{arg1} /= #{arg2}" if BOAST::get_lang == BOAST::FORTRAN
211
211
  return "#{arg1} != #{arg2}"
212
212
  end
213
213
  end
214
214
 
215
- class Affectation < Operator
215
+ class Affectation < BOAST::Operator
216
216
  def Affectation.to_s(arg1, arg2, return_type)
217
- if BOAST::get_lang == C then
218
- if arg1.class == Variable and arg1.type.vector_length > 1 then
217
+ if BOAST::get_lang == BOAST::C then
218
+ if arg1.class == BOAST::Variable and arg1.type.vector_length > 1 then
219
219
  #puts "#{arg1.type.vector_length} #{arg2.type.vector_length}"
220
220
  if arg1.type == arg2.type then
221
221
  return basic_usage(arg1, arg2)
@@ -230,12 +230,12 @@ module BOAST
230
230
  a2 = a2[1..-1]
231
231
  end
232
232
  case BOAST::get_architecture
233
- when ARM
233
+ when BOAST::ARM
234
234
  intr_name = "vldl"
235
235
  intr_name += "q" if size == 128
236
236
  intr_name += "_#{get_vector_name(arg1.type)}"
237
- when X86
238
- if arg1.type.class == Int and size == 64 then
237
+ when BOAST::X86
238
+ if arg1.type.class == BOAST::Int and size == 64 then
239
239
  return "#{arg1} = _m_from_int64( *((int64_t * ) #{a2} ) )"
240
240
  end
241
241
  intr_name = "_mm"
@@ -243,7 +243,7 @@ module BOAST
243
243
  intr_name += "#{size}"
244
244
  end
245
245
  intr_name += "_load_"
246
- if arg1.type.class == Int then
246
+ if arg1.type.class == BOAST::Int then
247
247
  intr_name += "si#{size}"
248
248
  else
249
249
  intr_name += "#{get_vector_name(arg1.type)}"
@@ -255,7 +255,7 @@ module BOAST
255
255
  else
256
256
  raise "Unknown convertion between vectors of different length!"
257
257
  end
258
- elsif arg2.class == Variable and arg2.type.vector_length > 1 then
258
+ elsif arg2.class == BOAST::Variable and arg2.type.vector_length > 1 then
259
259
  size = arg2.type.total_size*8
260
260
  a1 = "#{arg1}"
261
261
  if a1[0] != "*" then
@@ -264,12 +264,12 @@ module BOAST
264
264
  a1 = a1[1..-1]
265
265
  end
266
266
  case BOAST::get_architecture
267
- when ARM
267
+ when BOAST::ARM
268
268
  intr_name = "vstl"
269
269
  intr_name += "q" if size == 128
270
270
  intr_name += "_#{get_vector_name(arg2.type)}"
271
- when X86
272
- if arg2.type.class == Int and size == 64 then
271
+ when BOAST::X86
272
+ if arg2.type.class == BOAST::Int and size == 64 then
273
273
  return " *((int64_t * ) #{a1}) = _m_to_int64( #{arg2} )"
274
274
  end
275
275
  intr_name = "_mm"
@@ -277,7 +277,7 @@ module BOAST
277
277
  intr_name += "#{size}"
278
278
  end
279
279
  intr_name += "_store_"
280
- if arg2.type.class == Int then
280
+ if arg2.type.class == BOAST::Int then
281
281
  intr_name += "si#{size}"
282
282
  else
283
283
  intr_name += "#{get_vector_name(arg2.type)}"
@@ -299,7 +299,7 @@ module BOAST
299
299
  end
300
300
  end
301
301
 
302
- class Multiplication < BasicBinaryOperator
302
+ class Multiplication < BOAST::BasicBinaryOperator
303
303
  class << self
304
304
 
305
305
  def symbol
@@ -321,7 +321,7 @@ module BOAST
321
321
  end
322
322
  end
323
323
 
324
- class Addition < BasicBinaryOperator
324
+ class Addition < BOAST::BasicBinaryOperator
325
325
  class << self
326
326
 
327
327
  def symbol
@@ -343,7 +343,7 @@ module BOAST
343
343
  end
344
344
  end
345
345
 
346
- class Substraction < BasicBinaryOperator
346
+ class Substraction < BOAST::BasicBinaryOperator
347
347
  class << self
348
348
 
349
349
  def symbol
@@ -365,7 +365,7 @@ module BOAST
365
365
  end
366
366
  end
367
367
 
368
- class Division < BasicBinaryOperator
368
+ class Division < BOAST::BasicBinaryOperator
369
369
  class << self
370
370
 
371
371
  def symbol
@@ -387,16 +387,54 @@ module BOAST
387
387
  end
388
388
  end
389
389
 
390
- class Minus < Operator
390
+ class Minus < BOAST::Operator
391
391
  def Minus.to_s(arg1, arg2, return_type)
392
392
  return " -(#{arg2})"
393
393
  end
394
394
  end
395
395
 
396
- class Not < Operator
396
+ class Not < BOAST::Operator
397
397
  def Not.to_s(arg1, arg2, return_type)
398
398
  return " ! #{arg2}"
399
399
  end
400
400
  end
401
401
 
402
+ class Ternary
403
+ include BOAST::Arithmetic
404
+
405
+ def self.parens(*args,&block)
406
+ return self::new(*args,&block)
407
+ end
408
+
409
+ attr_reader :operand1
410
+ attr_reader :operand2
411
+ attr_reader :operand3
412
+
413
+ def initialize(x,y,z)
414
+ @operand1 = x
415
+ @operand2 = y
416
+ @operand3 = z
417
+ end
418
+
419
+ def to_s
420
+ self.to_str
421
+ end
422
+
423
+ def to_str
424
+ raise "Ternary operator unsupported in FORTRAN!" if BOAST::get_lang == BOAST::FORTRAN
425
+ return self.to_str_c if [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
426
+ end
427
+ def to_str_c
428
+ s = ""
429
+ s += "(#{@operand1} ? #{@operand2} : #{@operand3})"
430
+ end
431
+ def print(final=true)
432
+ s=""
433
+ s += " "*BOAST::get_indent_level if final
434
+ s += self.to_str
435
+ s += ";" if final and [BOAST::C, BOAST::CL, BOAST::CUDA].include?( BOAST::get_lang )
436
+ BOAST::get_output.puts s if final
437
+ return s
438
+ end
439
+ end
402
440
  end
@@ -0,0 +1,41 @@
1
+ module BOAST
2
+
3
+ class Pragma
4
+ def self.parens(*args,&block)
5
+ return self::new(*args,&block)
6
+ end
7
+
8
+ attr_reader :name
9
+ attr_reader :options
10
+
11
+ def initialize(name, options)
12
+ @name = name
13
+ @options = options
14
+ end
15
+
16
+ def to_s
17
+ self.to_str
18
+ end
19
+
20
+ def to_str
21
+ s = ""
22
+ if BOAST::get_lang == FORTRAN then
23
+ s += "$!"
24
+ else
25
+ s += "#pragma"
26
+ end
27
+ @options.each{ |opt|
28
+ s += " #{opt}"
29
+ }
30
+ return s
31
+ end
32
+
33
+ def print(final = true)
34
+ s=""
35
+ s += self.to_str
36
+ BOAST::get_output.puts s if final
37
+ return s
38
+ end
39
+ end
40
+
41
+ end