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



235
236
237
# File 'lib/wardite/value.rb', line 235

def value
  @value
end

Class Method Details

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/wardite/value.rb', line 241

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



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

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

#convert_s(to:) ⇒ Object



323
324
325
326
327
328
329
330
331
332
# File 'lib/wardite/value.rb', line 323

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



336
337
338
339
340
341
342
343
344
345
# File 'lib/wardite/value.rb', line 336

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:



349
350
351
# File 'lib/wardite/value.rb', line 349

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

#extend_s(to:) ⇒ Object

Raises:



299
300
301
# File 'lib/wardite/value.rb', line 299

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

#extend_u(to:) ⇒ Object

Raises:



305
306
307
# File 'lib/wardite/value.rb', line 305

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

#extendN_s(from:, to:) ⇒ Object

Raises:



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

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

#inspectObject

I64#inspect shows signed value



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

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

#memsizeObject



259
260
261
# File 'lib/wardite/value.rb', line 259

def memsize
  64
end

#packed(size: nil) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/wardite/value.rb', line 273

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:



355
356
357
# File 'lib/wardite/value.rb', line 355

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

#reinterpret(to:) ⇒ Object

Raises:



361
362
363
364
365
366
# File 'lib/wardite/value.rb', line 361

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:



311
312
313
# File 'lib/wardite/value.rb', line 311

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

#trunc_sat_s(to:) ⇒ Object

Raises:



383
384
385
# File 'lib/wardite/value.rb', line 383

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

#trunc_sat_u(to:) ⇒ Object

Raises:



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

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

#trunc_u(to:) ⇒ Object

Raises:



317
318
319
# File 'lib/wardite/value.rb', line 317

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

#value_sObject

returns a value interpreted as signed integer



265
266
267
268
269
# File 'lib/wardite/value.rb', line 265

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

#wrap(to:) ⇒ Object



290
291
292
293
294
295
# File 'lib/wardite/value.rb', line 290

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