Class: Wardite::F64

Inherits:
Object
  • Object
show all
Includes:
ValueHelper
Defined in:
lib/wardite/value.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Instance Attribute Details

#valueObject

: Float



626
627
628
# File 'lib/wardite/value.rb', line 626

def value
  @value
end

Class Method Details

.from_bytes(str) ⇒ Object



630
631
632
633
634
635
636
# File 'lib/wardite/value.rb', line 630

def self.from_bytes(str)
  v = str.unpack("E")[0]
  if !v.is_a?(Float)
    raise "broken string or unsupported size: #{str.inspect} -> 8"
  end
  Wardite::F64(v)
end

Instance Method Details

#==(other) ⇒ Object



821
822
823
# File 'lib/wardite/value.rb', line 821

def ==(other)
  return self.class == other.class && self.value == other.value
end

#convert_s(to:) ⇒ Object

Raises:



765
766
767
# File 'lib/wardite/value.rb', line 765

def convert_s(to:)
  raise EvalError, "unsupported operation"
end

#convert_u(to:) ⇒ Object

Raises:



771
772
773
# File 'lib/wardite/value.rb', line 771

def convert_u(to:)
  raise EvalError, "unsupported operation"
end

#demote(to:) ⇒ Object

TODO:

no loss of digits…

Raises:



778
779
780
781
# File 'lib/wardite/value.rb', line 778

def demote(to:)
  raise EvalError, "unsupported operation" if to != :f32
  F32(value)
end

#extend_s(to:) ⇒ Object

Raises:



666
667
668
# File 'lib/wardite/value.rb', line 666

def extend_s(to:)
  raise EvalError, "unsupported operation"
end

#extend_u(to:) ⇒ Object

Raises:



672
673
674
# File 'lib/wardite/value.rb', line 672

def extend_u(to:)
  raise EvalError, "unsupported operation"
end

#extendN_s(from:, to:) ⇒ Object

Raises:



801
802
803
# File 'lib/wardite/value.rb', line 801

def extendN_s(from:, to:)
  raise EvalError, "unsupported operation"
end

#inspectObject



817
818
819
# File 'lib/wardite/value.rb', line 817

def inspect
  "F64(#{@value})"
end

#memsizeObject



639
640
641
# File 'lib/wardite/value.rb', line 639

def memsize
  64
end

#packed(size: nil) ⇒ Object



654
655
656
# File 'lib/wardite/value.rb', line 654

def packed(size: nil)
  [self.value].pack("E")
end

#promote(to:) ⇒ Object

Raises:



785
786
787
# File 'lib/wardite/value.rb', line 785

def promote(to:)
  raise EvalError, "unsupported operation"
end

#reinterpret(to:) ⇒ Object

Raises:



791
792
793
794
795
796
# File 'lib/wardite/value.rb', line 791

def reinterpret(to:)
  raise EvalError, "unsupported operation" if to != :i64
  v = [value].pack("d").unpack("L!")[0]
  raise EvalError, "[BUG] String#unpack is broke, really?" if !v.is_a?(Integer)
  I64(v)
end

#signObject



644
645
646
647
648
649
650
# File 'lib/wardite/value.rb', line 644

def sign
  upper = [0.0].pack("G")[0]&.ord&.<<(7)
  if !upper
    raise "[BUG] Array#pack looks broken?"
  end
  upper.zero? ? :positive : :negative
end

#trunc_s(to:, saturating: false) ⇒ Object

See Also:

  • same as F32


680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/wardite/value.rb', line 680

def trunc_s(to:, saturating: false)
  v = value.to_i
  case to
  when :i32
    if v >= 0
      i32_signed_max = I32::I32_MAX >> 1
      if saturating
        v = i32_signed_max if v > i32_signed_max
      else
        v = v & i32_signed_max
      end
      I32(v & i32_signed_max)
    else
      i32_signed_min = -(I32::I32_MAX >> 1) - 1
      if saturating
        v = i32_signed_min if v < i32_signed_min
      else
        v = v & I32::I32_MAX
        if (v >> 31).zero?
          raise EvalError, "[undefined behavior] detected overflow: #{value}"
        end
      end
      I32(v)
    end
  when :i64
    if v >= 0
      i64_signed_max = I64::I64_MAX >> 1
      if saturating
        v = i64_signed_max if v > i64_signed_max
      else
        v = v & i64_signed_max
      end
      I64(v & i64_signed_max)
    else
      i64_signed_min = -(I64::I64_MAX >> 1) - 1
      if saturating
        v = i64_signed_min if v < i64_signed_min
      else
        v = v & I64::I64_MAX
        if (v >> 63).zero?
          raise EvalError, "[undefined behavior] detected overflow: #{value}"
        end
      end
      I64(v)
    end
  else
    raise EvalError, "unsupported operation to: #{to}"
  end
end

#trunc_sat_s(to:) ⇒ Object



813
814
815
# File 'lib/wardite/value.rb', line 813

def trunc_sat_s(to:)
  trunc_s(to: to, saturating: true)
end

#trunc_sat_u(to:) ⇒ Object



807
808
809
# File 'lib/wardite/value.rb', line 807

def trunc_sat_u(to:)
  trunc_u(to: to, saturating: true)
end

#trunc_u(to:, saturating: false) ⇒ Object

See Also:

  • same as F32


734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/wardite/value.rb', line 734

def trunc_u(to:, saturating: false)
  v = value.to_i
  if v < 0
    if saturating
      v = 0
    else
      raise EvalError, "[undefined behavior] unexpected negative value"
    end
  end
  case to
  when :i32
    if saturating
      v = I32::I32_MAX if v > I32::I32_MAX
    else
      v = v & I32::I32_MAX
    end
    I32(v)
  when :i64
    if saturating
      v = I64::I64_MAX if v > I64::I64_MAX
    else
      v = v & I64::I64_MAX
    end
    I64(v)
  else
    raise EvalError, "unsupported operation to: #{to}"
  end
end

#wrap(to:) ⇒ Object

Raises:



660
661
662
# File 'lib/wardite/value.rb', line 660

def wrap(to:)
  raise EvalError, "unsupported operation"
end