wardite 0.9.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +107 -2
- data/Rakefile +5 -0
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/README.md +500 -0
- data/benchmark/build_call_profile.rb +42 -0
- data/benchmark/call_cost_worker.rb +27 -0
- data/benchmark/compare.rb +93 -0
- data/benchmark/compare_call_types.rb +212 -0
- data/benchmark/compare_dispatch.rb +123 -0
- data/benchmark/compare_engines.rb +119 -0
- data/benchmark/compare_optimizations.rb +139 -0
- data/benchmark/engine_phases.rb +30 -0
- data/benchmark/figures/README.md +35 -0
- data/benchmark/figures/plot_spinel_comparison.py +137 -0
- data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
- data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
- data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
- data/benchmark/figures/spinel_new_vm_summary.json +82 -0
- data/benchmark/float_m5.wat +42 -0
- data/benchmark/i32_call_arities.wat +89 -0
- data/benchmark/i32_calls.wat +174 -0
- data/benchmark/i32_memory.wat +58 -0
- data/benchmark/i32_mix.wat +80 -0
- data/benchmark/i64_m4.wat +55 -0
- data/benchmark/investigate_calls.rb +231 -0
- data/benchmark/investigate_generated_guards.rb +218 -0
- data/benchmark/m3_call_worker.rb +38 -0
- data/benchmark/measure_m1.rb +51 -0
- data/benchmark/measure_m2.rb +55 -0
- data/benchmark/measure_m3_calls.rb +89 -0
- data/benchmark/measure_m4.rb +134 -0
- data/benchmark/measure_m5_boundary.rb +41 -0
- data/benchmark/measure_mir_cache_verify.rb +58 -0
- data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
- data/benchmark/measure_ruby_wasm_startup.rb +43 -0
- data/benchmark/measure_wasi_write.rb +75 -0
- data/benchmark/optimization_worker.rb +23 -0
- data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
- data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
- data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
- data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
- data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
- data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
- data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
- data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
- data/benchmark/results/efflorescence-m1-cruby.json +799 -0
- data/benchmark/results/efflorescence-m1-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
- data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
- data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-memory.json +408 -0
- data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
- data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
- data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
- data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
- data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
- data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
- data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
- data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
- data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
- data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
- data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
- data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
- data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
- data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
- data/benchmark/summarize_call_profile.py +54 -0
- data/benchmark/validate_m1_eh.rb +34 -0
- data/benchmark/validate_m2.rb +119 -0
- data/benchmark/validate_m3.rb +44 -0
- data/benchmark/validate_m4.rb +95 -0
- data/benchmark/validate_m5.rb +108 -0
- data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
- data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
- data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
- data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
- data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
- data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
- data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
- data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
- data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
- data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
- data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
- data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
- data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
- data/docs/efflorescence-call-arities.md +115 -0
- data/docs/efflorescence-call-investigation.md +155 -0
- data/docs/efflorescence-call-transfer.md +128 -0
- data/docs/efflorescence-call-types.md +102 -0
- data/docs/efflorescence-coverage-ja.md +351 -0
- data/docs/efflorescence-generated-c-guards.md +410 -0
- data/docs/efflorescence-inlining.md +165 -0
- data/docs/efflorescence-ja.md +155 -0
- data/docs/efflorescence-m1.md +160 -0
- data/docs/efflorescence-m2.md +233 -0
- data/docs/efflorescence-m3.md +131 -0
- data/docs/efflorescence-m4.md +88 -0
- data/docs/efflorescence-m5.md +82 -0
- data/docs/efflorescence-m6.md +95 -0
- data/docs/efflorescence-mir2-cache.md +123 -0
- data/docs/efflorescence-operand-decoding.md +105 -0
- data/docs/efflorescence-phase2-ja.md +225 -0
- data/docs/efflorescence-phase2.md +240 -0
- data/docs/efflorescence-register-initialization.md +75 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +72 -0
- data/docs/efflorescence-startup-snapshot.md +60 -0
- data/docs/efflorescence-unboxed-globals.md +61 -0
- data/docs/efflorescence.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/lib/wardite/alu_f32.generated.rb +11 -2
- data/lib/wardite/alu_f64.generated.rb +11 -2
- data/lib/wardite/alu_i32.generated.rb +54 -17
- data/lib/wardite/alu_i64.generated.rb +73 -31
- data/lib/wardite/cli/cli.rb +112 -29
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
- data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
- data/lib/wardite/efflorescence/inspection.rb +165 -0
- data/lib/wardite/efflorescence/ir.rb +1164 -0
- data/lib/wardite/efflorescence/mir_cache.rb +657 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +362 -0
- data/lib/wardite/efflorescence/snapshot.rb +229 -0
- data/lib/wardite/efflorescence/store_access.rb +366 -0
- data/lib/wardite/efflorescence/translator.rb +866 -0
- data/lib/wardite/efflorescence/vm.rb +1170 -0
- data/lib/wardite/efflorescence/wasi.rb +52 -0
- data/lib/wardite/efflorescence.rb +17 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +292 -143
- data/lib/wardite/value.rb +169 -175
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/consts.rb +1 -0
- data/lib/wardite/wasi/spinel.rb +41 -0
- data/lib/wardite/wasi.rb +483 -268
- data/lib/wardite/wasm_module.rb +6 -3
- data/lib/wardite.rb +360 -98
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +121 -21
- data/scripts/verify_ruby_wasm.rb +70 -0
- data/sig/generated/wardite/cli/cli.rbs +23 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
- data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
- data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
- data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +59 -7
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite/wasi/spinel.rbs +15 -0
- data/sig/generated/wardite/wasi.rbs +111 -4
- data/sig/generated/wardite.rbs +114 -13
- data/sig/mir_cache_stdlib.rbs +5 -0
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
- data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
- data/spinel/spin.toml +7 -0
- metadata +154 -3
data/lib/wardite/cli/cli.rb
CHANGED
|
@@ -3,6 +3,16 @@ require "optparse"
|
|
|
3
3
|
|
|
4
4
|
module Wardite
|
|
5
5
|
module Cli
|
|
6
|
+
# Keep the summary accumulator's type consistent in native builds.
|
|
7
|
+
class SpinelOptions < OptionParser
|
|
8
|
+
# @rbs return: String
|
|
9
|
+
def help
|
|
10
|
+
lines = [] #: Array[String]
|
|
11
|
+
summarize(lines)
|
|
12
|
+
"#{banner}".sub(/\n?\z/, "\n") + lines.join
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
6
16
|
class Cli
|
|
7
17
|
# @rbs @args: Array[String]
|
|
8
18
|
|
|
@@ -12,6 +22,12 @@ module Wardite
|
|
|
12
22
|
attr_reader :memsize #: Integer
|
|
13
23
|
attr_reader :wasi #: bool
|
|
14
24
|
attr_reader :yjit #: bool
|
|
25
|
+
attr_reader :new_engine #: bool
|
|
26
|
+
attr_reader :disabled_optimizations #: Array[Symbol]
|
|
27
|
+
attr_reader :mir_dump #: bool
|
|
28
|
+
attr_reader :mir_profile #: bool
|
|
29
|
+
attr_reader :mir_cache #: String?
|
|
30
|
+
attr_reader :snapshot #: String?
|
|
15
31
|
|
|
16
32
|
attr_reader :profile_file_path #: String?
|
|
17
33
|
|
|
@@ -19,10 +35,18 @@ module Wardite
|
|
|
19
35
|
# @rbs return: void
|
|
20
36
|
def initialize(args)
|
|
21
37
|
@invoke = @mapdir = nil
|
|
22
|
-
@wasi = true
|
|
38
|
+
@wasi = true
|
|
23
39
|
@yjit = false
|
|
40
|
+
@new_engine = false
|
|
41
|
+
@disabled_optimizations = []
|
|
42
|
+
@mir_dump = false
|
|
43
|
+
@mir_profile = false
|
|
44
|
+
@mir_cache = nil
|
|
45
|
+
@mir_cache_verify = :checksum
|
|
46
|
+
@snapshot = nil
|
|
24
47
|
@memsize = 1
|
|
25
|
-
|
|
48
|
+
parser = RUBY_ENGINE == "spinel" ? SpinelOptions : OptionParser
|
|
49
|
+
options = parser.new do |opts|
|
|
26
50
|
opts.version = Wardite::VERSION
|
|
27
51
|
opts.on("--invoke [fnname]", "Invoke the function") { |v|
|
|
28
52
|
@invoke = v
|
|
@@ -36,6 +60,22 @@ module Wardite
|
|
|
36
60
|
opts.on("--no-wasi", "Disable WASI feature") {|_v|
|
|
37
61
|
@wasi = false
|
|
38
62
|
}
|
|
63
|
+
opts.on("--new-engine", "Use the Efflorescence register VM") {|_v|
|
|
64
|
+
@new_engine = true
|
|
65
|
+
}
|
|
66
|
+
opts.on("--mir-disable NAME", "Disable copy, immediate, branch, superinstruction, inline, frames, or all (repeatable)") {|v|
|
|
67
|
+
@disabled_optimizations << v.to_sym
|
|
68
|
+
}
|
|
69
|
+
opts.on("--mir-dump", "Print semantic and optimized MIR") { @mir_dump = true }
|
|
70
|
+
opts.on("--mir-profile", "Instrument MIR execution (do not use for timing)") { @mir_profile = true }
|
|
71
|
+
opts.on("--mir-cache DIR", "Reuse MIR2 in a local directory") { |dir| @mir_cache = dir }
|
|
72
|
+
opts.on("--mir-cache-verify MODE", "Verify the MIR2 cache with checksum (default) or sha256") do |mode|
|
|
73
|
+
unless mode == "checksum" || mode == "sha256"
|
|
74
|
+
raise Wardite::ArgumentError, "--mir-cache-verify expects checksum or sha256"
|
|
75
|
+
end
|
|
76
|
+
@mir_cache_verify = mode.to_sym
|
|
77
|
+
end
|
|
78
|
+
opts.on("--snapshot DIR", "Resume WASI startup from a local snapshot (needs --mir-cache; fixes clocks and random bytes read before it)") { |dir| @snapshot = dir }
|
|
39
79
|
opts.on("--yjit", "Enable yjit if available; setting WARDITE_YJIT_ON=1 has the same effect") {|_v|
|
|
40
80
|
@yjit = true
|
|
41
81
|
}
|
|
@@ -45,21 +85,30 @@ module Wardite
|
|
|
45
85
|
opts.on("FILE.wasm") { }
|
|
46
86
|
end
|
|
47
87
|
options.parse!(args)
|
|
88
|
+
if !new_engine && (!disabled_optimizations.empty? || mir_dump || mir_profile || mir_cache)
|
|
89
|
+
raise "MIR options require --new-engine"
|
|
90
|
+
end
|
|
91
|
+
if snapshot && (!new_engine || !mir_cache || mir_dump || mir_profile)
|
|
92
|
+
raise "--snapshot requires --new-engine and --mir-cache, without --mir-dump or --mir-profile"
|
|
93
|
+
end
|
|
48
94
|
@file = args[0] || raise("require file argument")
|
|
49
95
|
@args = (args[1..-1] || [])
|
|
50
96
|
@args.unshift if @args[0] == '--'
|
|
51
97
|
|
|
52
|
-
if
|
|
53
|
-
|
|
54
|
-
|
|
98
|
+
if RUBY_ENGINE == "spinel"
|
|
99
|
+
warn "Warning: YJIT is not available in Spinel builds" if yjit || ENV["WARDITE_YJIT_ON"] == "1"
|
|
100
|
+
else
|
|
101
|
+
if (yjit || ENV["WARDITE_YJIT_ON"] == "1") && (defined? RubyVM::YJIT)
|
|
102
|
+
RubyVM::YJIT.enable
|
|
103
|
+
end
|
|
55
104
|
|
|
56
|
-
|
|
57
|
-
|
|
105
|
+
if (yjit || ENV["WARDITE_YJIT_ON"] == "1") && !defined?(RubyVM::YJIT)
|
|
106
|
+
warn "Warning: --yjit option is specified, but not available in this Ruby build"
|
|
107
|
+
end
|
|
58
108
|
end
|
|
59
109
|
end
|
|
60
110
|
|
|
61
111
|
# @rbs return: Array[Integer | Float]
|
|
62
|
-
# @rbs %a{pure}
|
|
63
112
|
def args
|
|
64
113
|
@args.map do |a|
|
|
65
114
|
if a.include? "."
|
|
@@ -72,8 +121,11 @@ module Wardite
|
|
|
72
121
|
|
|
73
122
|
# @rbs return: ::Wardite::Instance
|
|
74
123
|
def new_instance
|
|
75
|
-
|
|
76
|
-
|
|
124
|
+
ins = File.open(file, "rb") do |f|
|
|
125
|
+
Wardite::BinaryLoader.load_from_buffer(f, enable_wasi: wasi, engine: new_engine ? :efflorescence : :stack,
|
|
126
|
+
disabled_optimizations: disabled_optimizations, mir_profile: mir_profile,
|
|
127
|
+
mir_cache: mir_dump ? nil : mir_cache, mir_cache_verify: @mir_cache_verify)
|
|
128
|
+
end
|
|
77
129
|
if memsize > 1
|
|
78
130
|
ins.store.memories[0].grow(memsize)
|
|
79
131
|
end
|
|
@@ -82,14 +134,18 @@ module Wardite
|
|
|
82
134
|
|
|
83
135
|
# @rbs return: void
|
|
84
136
|
def run
|
|
85
|
-
if
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
137
|
+
if RUBY_ENGINE == "spinel"
|
|
138
|
+
__run
|
|
139
|
+
else
|
|
140
|
+
if profile_file_path
|
|
141
|
+
require "vernier"
|
|
142
|
+
$stderr.puts "Activated Vernier profiling"
|
|
143
|
+
Vernier.profile(out: profile_file_path) do
|
|
144
|
+
__run
|
|
145
|
+
end
|
|
146
|
+
else
|
|
89
147
|
__run
|
|
90
148
|
end
|
|
91
|
-
else
|
|
92
|
-
__run
|
|
93
149
|
end
|
|
94
150
|
end
|
|
95
151
|
|
|
@@ -113,39 +169,66 @@ module Wardite
|
|
|
113
169
|
end
|
|
114
170
|
instance = new_instance
|
|
115
171
|
ret = instance.runtime.call(invoke, args)
|
|
172
|
+
runtime = instance.runtime
|
|
173
|
+
if runtime.is_a?(Wardite::Efflorescence::VM)
|
|
174
|
+
$stderr.puts runtime.dump_mir if mir_dump
|
|
175
|
+
$stderr.puts runtime.metrics.report if mir_profile
|
|
176
|
+
end
|
|
116
177
|
$stderr.puts "return value: #{ret.inspect}"
|
|
117
178
|
end
|
|
118
179
|
|
|
119
180
|
# @rbs return: void
|
|
120
181
|
def invoke_wasi
|
|
121
182
|
instance = new_instance #: ::Wardite::Instance
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
183
|
+
wasi_env = instance.wasi
|
|
184
|
+
if wasi_env
|
|
185
|
+
wasi_env.argv = ["wardite"] + @args
|
|
186
|
+
destination = mount_dst
|
|
187
|
+
source = mount_src
|
|
188
|
+
if mapdir && destination && source
|
|
189
|
+
# TODO: support multiple mapdir
|
|
190
|
+
wasi_env.mapdir[destination] = source
|
|
191
|
+
wasi_env.set_preopened_dir(destination, source)
|
|
192
|
+
end
|
|
130
193
|
end
|
|
131
194
|
|
|
132
195
|
if defined? Bundler
|
|
133
196
|
Bundler.with_original_env do
|
|
134
|
-
instance
|
|
197
|
+
start_wasi(instance)
|
|
135
198
|
end
|
|
136
199
|
else
|
|
137
|
-
instance
|
|
200
|
+
start_wasi(instance)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# The snapshot key reads the environment, so build it where _start runs.
|
|
205
|
+
# @rbs instance: ::Wardite::Instance
|
|
206
|
+
# @rbs return: void
|
|
207
|
+
def start_wasi(instance)
|
|
208
|
+
runtime = instance.runtime
|
|
209
|
+
directory = snapshot
|
|
210
|
+
if directory && runtime.is_a?(Wardite::Efflorescence::VM)
|
|
211
|
+
cache = runtime.mir_cache
|
|
212
|
+
if cache && instance.wasi && !instance.start_section
|
|
213
|
+
require "wardite/efflorescence/snapshot" unless RUBY_ENGINE == "spinel"
|
|
214
|
+
recorder = Wardite::Efflorescence::Snapshot.new(directory, instance, cache)
|
|
215
|
+
frames = runtime.mir_cache_hit ? recorder.restore(runtime) : nil
|
|
216
|
+
if frames
|
|
217
|
+
runtime.resume(frames)
|
|
218
|
+
return
|
|
219
|
+
end
|
|
220
|
+
runtime.snapshot = recorder
|
|
221
|
+
end
|
|
138
222
|
end
|
|
223
|
+
runtime.call(:_start, [])
|
|
139
224
|
end
|
|
140
225
|
|
|
141
226
|
# @rbs return: String?
|
|
142
|
-
# @rbs %a{pure}
|
|
143
227
|
def mount_src
|
|
144
228
|
mapdir&.split(":")&.first
|
|
145
229
|
end
|
|
146
230
|
|
|
147
231
|
# @rbs return: String?
|
|
148
|
-
# @rbs %a{pure}
|
|
149
232
|
def mount_dst
|
|
150
233
|
m = mapdir&.split(":")
|
|
151
234
|
if m
|
|
@@ -154,4 +237,4 @@ module Wardite
|
|
|
154
237
|
end
|
|
155
238
|
end
|
|
156
239
|
end
|
|
157
|
-
end
|
|
240
|
+
end
|
data/lib/wardite/const.rb
CHANGED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Wardite
|
|
5
|
+
module Efflorescence
|
|
6
|
+
module FloatArithmetic
|
|
7
|
+
F32_SIGN = 0x80000000
|
|
8
|
+
F32_INFINITY = 0x7f800000
|
|
9
|
+
F64_INFINITY = 0x7ff0000000000000
|
|
10
|
+
F32_NAN = 0x7fc00000
|
|
11
|
+
F64_NAN = 0x7ff8000000000000
|
|
12
|
+
|
|
13
|
+
# @rbs opcode: Integer
|
|
14
|
+
# @rbs left: Integer
|
|
15
|
+
# @rbs right: Integer
|
|
16
|
+
# @rbs return: Integer
|
|
17
|
+
def self.calculate(opcode, left, right)
|
|
18
|
+
type = opcode < Opcode::F64_EQ ? :f32 : :f64
|
|
19
|
+
op = type == :f32 ? opcode : opcode - (Opcode::F64_EQ - Opcode::F32_EQ)
|
|
20
|
+
sign = type == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
21
|
+
magnitude = left & ~sign
|
|
22
|
+
infinity = type == :f32 ? F32_INFINITY : F64_INFINITY
|
|
23
|
+
nan = type == :f32 ? F32_NAN : F64_NAN
|
|
24
|
+
case op
|
|
25
|
+
when Opcode::F32_ABS then return magnitude
|
|
26
|
+
when Opcode::F32_NEG then return left ^ sign
|
|
27
|
+
when Opcode::F32_COPYSIGN then return magnitude | (right & sign)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
other_magnitude = right & ~sign
|
|
31
|
+
if magnitude > infinity || other_magnitude > infinity
|
|
32
|
+
return op == Opcode::F32_NE ? 1 : 0 if op <= Opcode::F32_GE
|
|
33
|
+
return nan
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
x = FloatBits.to_float(type, left)
|
|
37
|
+
if op >= Opcode::F32_CEIL && op <= Opcode::F32_NEAREST
|
|
38
|
+
# Values from 2**fraction_width onward are already integral.
|
|
39
|
+
limit = type == :f32 ? 0x4b000000 : 0x4330000000000000
|
|
40
|
+
return left if magnitude == 0 || magnitude >= limit
|
|
41
|
+
rounded = integral(op, x)
|
|
42
|
+
return left & sign if rounded == 0
|
|
43
|
+
return FloatBits.from_float(type, rounded.to_f)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if op == Opcode::F32_SQRT
|
|
47
|
+
return nan if (left & sign) != 0 && magnitude != 0
|
|
48
|
+
return FloatBits.from_float(type, sqrt(x))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
y = FloatBits.to_float(type, right)
|
|
52
|
+
case op
|
|
53
|
+
when Opcode::F32_EQ then return x == y ? 1 : 0
|
|
54
|
+
when Opcode::F32_NE then return x != y ? 1 : 0
|
|
55
|
+
when Opcode::F32_LT then return x < y ? 1 : 0
|
|
56
|
+
when Opcode::F32_GT then return x > y ? 1 : 0
|
|
57
|
+
when Opcode::F32_LE then return x <= y ? 1 : 0
|
|
58
|
+
when Opcode::F32_GE then return x >= y ? 1 : 0
|
|
59
|
+
when Opcode::F32_MIN
|
|
60
|
+
return (left | right) if magnitude == 0 && other_magnitude == 0
|
|
61
|
+
return x < y ? left : right
|
|
62
|
+
when Opcode::F32_MAX
|
|
63
|
+
return (left & right) if magnitude == 0 && other_magnitude == 0
|
|
64
|
+
return x > y ? left : right
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
result = case op
|
|
68
|
+
when Opcode::F32_ADD then x + y
|
|
69
|
+
when Opcode::F32_SUB then x - y
|
|
70
|
+
when Opcode::F32_MUL then x * y
|
|
71
|
+
when Opcode::F32_DIV then divide(x, y)
|
|
72
|
+
else raise EvalError, "invalid MIR float opcode"
|
|
73
|
+
end
|
|
74
|
+
result.nan? ? nan : FloatBits.from_float(type, result)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @rbs op: Integer
|
|
78
|
+
# @rbs value: Float
|
|
79
|
+
# @rbs return: Integer
|
|
80
|
+
def self.integral(op, value)
|
|
81
|
+
case op
|
|
82
|
+
when Opcode::F32_CEIL then value.ceil
|
|
83
|
+
when Opcode::F32_FLOOR then value.floor
|
|
84
|
+
when Opcode::F32_TRUNC then value.to_i
|
|
85
|
+
else
|
|
86
|
+
# Use the magnitude so subtraction near 0.5 is exact.
|
|
87
|
+
magnitude = value.abs
|
|
88
|
+
lower = magnitude.floor
|
|
89
|
+
fraction = magnitude - lower.to_f
|
|
90
|
+
rounded = fraction > 0.5 || (fraction == 0.5 && lower.odd?) ? lower + 1 : lower
|
|
91
|
+
value < 0.0 ? -rounded : rounded
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @rbs opcode: Integer
|
|
96
|
+
# @rbs bits: Integer
|
|
97
|
+
# @rbs return: Integer
|
|
98
|
+
def self.convert(opcode, bits)
|
|
99
|
+
signature = Opcode::FLOAT_ALU_TYPES.fetch(opcode)
|
|
100
|
+
target, source = signature.fetch(0), signature.fetch(1)
|
|
101
|
+
case opcode
|
|
102
|
+
when Opcode::F32_DEMOTE_F64, Opcode::F64_PROMOTE_F32
|
|
103
|
+
mask = source == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
104
|
+
infinity = source == :f32 ? F32_INFINITY : F64_INFINITY
|
|
105
|
+
return target == :f32 ? F32_NAN : F64_NAN if (bits & ~mask) > infinity
|
|
106
|
+
|
|
107
|
+
FloatBits.from_float(target, FloatBits.to_float(source, bits))
|
|
108
|
+
else
|
|
109
|
+
if opcode < Opcode::F32_CONVERT_I32_S
|
|
110
|
+
signed = (opcode - Opcode::I32_TRUNC_F32_S).even?
|
|
111
|
+
saturating = opcode >= Opcode::I32_TRUNC_SAT_F32_S
|
|
112
|
+
truncate(target, source, bits, signed, saturating)
|
|
113
|
+
else
|
|
114
|
+
signed = (opcode - Opcode::F32_CONVERT_I32_S).even?
|
|
115
|
+
integer = source == :i32 && signed ? I64Arithmetic.extend(bits, 32) : bits
|
|
116
|
+
integer_to_float(target, integer, signed)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Round the unsigned magnitude directly to the target significand.
|
|
122
|
+
# @rbs target: Symbol
|
|
123
|
+
# @rbs value: Integer
|
|
124
|
+
# @rbs signed: bool
|
|
125
|
+
# @rbs return: Integer
|
|
126
|
+
def self.integer_to_float(target, value, signed)
|
|
127
|
+
return 0 if value == 0
|
|
128
|
+
|
|
129
|
+
negative = signed && value < 0
|
|
130
|
+
magnitude = negative ? I64Arithmetic.subtract_i64(0, value) : value
|
|
131
|
+
high = I64Arithmetic.high(magnitude)
|
|
132
|
+
exponent = high == 0 ? 31 - I64Arithmetic.clz32(magnitude) : 63 - I64Arithmetic.clz32(high)
|
|
133
|
+
fraction_width = target == :f32 ? 23 : 52
|
|
134
|
+
bias = target == :f32 ? 127 : 1023
|
|
135
|
+
shift = exponent - fraction_width
|
|
136
|
+
if shift > 0
|
|
137
|
+
significand = I64Arithmetic.shift_right(magnitude, shift, false)
|
|
138
|
+
halfway = 1 << (shift - 1)
|
|
139
|
+
remainder = magnitude & ((halfway << 1) - 1)
|
|
140
|
+
significand += 1 if remainder > halfway || (remainder == halfway && significand.odd?)
|
|
141
|
+
if significand == (2 << fraction_width)
|
|
142
|
+
significand >>= 1
|
|
143
|
+
exponent += 1
|
|
144
|
+
end
|
|
145
|
+
else
|
|
146
|
+
significand = magnitude << -shift
|
|
147
|
+
end
|
|
148
|
+
bits = ((exponent + bias) << fraction_width) | (significand & ((1 << fraction_width) - 1))
|
|
149
|
+
sign_bit = target == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
150
|
+
negative ? bits | sign_bit : bits
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Compare the truncated magnitude in integer space, including MIN and
|
|
154
|
+
# the upper half of unsigned i64, which both have the carrier sign set.
|
|
155
|
+
# @rbs target: Symbol
|
|
156
|
+
# @rbs source: Symbol
|
|
157
|
+
# @rbs bits: Integer
|
|
158
|
+
# @rbs signed: bool
|
|
159
|
+
# @rbs saturating: bool
|
|
160
|
+
# @rbs return: Integer
|
|
161
|
+
def self.truncate(target, source, bits, signed, saturating)
|
|
162
|
+
fraction_width = source == :f32 ? 23 : 52
|
|
163
|
+
bias = source == :f32 ? 127 : 1023
|
|
164
|
+
negative = source == :f32 ? (bits & F32_SIGN) != 0 : bits < 0
|
|
165
|
+
exponent = ((bits >> fraction_width) & (2 * bias + 1)) - bias
|
|
166
|
+
fraction = bits & ((1 << fraction_width) - 1)
|
|
167
|
+
if exponent == bias + 1 && fraction != 0
|
|
168
|
+
raise EvalError, "invalid conversion to integer" unless saturating
|
|
169
|
+
return 0
|
|
170
|
+
end
|
|
171
|
+
return 0 if exponent < 0
|
|
172
|
+
|
|
173
|
+
width = target == :i32 ? 32 : 64
|
|
174
|
+
if exponent < width && (signed || !negative)
|
|
175
|
+
significand = fraction | (1 << fraction_width)
|
|
176
|
+
shift = exponent - fraction_width
|
|
177
|
+
magnitude = shift < 0 ? significand >> -shift : I64Arithmetic.shift_left(significand, shift)
|
|
178
|
+
sign_bit = width == 32 ? F32_SIGN : I64Arithmetic::MIN
|
|
179
|
+
if !signed || exponent < width - 1 || (negative && magnitude == sign_bit)
|
|
180
|
+
result = negative ? I64Arithmetic.subtract_i64(0, magnitude) : magnitude
|
|
181
|
+
return width == 32 ? I64Arithmetic.low(result) : result
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
raise Wardite::IntegerOverflow, "integer overflow" unless saturating
|
|
185
|
+
|
|
186
|
+
if negative
|
|
187
|
+
signed ? (width == 32 ? F32_SIGN : I64Arithmetic::MIN) : 0
|
|
188
|
+
elsif signed
|
|
189
|
+
width == 32 ? 0x7fffffff : 0x7fffffffffffffff
|
|
190
|
+
else
|
|
191
|
+
width == 32 ? 0xffffffff : -1
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# A normalized quotient of binary32 inputs is A/B scaled by a power of
|
|
196
|
+
# two. B < 2**24. Its distance from a binary32 midpoint, unless equal,
|
|
197
|
+
# exceeds 2**-48 in the quotient's binade scaled to [1, 2).
|
|
198
|
+
# @rbs left: Float
|
|
199
|
+
# @rbs right: Float
|
|
200
|
+
# @rbs return: Float
|
|
201
|
+
def self.divide(left, right)
|
|
202
|
+
left / right
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Squaring a binary32 midpoint gives a dyadic with at most 48 fractional
|
|
206
|
+
# bits in that binade's scale. A distinct binary32 radicand differs by at
|
|
207
|
+
# least 2**-48, so its square root differs by more than 2**-50.
|
|
208
|
+
# @rbs value: Float
|
|
209
|
+
# @rbs return: Float
|
|
210
|
+
def self.sqrt(value)
|
|
211
|
+
return value if value == 0.0
|
|
212
|
+
value < 0.0 ? Float::NAN : Math.sqrt(value)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|