Class: RubyHDL::High::Select
- Inherits:
-
Expression
- Object
- Expression
- RubyHDL::High::Select
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes the software implementation of an select operation.
Instance Attribute Summary
Attributes inherited from Expression
Instance Method Summary collapse
-
#initialize(type, operator, sel, *choices) ⇒ Select
constructor
Create a new select operation with +type+ data type, selection operand +sel+ and possible choices +choices+.
-
#to_c ⇒ Object
Convert to C code.
-
#to_python(l = "") ⇒ Object
Convert to Python code.
-
#to_ruby ⇒ Object
Convert to Ruby code.
-
#to_tf(l = "") ⇒ Object
Convert to TensorFlow code.
Methods inherited from Expression
#<=, #[], #heach, #mux, #sdownto, #seach, #stimes, #supto, #to_expr, #to_value
Methods included from HEnumerable
#hall?, #hany?, #hchain, #hchunk, #hchunk_while, #hcompact, #hcount, #hcycle, #hdrop, #hdrop_while, #heach_cons, #heach_entry, #heach_range, #heach_slice, #heach_with_index, #heach_with_object, #hfind, #hfind_index, #hfirst, #hflat_map, #hgrep, #hgrep_v, #hgroup_by, #hinclude?, #hinject, #hlazy, #hmap, #hmax, #hmax_by, #hmin, #hmin_by, #hminmax, #hminmax_by, #hnone?, #hone?, #hpartition, #hreduce, #hreject, #hreverse_each, #hselect, #hslice_after, #hslice_before, #hslice_when, #hsort, #hsort_by, #hsum, #htake, #htake_while, #htally, #hto_a, #hto_h, #huniq, #hzip
Constructor Details
#initialize(type, operator, sel, *choices) ⇒ Select
Create a new select operation with +type+ data type, selection operand +sel+ and possible choices +choices+.
2507 2508 2509 2510 2511 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2507 def initialize(type,operator,sel,*choices) super(type) @sel = sel.to_expr @choices = choices.map(&:to_expr) end |
Instance Method Details
#to_c ⇒ Object
Convert to C code.
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2523 def to_c # return "switch(#{@sel.to_c}) {\n" + # @choices.map.with_index do |choice,i| # "case #{i}:\n#{choice.to_c}\nbreak;" # end.join("\n") + "\n}" selc = @sel.to_c return @choices.map.with_index do |choice, i| "#{selc} == #{i} ? #{choice} : " end.join + "0" end |
#to_python(l = "") ⇒ Object
Convert to Python code.
2535 2536 2537 2538 2539 2540 2541 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2535 def to_python(l = "") # return "\n#{l}match #{@sel.to_python}:\n" + # @choices.map.with_index do |choice,i| # "#{l} case #{i}:\n #{choice.to_python(l + " ")}\n" # end.join return "[" + @choices.map(&:to_python) + "][#{@sel.to_python}]" end |
#to_ruby ⇒ Object
Convert to Ruby code.
2514 2515 2516 2517 2518 2519 2520 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2514 def to_ruby # return "\ncase(#{@sel.to_ruby}) ; " + # @choices.map.with_index do |choice,i| # "when #{i} ; #{choice.to_ruby} ; " # end.join + "end\n" return "[" + @choices.map(&:to_ruby) + "][#{@sel.to_ruby}]" end |
#to_tf(l = "") ⇒ Object
Convert to TensorFlow code.
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2544 def to_tf(l = "") # return "tf.switch_case(tf.cast(#{@sel.to_tf},tf.int32)," + # "\nbranch_fns={" + # @choices.map.with_index do |choice,i| # (i < @choices.size-1) ? # "\n#{i}: lambda: #{choice.to_tf(l + " ")}," : # "\n#{i}: lambda: #{choice.to_tf(l + " ")}}," # end.join + ")" return "tf.constant([" + @choices.map(&:to_python) + "])[#{@sel.to_python}]" end |