Class: Wardite::F32

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



408
409
410
# File 'lib/wardite/value.rb', line 408

def value
  @value
end

Class Method Details

.from_bytes(str) ⇒ Object



412
413
414
415
416
417
418
# File 'lib/wardite/value.rb', line 412

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

Instance Method Details

#convert_s(to:) ⇒ Object

Raises:



550
551
552
# File 'lib/wardite/value.rb', line 550

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

#convert_u(to:) ⇒ Object

Raises:



556
557
558
# File 'lib/wardite/value.rb', line 556

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

#demote(to:) ⇒ Object

Raises:



562
563
564
# File 'lib/wardite/value.rb', line 562

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

#extend_s(to:) ⇒ Object

Raises:



448
449
450
# File 'lib/wardite/value.rb', line 448

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

#extend_u(to:) ⇒ Object

Raises:



454
455
456
# File 'lib/wardite/value.rb', line 454

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

#extendN_s(from:, to:) ⇒ Object

Raises:



585
586
587
# File 'lib/wardite/value.rb', line 585

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

#inspectObject



601
602
603
# File 'lib/wardite/value.rb', line 601

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

#memsizeObject



421
422
423
# File 'lib/wardite/value.rb', line 421

def memsize
  32
end

#packed(size: nil) ⇒ Object



436
437
438
# File 'lib/wardite/value.rb', line 436

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

#promote(to:) ⇒ Object

Raises:



568
569
570
571
# File 'lib/wardite/value.rb', line 568

def promote(to:)
  raise EvalError, "unsupported operation" if to != :f64
  F64(value)
end

#reinterpret(to:) ⇒ Object

Raises:



575
576
577
578
579
580
# File 'lib/wardite/value.rb', line 575

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

#signObject



426
427
428
429
430
431
432
# File 'lib/wardite/value.rb', line 426

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

TODO:

need more testcase…



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/wardite/value.rb', line 464

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



597
598
599
# File 'lib/wardite/value.rb', line 597

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

#trunc_sat_u(to:) ⇒ Object



591
592
593
# File 'lib/wardite/value.rb', line 591

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

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



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/wardite/value.rb', line 519

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:



442
443
444
# File 'lib/wardite/value.rb', line 442

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