Class: Wardite::I64

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

Constant Summary collapse

I64_MAX =

: Integer

(1<<64) - 1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Instance Attribute Details

#valueObject

: Integer



251
252
253
# File 'lib/wardite/value.rb', line 251

def value
  @value
end

Class Method Details

.from_bytes(str, size: nil, signed: false) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/wardite/value.rb', line 257

def self.from_bytes(str, size: nil, signed: false)
  v = case size
    when nil
      str.unpack("L!")[0]
    when 8
      signed ? str.unpack("c")[0] : str.unpack("C")[0]
    when 16
      signed ? str.unpack("s!")[0] : str.unpack("S!")[0]
    when 32
      signed ? str.unpack("i!")[0] : str.unpack("I!")[0]
    end
  if !v.is_a?(Integer)
    raise "broken string or unsupported size: #{str.inspect} -> #{size}"
  end
  Wardite::I64(v)
end

Instance Method Details

#==(other) ⇒ Object



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

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

#convert_s(to:) ⇒ Object



339
340
341
342
343
344
345
346
347
348
# File 'lib/wardite/value.rb', line 339

def convert_s(to:)
  case to
  when :f32
    F32(value_s.to_f)
  when :f64
    F64(value_s.to_f)
  else
    raise EvalError, "unsupported operation"
  end
end

#convert_u(to:) ⇒ Object



352
353
354
355
356
357
358
359
360
361
# File 'lib/wardite/value.rb', line 352

def convert_u(to:)
  case to
  when :f32
    F32(value.to_f)
  when :f64
    F64(value.to_f)
  else
    raise EvalError, "unsupported operation"
  end
end

#demote(to:) ⇒ Object

Raises:



365
366
367
# File 'lib/wardite/value.rb', line 365

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

#extend_s(to:) ⇒ Object

Raises:



315
316
317
# File 'lib/wardite/value.rb', line 315

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

#extend_u(to:) ⇒ Object

Raises:



321
322
323
# File 'lib/wardite/value.rb', line 321

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

#extendN_s(from:, to:) ⇒ Object

Raises:



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

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

#inspectObject

I64#inspect shows signed value



404
405
406
# File 'lib/wardite/value.rb', line 404

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

#memsizeObject



275
276
277
# File 'lib/wardite/value.rb', line 275

def memsize
  64
end

#packed(size: nil) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/wardite/value.rb', line 289

def packed(size: nil)
  case size
  when nil
    [self.value].pack("L!")
  when 8
    [self.value & 0xff].pack("C")
  when 16
    [self.value & 0xffff].pack("S!")
  when 32
    [self.value & 0xffffffff].pack("I!")
  else
    raise EvalError, "unsupported size #{size}"
  end
end

#promote(to:) ⇒ Object

Raises:



371
372
373
# File 'lib/wardite/value.rb', line 371

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

#reinterpret(to:) ⇒ Object

Raises:



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

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

#trunc_s(to:) ⇒ Object

Raises:



327
328
329
# File 'lib/wardite/value.rb', line 327

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

#trunc_sat_s(to:) ⇒ Object

Raises:



399
400
401
# File 'lib/wardite/value.rb', line 399

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

#trunc_sat_u(to:) ⇒ Object

Raises:



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

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

#trunc_u(to:) ⇒ Object

Raises:



333
334
335
# File 'lib/wardite/value.rb', line 333

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

#value_sObject

returns a value interpreted as signed integer



281
282
283
284
285
# File 'lib/wardite/value.rb', line 281

def value_s
  (@value >> 63).zero? ?
    @value :
    ((-@value) ^ I64_MAX) + 1
end

#wrap(to:) ⇒ Object



306
307
308
309
310
311
# File 'lib/wardite/value.rb', line 306

def wrap(to:)
  if to != :i32
    raise EvalError, "unsupported operation #{to}"
  end
  I32(value % (1 << 32))
end