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



346
347
348
# File 'lib/wardite/value.rb', line 346

def value
  @value
end

Class Method Details

.from_bytes(str) ⇒ Object



350
351
352
353
354
355
356
# File 'lib/wardite/value.rb', line 350

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:



450
451
452
# File 'lib/wardite/value.rb', line 450

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

#convert_u(to:) ⇒ Object

Raises:



456
457
458
# File 'lib/wardite/value.rb', line 456

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

#demote(to:) ⇒ Object

Raises:



462
463
464
# File 'lib/wardite/value.rb', line 462

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

#extend_s(to:) ⇒ Object

Raises:



386
387
388
# File 'lib/wardite/value.rb', line 386

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

#extend_u(to:) ⇒ Object

Raises:



392
393
394
# File 'lib/wardite/value.rb', line 392

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

#inspectObject



482
483
484
# File 'lib/wardite/value.rb', line 482

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

#memsizeObject



359
360
361
# File 'lib/wardite/value.rb', line 359

def memsize
  32
end

#packed(size: nil) ⇒ Object



374
375
376
# File 'lib/wardite/value.rb', line 374

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

#promote(to:) ⇒ Object

Raises:



468
469
470
471
# File 'lib/wardite/value.rb', line 468

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

#reinterpret(to:) ⇒ Object

Raises:



475
476
477
478
479
480
# File 'lib/wardite/value.rb', line 475

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



364
365
366
367
368
369
370
# File 'lib/wardite/value.rb', line 364

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

TODO:

need more testcase…



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/wardite/value.rb', line 400

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



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/wardite/value.rb', line 431

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:



380
381
382
# File 'lib/wardite/value.rb', line 380

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