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



490
491
492
# File 'lib/wardite/value.rb', line 490

def value
  @value
end

Class Method Details

.from_bytes(str) ⇒ Object



494
495
496
497
498
499
500
# File 'lib/wardite/value.rb', line 494

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

#convert_s(to:) ⇒ Object

Raises:



593
594
595
# File 'lib/wardite/value.rb', line 593

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

#convert_u(to:) ⇒ Object

Raises:



599
600
601
# File 'lib/wardite/value.rb', line 599

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

#demote(to:) ⇒ Object

TODO:

no loss of digits…

Raises:



606
607
608
609
# File 'lib/wardite/value.rb', line 606

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

#extend_s(to:) ⇒ Object

Raises:



530
531
532
# File 'lib/wardite/value.rb', line 530

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

#extend_u(to:) ⇒ Object

Raises:



536
537
538
# File 'lib/wardite/value.rb', line 536

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

#inspectObject



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

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

#memsizeObject



503
504
505
# File 'lib/wardite/value.rb', line 503

def memsize
  64
end

#packed(size: nil) ⇒ Object



518
519
520
# File 'lib/wardite/value.rb', line 518

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

#promote(to:) ⇒ Object

Raises:



613
614
615
# File 'lib/wardite/value.rb', line 613

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

#reinterpret(to:) ⇒ Object

Raises:



619
620
621
622
623
624
# File 'lib/wardite/value.rb', line 619

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



508
509
510
511
512
513
514
# File 'lib/wardite/value.rb', line 508

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:) ⇒ Object

See Also:

  • same as F32


543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/wardite/value.rb', line 543

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

#trunc_u(to:) ⇒ Object

See Also:

  • same as F32


574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/wardite/value.rb', line 574

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

#wrap(to:) ⇒ Object

Raises:



524
525
526
# File 'lib/wardite/value.rb', line 524

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