Class: Wardite::I32
- Inherits:
-
Object
- Object
- Wardite::I32
- Includes:
- ValueHelper
- Defined in:
- lib/wardite/value.rb
Constant Summary collapse
- I32_MAX =
: Integer
(1<<32) - 1
Instance Attribute Summary collapse
-
#value ⇒ Object
value should be stored as unsigned Integer, even in I32/I64 when we want to access signed value, it’d be done via #value_s.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #convert_s(to:) ⇒ Object
- #convert_u(to:) ⇒ Object
- #demote(to:) ⇒ Object
- #extend_s(to:) ⇒ Object
- #extend_u(to:) ⇒ Object
- #extendN_s(from:, to:) ⇒ Object
-
#inspect ⇒ Object
I32#inspect shows signed value for convinience.
- #memsize ⇒ Object
-
#packed(size: nil) ⇒ Object
TODO: eliminate use of pack, to support mruby - in this file!.
- #promote(to:) ⇒ Object
- #reinterpret(to:) ⇒ Object
- #trunc_s(to:) ⇒ Object
- #trunc_sat_s(to:) ⇒ Object
- #trunc_sat_u(to:) ⇒ Object
- #trunc_u(to:) ⇒ Object
-
#value_s ⇒ Object
returns a value interpreted as signed integer.
- #wrap(to:) ⇒ Object
Methods included from ValueHelper
Instance Attribute Details
#value ⇒ Object
value should be stored as unsigned Integer, even in I32/I64 when we want to access signed value, it’d be done via #value_s
62 63 64 |
# File 'lib/wardite/value.rb', line 62 def value @value end |
Class Method Details
.from_bytes(str, size: nil, signed: false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wardite/value.rb', line 68 def self.from_bytes(str, size: nil, signed: false) v = case size when nil str.unpack("I!")[0] when 8 signed ? str.unpack("c")[0] : str.unpack("C")[0] when 16 signed ? str.unpack("s!")[0] : str.unpack("S!")[0] end if !v.is_a?(Integer) raise "broken string or unsupported size: #{str.inspect} -> #{size}" end Wardite::I32(v) end |
Instance Method Details
#==(other) ⇒ Object
241 242 243 |
# File 'lib/wardite/value.rb', line 241 def ==(other) return self.class == other.class && self.value == other.value end |
#convert_s(to:) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/wardite/value.rb', line 146 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
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/wardite/value.rb', line 159 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
172 173 174 175 |
# File 'lib/wardite/value.rb', line 172 def demote(to:) raise EvalError, "unsupported operation" end |
#extend_s(to:) ⇒ Object
120 121 122 123 |
# File 'lib/wardite/value.rb', line 120 def extend_s(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value_s) end |
#extend_u(to:) ⇒ Object
127 128 129 130 |
# File 'lib/wardite/value.rb', line 127 def extend_u(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value) end |
#extendN_s(from:, to:) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/wardite/value.rb', line 196 def extendN_s(from:, to:) src_value = case from when :i8 base = value & 0xff (base >> 8).zero? ? base : ((-base) ^ 0xff) + 1 when :i16 base = value & 0xffff (base >> 15).zero? ? base : ((-base) ^ 0xffff) + 1 when :i32 value_s else raise EvalError, "unsupported value size" end case to when :i32 I32(src_value) when :i64 I64(src_value) else raise EvalError, "unsupported value size" end end |
#inspect ⇒ Object
I32#inspect shows signed value for convinience
237 238 239 |
# File 'lib/wardite/value.rb', line 237 def inspect "I32(#{value_s})" end |
#memsize ⇒ Object
84 85 86 |
# File 'lib/wardite/value.rb', line 84 def memsize 32 end |
#packed(size: nil) ⇒ Object
TODO: eliminate use of pack, to support mruby - in this file!
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wardite/value.rb', line 99 def packed(size: nil) case size when nil [self.value].pack("I!") when 8 [self.value & 0xff].pack("C") when 16 [self.value & 0xffff].pack("S!") else raise EvalError, "unsupported size #{size}" end end |
#promote(to:) ⇒ Object
179 180 181 182 |
# File 'lib/wardite/value.rb', line 179 def promote(to:) raise EvalError, "unsupported operation" end |
#reinterpret(to:) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/wardite/value.rb', line 186 def reinterpret(to:) raise EvalError, "unsupported operation" if to != :f32 v = [value].pack("I!").unpack("f")[0] raise EvalError, "[BUG] String#unpack is broke, really?" if !v.is_a?(Float) F32(v) end |
#trunc_s(to:) ⇒ Object
134 135 136 |
# File 'lib/wardite/value.rb', line 134 def trunc_s(to:) raise EvalError, "unsupported operation" end |
#trunc_sat_s(to:) ⇒ Object
232 233 234 |
# File 'lib/wardite/value.rb', line 232 def trunc_sat_s(to:) raise EvalError, "unsupported operation" end |
#trunc_sat_u(to:) ⇒ Object
226 227 228 |
# File 'lib/wardite/value.rb', line 226 def trunc_sat_u(to:) raise EvalError, "unsupported operation" end |
#trunc_u(to:) ⇒ Object
140 141 142 |
# File 'lib/wardite/value.rb', line 140 def trunc_u(to:) raise EvalError, "unsupported operation" end |
#value_s ⇒ Object
returns a value interpreted as signed integer
90 91 92 93 94 |
# File 'lib/wardite/value.rb', line 90 def value_s (@value >> 31).zero? ? @value : ((-@value) ^ I32_MAX) + 1 end |
#wrap(to:) ⇒ Object
114 115 116 |
# File 'lib/wardite/value.rb', line 114 def wrap(to:) raise EvalError, "unsupported operation" end |