Skip to main content

pulley_interpreter/
interp.rs

1//! Interpretation of pulley bytecode.
2
3use crate::decode::*;
4use crate::encode::Encode;
5use crate::imms::*;
6use crate::profile::{ExecutingPc, ExecutingPcRef};
7use crate::regs::*;
8use alloc::string::ToString;
9use core::fmt;
10use core::mem;
11use core::ops::ControlFlow;
12use core::ops::{Index, IndexMut};
13use core::ptr::NonNull;
14use pulley_macros::interp_disable_if_cfg;
15use wasmtime_core::alloc::TryVec;
16use wasmtime_core::error::OutOfMemory;
17use wasmtime_core::math::{WasmFloat, f32_cvt_to_int_bounds, f64_cvt_to_int_bounds};
18
19mod debug;
20#[cfg(all(not(pulley_tail_calls), not(pulley_assume_llvm_makes_tail_calls)))]
21mod match_loop;
22#[cfg(any(pulley_tail_calls, pulley_assume_llvm_makes_tail_calls))]
23mod tail_loop;
24
25const DEFAULT_STACK_SIZE: usize = 1 << 20; // 1 MiB
26
27/// A virtual machine for interpreting Pulley bytecode.
28pub struct Vm {
29    state: MachineState,
30    executing_pc: ExecutingPc,
31}
32
33impl Vm {
34    /// Create a new virtual machine with the default stack size.
35    pub fn new() -> Result<Self, OutOfMemory> {
36        Self::with_stack(DEFAULT_STACK_SIZE)
37    }
38
39    /// Create a new virtual machine with the given stack.
40    pub fn with_stack(stack_size: usize) -> Result<Self, OutOfMemory> {
41        Ok(Self {
42            state: MachineState::with_stack(stack_size)?,
43            executing_pc: ExecutingPc::default(),
44        })
45    }
46
47    /// Get a shared reference to this VM's machine state.
48    pub fn state(&self) -> &MachineState {
49        &self.state
50    }
51
52    /// Get an exclusive reference to this VM's machine state.
53    pub fn state_mut(&mut self) -> &mut MachineState {
54        &mut self.state
55    }
56
57    /// Call a bytecode function.
58    ///
59    /// The given `func` must point to the beginning of a valid Pulley bytecode
60    /// function.
61    ///
62    /// The given `args` must match the number and type of arguments that
63    /// function expects.
64    ///
65    /// The given `rets` must match the function's actual return types.
66    ///
67    /// Returns either the resulting values, or the PC at which a trap was
68    /// raised.
69    pub unsafe fn call<'a, T>(
70        &'a mut self,
71        func: NonNull<u8>,
72        args: &[Val],
73        rets: T,
74    ) -> DoneReason<impl Iterator<Item = Val> + use<'a, T>>
75    where
76        T: IntoIterator<Item = RegType> + 'a,
77    {
78        unsafe {
79            let lr = self.call_start(args);
80
81            match self.call_run(func) {
82                DoneReason::ReturnToHost(()) => DoneReason::ReturnToHost(self.call_end(lr, rets)),
83                DoneReason::Trap { pc, kind } => DoneReason::Trap { pc, kind },
84                DoneReason::CallIndirectHost { id, resume } => {
85                    DoneReason::CallIndirectHost { id, resume }
86                }
87            }
88        }
89    }
90
91    /// Performs the initial part of [`Vm::call`] in setting up the `args`
92    /// provided in registers according to Pulley's ABI.
93    ///
94    /// # Return
95    ///
96    /// Returns the old `lr` register value. The current `lr` value is replaced
97    /// with a sentinel that triggers a return to the host when returned-to.
98    ///
99    /// # Unsafety
100    ///
101    /// All the same unsafety as `call` and additionally, you must
102    /// invoke `call_run` and then `call_end` after calling `call_start`.
103    /// If you don't want to wrangle these invocations, use `call` instead
104    /// of `call_{start,run,end}`.
105    pub unsafe fn call_start<'a>(&'a mut self, args: &[Val]) -> *mut u8 {
106        // NB: make sure this method stays in sync with
107        // `PulleyMachineDeps::compute_arg_locs`!
108
109        let mut x_args = (0..15).map(|x| unsafe { XReg::new_unchecked(x) });
110        let mut f_args = (0..16).map(|f| unsafe { FReg::new_unchecked(f) });
111        #[cfg(not(pulley_disable_interp_simd))]
112        let mut v_args = (0..16).map(|v| unsafe { VReg::new_unchecked(v) });
113
114        for arg in args {
115            match arg {
116                Val::XReg(val) => match x_args.next() {
117                    Some(reg) => self.state[reg] = *val,
118                    None => todo!("stack slots"),
119                },
120                Val::FReg(val) => match f_args.next() {
121                    Some(reg) => self.state[reg] = *val,
122                    None => todo!("stack slots"),
123                },
124                #[cfg(not(pulley_disable_interp_simd))]
125                Val::VReg(val) => match v_args.next() {
126                    Some(reg) => self.state[reg] = *val,
127                    None => todo!("stack slots"),
128                },
129            }
130        }
131
132        mem::replace(&mut self.state.lr, HOST_RETURN_ADDR)
133    }
134
135    /// Performs the internal part of [`Vm::call`] where bytecode is actually
136    /// executed.
137    ///
138    /// # Unsafety
139    ///
140    /// In addition to all the invariants documented for `call`, you
141    /// may only invoke `call_run` after invoking `call_start` to
142    /// initialize this call's arguments.
143    pub unsafe fn call_run(&mut self, pc: NonNull<u8>) -> DoneReason<()> {
144        self.state.debug_assert_done_reason_none();
145        let interpreter = Interpreter {
146            state: &mut self.state,
147            pc: unsafe { UnsafeBytecodeStream::new(pc) },
148            executing_pc: self.executing_pc.as_ref(),
149        };
150        let done = interpreter.run();
151        self.state.done_decode(done)
152    }
153
154    /// Performs the tail end of [`Vm::call`] by returning the values as
155    /// determined by `rets` according to Pulley's ABI.
156    ///
157    /// The `old_ret` value should have been provided from `call_start`
158    /// previously.
159    ///
160    /// # Unsafety
161    ///
162    /// In addition to the invariants documented for `call`, this may
163    /// only be called after `call_run`.
164    pub unsafe fn call_end<'a>(
165        &'a mut self,
166        old_ret: *mut u8,
167        rets: impl IntoIterator<Item = RegType> + 'a,
168    ) -> impl Iterator<Item = Val> + 'a {
169        self.state.lr = old_ret;
170        // NB: make sure this method stays in sync with
171        // `PulleyMachineDeps::compute_arg_locs`!
172
173        let mut x_rets = (0..15).map(|x| unsafe { XReg::new_unchecked(x) });
174        let mut f_rets = (0..16).map(|f| unsafe { FReg::new_unchecked(f) });
175        #[cfg(not(pulley_disable_interp_simd))]
176        let mut v_rets = (0..16).map(|v| unsafe { VReg::new_unchecked(v) });
177
178        rets.into_iter().map(move |ty| match ty {
179            RegType::XReg => match x_rets.next() {
180                Some(reg) => Val::XReg(self.state[reg]),
181                None => todo!("stack slots"),
182            },
183            RegType::FReg => match f_rets.next() {
184                Some(reg) => Val::FReg(self.state[reg]),
185                None => todo!("stack slots"),
186            },
187            #[cfg(not(pulley_disable_interp_simd))]
188            RegType::VReg => match v_rets.next() {
189                Some(reg) => Val::VReg(self.state[reg]),
190                None => todo!("stack slots"),
191            },
192            #[cfg(pulley_disable_interp_simd)]
193            RegType::VReg => panic!("simd support disabled at compile time"),
194        })
195    }
196
197    /// Returns the current `fp` register value.
198    pub fn fp(&self) -> *mut u8 {
199        self.state.fp
200    }
201
202    /// Returns the current `lr` register value.
203    pub fn lr(&self) -> *mut u8 {
204        self.state.lr
205    }
206
207    /// Returns whether the current `lr` register value is the return address
208    /// to the host that [`Vm::call_start`] installs.
209    pub fn lr_is_host_return(&self) -> bool {
210        self.state.lr == HOST_RETURN_ADDR
211    }
212
213    /// Sets the current `fp` register value.
214    pub unsafe fn set_fp(&mut self, fp: *mut u8) {
215        self.state.fp = fp;
216    }
217
218    /// Sets the current `lr` register value.
219    pub unsafe fn set_lr(&mut self, lr: *mut u8) {
220        self.state.lr = lr;
221    }
222
223    /// Gets a handle to the currently executing program counter for this
224    /// interpreter which can be read from other threads.
225    //
226    // Note that despite this field still existing with `not(feature =
227    // "profile")` it's hidden from the public API in that scenario as it has no
228    // methods anyway.
229    #[cfg(feature = "profile")]
230    pub fn executing_pc(&self) -> &ExecutingPc {
231        &self.executing_pc
232    }
233}
234
235impl Drop for Vm {
236    fn drop(&mut self) {
237        self.executing_pc.set_done();
238    }
239}
240
241/// The type of a register in the Pulley machine state.
242#[derive(Clone, Copy, Debug)]
243pub enum RegType {
244    /// An `x` register: integers.
245    XReg,
246
247    /// An `f` register: floats.
248    FReg,
249
250    /// A `v` register: vectors.
251    VReg,
252}
253
254/// A value that can be stored in a register.
255#[derive(Clone, Copy, Debug)]
256pub enum Val {
257    /// An `x` register value: integers.
258    XReg(XRegVal),
259
260    /// An `f` register value: floats.
261    FReg(FRegVal),
262
263    /// A `v` register value: vectors.
264    #[cfg(not(pulley_disable_interp_simd))]
265    VReg(VRegVal),
266}
267
268impl fmt::LowerHex for Val {
269    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270        match self {
271            Val::XReg(v) => fmt::LowerHex::fmt(v, f),
272            Val::FReg(v) => fmt::LowerHex::fmt(v, f),
273            #[cfg(not(pulley_disable_interp_simd))]
274            Val::VReg(v) => fmt::LowerHex::fmt(v, f),
275        }
276    }
277}
278
279impl From<XRegVal> for Val {
280    fn from(value: XRegVal) -> Self {
281        Val::XReg(value)
282    }
283}
284
285impl From<u64> for Val {
286    fn from(value: u64) -> Self {
287        XRegVal::new_u64(value).into()
288    }
289}
290
291impl From<u32> for Val {
292    fn from(value: u32) -> Self {
293        XRegVal::new_u32(value).into()
294    }
295}
296
297impl From<i64> for Val {
298    fn from(value: i64) -> Self {
299        XRegVal::new_i64(value).into()
300    }
301}
302
303impl From<i32> for Val {
304    fn from(value: i32) -> Self {
305        XRegVal::new_i32(value).into()
306    }
307}
308
309impl<T> From<*mut T> for Val {
310    fn from(value: *mut T) -> Self {
311        XRegVal::new_ptr(value).into()
312    }
313}
314
315impl From<FRegVal> for Val {
316    fn from(value: FRegVal) -> Self {
317        Val::FReg(value)
318    }
319}
320
321impl From<f64> for Val {
322    fn from(value: f64) -> Self {
323        FRegVal::new_f64(value).into()
324    }
325}
326
327impl From<f32> for Val {
328    fn from(value: f32) -> Self {
329        FRegVal::new_f32(value).into()
330    }
331}
332
333#[cfg(not(pulley_disable_interp_simd))]
334impl From<VRegVal> for Val {
335    fn from(value: VRegVal) -> Self {
336        Val::VReg(value)
337    }
338}
339
340/// An `x` register value: integers.
341#[derive(Copy, Clone)]
342pub struct XRegVal(XRegUnion);
343
344impl PartialEq for XRegVal {
345    fn eq(&self, other: &Self) -> bool {
346        self.get_u64() == other.get_u64()
347    }
348}
349
350impl Eq for XRegVal {}
351
352impl fmt::Debug for XRegVal {
353    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
354        f.debug_struct("XRegVal")
355            .field("as_u64", &self.get_u64())
356            .finish()
357    }
358}
359
360impl fmt::LowerHex for XRegVal {
361    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
362        fmt::LowerHex::fmt(&self.get_u64(), f)
363    }
364}
365
366/// Contents of an "x" register, or a general-purpose register.
367///
368/// This is represented as a Rust `union` to make it easier to access typed
369/// views of this, notably the `ptr` field which enables preserving a bit of
370/// provenance for Rust for values stored as a pointer and read as a pointer.
371///
372/// Note that the actual in-memory representation of this value is handled
373/// carefully at this time. Pulley bytecode exposes the ability to store a
374/// 32-bit result into a register and then read the 64-bit contents of the
375/// register. This leaves us with the question of what to do with the upper bits
376/// of the register when the 32-bit result is generated. Possibilities for
377/// handling this are:
378///
379/// 1. Do nothing, just store the 32-bit value. The problem with this approach
380///    means that the "upper bits" are now endianness-dependent. That means that
381///    the state of the register is now platform-dependent.
382/// 2. Sign or zero-extend. This restores platform-independent behavior but
383///    requires an extra store on 32-bit platforms because they can probably
384///    only store 32-bits at a time.
385/// 3. Always store the values in this union as little-endian. This means that
386///    big-endian platforms have to do a byte-swap but otherwise it has
387///    platform-independent behavior.
388///
389/// This union chooses route (3) at this time where the values here are always
390/// stored in little-endian form (even the `ptr` field). That guarantees
391/// cross-platform behavior while also minimizing the amount of data stored on
392/// writes.
393///
394/// In the future we may wish to benchmark this and possibly change this.
395/// Technically Cranelift-generated bytecode should never rely on the upper bits
396/// of a register if it didn't previously write them so this in theory doesn't
397/// actually matter for Cranelift or wasm semantics. The only cost right now is
398/// to big-endian platforms though and it's not certain how crucial performance
399/// will be there.
400///
401/// One final note is that this notably contrasts with native CPUs where
402/// native ISAs like RISC-V specifically define the entire register on every
403/// instruction, even if only the low half contains a significant result. Pulley
404/// is unlikely to become out-of-order within the CPU itself as it's interpreted
405/// meaning that severing data-dependencies with previous operations is
406/// hypothesized to not be too important. If this is ever a problem though it
407/// could increase the likelihood we go for route (2) above instead (or maybe
408/// even (1)).
409#[derive(Copy, Clone)]
410union XRegUnion {
411    i32: i32,
412    u32: u32,
413    i64: i64,
414    u64: u64,
415
416    // Note that this is intentionally `usize` and not an actual pointer like
417    // `*mut u8`. The reason for this is that provenance is required in Rust for
418    // pointers but Cranelift has no pointer type and thus no concept of
419    // provenance. That means that at-rest it's not known whether the value has
420    // provenance or not and basically means that Pulley is required to use
421    // "permissive provenance" in Rust as opposed to strict provenance.
422    //
423    // That's more-or-less a long-winded way of saying that storage of a pointer
424    // in this value is done with `.expose_provenance()` and reading a pointer
425    // uses `with_exposed_provenance_mut(..)`.
426    ptr: usize,
427}
428
429impl Default for XRegVal {
430    fn default() -> Self {
431        Self(unsafe { mem::zeroed() })
432    }
433}
434
435#[expect(missing_docs, reason = "self-describing methods")]
436impl XRegVal {
437    pub fn new_i32(x: i32) -> Self {
438        let mut val = XRegVal::default();
439        val.set_i32(x);
440        val
441    }
442
443    pub fn new_u32(x: u32) -> Self {
444        let mut val = XRegVal::default();
445        val.set_u32(x);
446        val
447    }
448
449    pub fn new_i64(x: i64) -> Self {
450        let mut val = XRegVal::default();
451        val.set_i64(x);
452        val
453    }
454
455    pub fn new_u64(x: u64) -> Self {
456        let mut val = XRegVal::default();
457        val.set_u64(x);
458        val
459    }
460
461    pub fn new_ptr<T>(ptr: *mut T) -> Self {
462        let mut val = XRegVal::default();
463        val.set_ptr(ptr);
464        val
465    }
466
467    pub fn get_i32(&self) -> i32 {
468        let x = unsafe { self.0.i32 };
469        i32::from_le(x)
470    }
471
472    pub fn get_u32(&self) -> u32 {
473        let x = unsafe { self.0.u32 };
474        u32::from_le(x)
475    }
476
477    pub fn get_i64(&self) -> i64 {
478        let x = unsafe { self.0.i64 };
479        i64::from_le(x)
480    }
481
482    pub fn get_u64(&self) -> u64 {
483        let x = unsafe { self.0.u64 };
484        u64::from_le(x)
485    }
486
487    pub fn get_ptr<T>(&self) -> *mut T {
488        let ptr = unsafe { self.0.ptr };
489        core::ptr::with_exposed_provenance_mut(usize::from_le(ptr))
490    }
491
492    pub fn set_i32(&mut self, x: i32) {
493        self.0.i32 = x.to_le();
494    }
495
496    pub fn set_u32(&mut self, x: u32) {
497        self.0.u32 = x.to_le();
498    }
499
500    pub fn set_i64(&mut self, x: i64) {
501        self.0.i64 = x.to_le();
502    }
503
504    pub fn set_u64(&mut self, x: u64) {
505        self.0.u64 = x.to_le();
506    }
507
508    pub fn set_ptr<T>(&mut self, ptr: *mut T) {
509        self.0.ptr = ptr.expose_provenance().to_le();
510    }
511}
512
513/// An `f` register value: floats.
514#[derive(Copy, Clone)]
515pub struct FRegVal(FRegUnion);
516
517impl fmt::Debug for FRegVal {
518    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
519        f.debug_struct("FRegVal")
520            .field("as_f32", &self.get_f32())
521            .field("as_f64", &self.get_f64())
522            .finish()
523    }
524}
525
526impl fmt::LowerHex for FRegVal {
527    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
528        fmt::LowerHex::fmt(&self.get_f64().to_bits(), f)
529    }
530}
531
532// NB: like `XRegUnion` values here are always little-endian, see the
533// documentation above for more details.
534#[derive(Copy, Clone)]
535union FRegUnion {
536    f32: u32,
537    f64: u64,
538}
539
540impl Default for FRegVal {
541    fn default() -> Self {
542        Self(unsafe { mem::zeroed() })
543    }
544}
545
546#[expect(missing_docs, reason = "self-describing methods")]
547impl FRegVal {
548    pub fn new_f32(f: f32) -> Self {
549        let mut val = Self::default();
550        val.set_f32(f);
551        val
552    }
553
554    pub fn new_f64(f: f64) -> Self {
555        let mut val = Self::default();
556        val.set_f64(f);
557        val
558    }
559
560    pub fn get_f32(&self) -> f32 {
561        let val = unsafe { self.0.f32 };
562        f32::from_le_bytes(val.to_ne_bytes())
563    }
564
565    pub fn get_f64(&self) -> f64 {
566        let val = unsafe { self.0.f64 };
567        f64::from_le_bytes(val.to_ne_bytes())
568    }
569
570    pub fn set_f32(&mut self, val: f32) {
571        self.0.f32 = u32::from_ne_bytes(val.to_le_bytes());
572    }
573
574    pub fn set_f64(&mut self, val: f64) {
575        self.0.f64 = u64::from_ne_bytes(val.to_le_bytes());
576    }
577}
578
579/// A `v` register value: vectors.
580#[derive(Copy, Clone)]
581#[cfg(not(pulley_disable_interp_simd))]
582pub struct VRegVal(VRegUnion);
583
584#[cfg(not(pulley_disable_interp_simd))]
585impl fmt::Debug for VRegVal {
586    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
587        f.debug_struct("VRegVal")
588            .field("as_u128", &unsafe { self.0.u128 })
589            .finish()
590    }
591}
592
593#[cfg(not(pulley_disable_interp_simd))]
594impl fmt::LowerHex for VRegVal {
595    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
596        fmt::LowerHex::fmt(unsafe { &self.0.u128 }, f)
597    }
598}
599
600/// 128-bit vector registers.
601///
602/// This register is always stored in little-endian order and has different
603/// constraints than `XRegVal` and `FRegVal` above. Notably all fields of this
604/// union are the same width so all bits are always defined. Note that
605/// little-endian is required though so bitcasts between different shapes of
606/// vectors works. This union cannot be stored in big-endian.
607#[derive(Copy, Clone)]
608#[repr(align(16))]
609#[cfg(not(pulley_disable_interp_simd))]
610union VRegUnion {
611    u128: u128,
612    i8x16: [i8; 16],
613    i16x8: [i16; 8],
614    i32x4: [i32; 4],
615    i64x2: [i64; 2],
616    u8x16: [u8; 16],
617    u16x8: [u16; 8],
618    u32x4: [u32; 4],
619    u64x2: [u64; 2],
620    // Note that these are `u32` and `u64`, not f32/f64. That's only because
621    // f32/f64 don't have `.to_le()` and `::from_le()` so need to go through the
622    // bits anyway.
623    f32x4: [u32; 4],
624    f64x2: [u64; 2],
625}
626
627#[cfg(not(pulley_disable_interp_simd))]
628impl Default for VRegVal {
629    fn default() -> Self {
630        Self(unsafe { mem::zeroed() })
631    }
632}
633
634#[expect(missing_docs, reason = "self-describing methods")]
635#[cfg(not(pulley_disable_interp_simd))]
636impl VRegVal {
637    pub fn new_u128(i: u128) -> Self {
638        let mut val = Self::default();
639        val.set_u128(i);
640        val
641    }
642
643    pub fn get_u128(&self) -> u128 {
644        let val = unsafe { self.0.u128 };
645        u128::from_le(val)
646    }
647
648    pub fn set_u128(&mut self, val: u128) {
649        self.0.u128 = val.to_le();
650    }
651
652    fn get_i8x16(&self) -> [i8; 16] {
653        let val = unsafe { self.0.i8x16 };
654        val.map(|e| i8::from_le(e))
655    }
656
657    fn set_i8x16(&mut self, val: [i8; 16]) {
658        self.0.i8x16 = val.map(|e| e.to_le());
659    }
660
661    fn get_u8x16(&self) -> [u8; 16] {
662        let val = unsafe { self.0.u8x16 };
663        val.map(|e| u8::from_le(e))
664    }
665
666    fn set_u8x16(&mut self, val: [u8; 16]) {
667        self.0.u8x16 = val.map(|e| e.to_le());
668    }
669
670    fn get_i16x8(&self) -> [i16; 8] {
671        let val = unsafe { self.0.i16x8 };
672        val.map(|e| i16::from_le(e))
673    }
674
675    fn set_i16x8(&mut self, val: [i16; 8]) {
676        self.0.i16x8 = val.map(|e| e.to_le());
677    }
678
679    fn get_u16x8(&self) -> [u16; 8] {
680        let val = unsafe { self.0.u16x8 };
681        val.map(|e| u16::from_le(e))
682    }
683
684    fn set_u16x8(&mut self, val: [u16; 8]) {
685        self.0.u16x8 = val.map(|e| e.to_le());
686    }
687
688    fn get_i32x4(&self) -> [i32; 4] {
689        let val = unsafe { self.0.i32x4 };
690        val.map(|e| i32::from_le(e))
691    }
692
693    fn set_i32x4(&mut self, val: [i32; 4]) {
694        self.0.i32x4 = val.map(|e| e.to_le());
695    }
696
697    fn get_u32x4(&self) -> [u32; 4] {
698        let val = unsafe { self.0.u32x4 };
699        val.map(|e| u32::from_le(e))
700    }
701
702    fn set_u32x4(&mut self, val: [u32; 4]) {
703        self.0.u32x4 = val.map(|e| e.to_le());
704    }
705
706    fn get_i64x2(&self) -> [i64; 2] {
707        let val = unsafe { self.0.i64x2 };
708        val.map(|e| i64::from_le(e))
709    }
710
711    fn set_i64x2(&mut self, val: [i64; 2]) {
712        self.0.i64x2 = val.map(|e| e.to_le());
713    }
714
715    fn get_u64x2(&self) -> [u64; 2] {
716        let val = unsafe { self.0.u64x2 };
717        val.map(|e| u64::from_le(e))
718    }
719
720    fn set_u64x2(&mut self, val: [u64; 2]) {
721        self.0.u64x2 = val.map(|e| e.to_le());
722    }
723
724    fn get_f64x2(&self) -> [f64; 2] {
725        let val = unsafe { self.0.f64x2 };
726        val.map(|e| f64::from_bits(u64::from_le(e)))
727    }
728
729    fn set_f64x2(&mut self, val: [f64; 2]) {
730        self.0.f64x2 = val.map(|e| e.to_bits().to_le());
731    }
732
733    fn get_f32x4(&self) -> [f32; 4] {
734        let val = unsafe { self.0.f32x4 };
735        val.map(|e| f32::from_bits(u32::from_le(e)))
736    }
737
738    fn set_f32x4(&mut self, val: [f32; 4]) {
739        self.0.f32x4 = val.map(|e| e.to_bits().to_le());
740    }
741}
742
743/// The machine state for a Pulley virtual machine: the various registers and
744/// stack.
745pub struct MachineState {
746    x_regs: [XRegVal; XReg::RANGE.end as usize],
747    f_regs: [FRegVal; FReg::RANGE.end as usize],
748    #[cfg(not(pulley_disable_interp_simd))]
749    v_regs: [VRegVal; VReg::RANGE.end as usize],
750    fp: *mut u8,
751    lr: *mut u8,
752    stack: Stack,
753    done_reason: Option<DoneReason<()>>,
754}
755
756unsafe impl Send for MachineState {}
757unsafe impl Sync for MachineState {}
758
759/// Helper structure to store the state of the Pulley stack.
760///
761/// The Pulley stack notably needs to be a 16-byte aligned allocation on the
762/// host to ensure that addresses handed out are indeed 16-byte aligned. This is
763/// done with a custom `Vec<T>` internally where `T` has size and align of 16.
764/// This is manually done with a helper `Align16` type below.
765struct Stack {
766    storage: TryVec<Align16>,
767}
768
769/// Helper type used with `Stack` above.
770#[derive(Copy, Clone)]
771#[repr(align(16))]
772struct Align16 {
773    // Just here to give the structure a size of 16. The alignment is always 16
774    // regardless of what the host platform's alignment of u128 is.
775    _unused: u128,
776}
777
778impl Stack {
779    /// Creates a new stack which will have a byte size of at least `size`.
780    ///
781    /// The allocated stack might be slightly larger due to rounding necessary.
782    fn new(size: usize) -> Result<Stack, OutOfMemory> {
783        let mut storage = TryVec::new();
784        // Round up `size` to the nearest multiple of 16. Note that the
785        // stack is also allocated here but not initialized, and that's
786        // intentional as pulley bytecode should always initialize the stack
787        // before use.
788        storage.reserve_exact(size.checked_next_multiple_of(16).unwrap_or(usize::MAX) / 16)?;
789        Ok(Stack { storage })
790    }
791
792    /// Returns a pointer to the top of the stack (the highest address).
793    ///
794    /// Note that the returned pointer has provenance for the entire stack
795    /// allocation, however, not just the top.
796    fn top(&mut self) -> *mut u8 {
797        let len = self.len();
798        unsafe { self.base().add(len) }
799    }
800
801    /// Returns a pointer to the base of the stack (the lowest address).
802    ///
803    /// Note that the returned pointer has provenance for the entire stack
804    /// allocation, however, not just the top.
805    fn base(&mut self) -> *mut u8 {
806        self.storage.as_mut_ptr().cast::<u8>()
807    }
808
809    /// Returns the length, in bytes, of this stack allocation.
810    fn len(&self) -> usize {
811        self.storage.capacity() * mem::size_of::<Align16>()
812    }
813}
814
815impl fmt::Debug for MachineState {
816    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
817        let MachineState {
818            x_regs,
819            f_regs,
820            #[cfg(not(pulley_disable_interp_simd))]
821            v_regs,
822            stack: _,
823            done_reason: _,
824            fp: _,
825            lr: _,
826        } = self;
827
828        struct RegMap<'a, R>(&'a [R], fn(u8) -> alloc::string::String);
829
830        impl<R: fmt::Debug> fmt::Debug for RegMap<'_, R> {
831            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
832                let mut f = f.debug_map();
833                for (i, r) in self.0.iter().enumerate() {
834                    f.entry(&(self.1)(i as u8), r);
835                }
836                f.finish()
837            }
838        }
839
840        let mut f = f.debug_struct("MachineState");
841
842        f.field(
843            "x_regs",
844            &RegMap(x_regs, |i| XReg::new(i).unwrap().to_string()),
845        )
846        .field(
847            "f_regs",
848            &RegMap(f_regs, |i| FReg::new(i).unwrap().to_string()),
849        );
850        #[cfg(not(pulley_disable_interp_simd))]
851        f.field(
852            "v_regs",
853            &RegMap(v_regs, |i| VReg::new(i).unwrap().to_string()),
854        );
855        f.finish_non_exhaustive()
856    }
857}
858
859macro_rules! index_reg {
860    ($reg_ty:ty,$value_ty:ty,$field:ident) => {
861        impl Index<$reg_ty> for Vm {
862            type Output = $value_ty;
863
864            fn index(&self, reg: $reg_ty) -> &Self::Output {
865                &self.state[reg]
866            }
867        }
868
869        impl IndexMut<$reg_ty> for Vm {
870            fn index_mut(&mut self, reg: $reg_ty) -> &mut Self::Output {
871                &mut self.state[reg]
872            }
873        }
874
875        impl Index<$reg_ty> for MachineState {
876            type Output = $value_ty;
877
878            fn index(&self, reg: $reg_ty) -> &Self::Output {
879                &self.$field[reg.index()]
880            }
881        }
882
883        impl IndexMut<$reg_ty> for MachineState {
884            fn index_mut(&mut self, reg: $reg_ty) -> &mut Self::Output {
885                &mut self.$field[reg.index()]
886            }
887        }
888    };
889}
890
891index_reg!(XReg, XRegVal, x_regs);
892index_reg!(FReg, FRegVal, f_regs);
893#[cfg(not(pulley_disable_interp_simd))]
894index_reg!(VReg, VRegVal, v_regs);
895
896/// Sentinel return address that signals the end of the call stack.
897const HOST_RETURN_ADDR: *mut u8 = usize::MAX as *mut u8;
898
899impl MachineState {
900    fn with_stack(stack_size: usize) -> Result<Self, OutOfMemory> {
901        let mut state = Self {
902            x_regs: [Default::default(); XReg::RANGE.end as usize],
903            f_regs: Default::default(),
904            #[cfg(not(pulley_disable_interp_simd))]
905            v_regs: Default::default(),
906            stack: Stack::new(stack_size)?,
907            done_reason: None,
908            fp: HOST_RETURN_ADDR,
909            lr: HOST_RETURN_ADDR,
910        };
911
912        let sp = state.stack.top();
913        state[XReg::sp] = XRegVal::new_ptr(sp);
914
915        Ok(state)
916    }
917}
918
919/// Inner private module to prevent creation of the `Done` structure outside of
920/// this module.
921mod done {
922    use super::{Encode, Interpreter, MachineState};
923    use core::ops::ControlFlow;
924    use core::ptr::NonNull;
925
926    /// Zero-sized sentinel indicating that pulley execution has halted.
927    ///
928    /// The reason for halting is stored in `MachineState`.
929    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
930    pub struct Done {
931        _priv: (),
932    }
933
934    /// Reason that the pulley interpreter has ceased execution.
935    pub enum DoneReason<T> {
936        /// A trap happened at this bytecode instruction.
937        Trap {
938            /// Which instruction is raising this trap.
939            pc: NonNull<u8>,
940            /// The kind of trap being raised, if known.
941            kind: Option<TrapKind>,
942        },
943        /// The `call_indirect_host` instruction was executed.
944        CallIndirectHost {
945            /// The payload of `call_indirect_host`.
946            id: u8,
947            /// Where to resume execution after the host has finished.
948            resume: NonNull<u8>,
949        },
950        /// Pulley has finished and the provided value is being returned.
951        ReturnToHost(T),
952    }
953
954    /// Stored within `DoneReason::Trap`.
955    #[expect(missing_docs, reason = "self-describing variants")]
956    pub enum TrapKind {
957        DivideByZero,
958        IntegerOverflow,
959        BadConversionToInteger,
960        MemoryOutOfBounds,
961        DisabledOpcode,
962        StackOverflow,
963    }
964
965    impl MachineState {
966        pub(super) fn debug_assert_done_reason_none(&mut self) {
967            debug_assert!(self.done_reason.is_none());
968        }
969
970        pub(super) fn done_decode(&mut self, Done { _priv }: Done) -> DoneReason<()> {
971            self.done_reason.take().unwrap()
972        }
973    }
974
975    impl Interpreter<'_> {
976        /// Finishes execution by recording `DoneReason::Trap`.
977        ///
978        /// This method takes an `I` generic parameter indicating which
979        /// instruction is executing this function and generating a trap. That's
980        /// used to go backwards from the current `pc` which is just beyond the
981        /// instruction to point to the instruction itself in the trap metadata
982        /// returned from the interpreter.
983        #[cold]
984        pub fn done_trap<I: Encode>(&mut self) -> ControlFlow<Done> {
985            self.done_trap_kind::<I>(None)
986        }
987
988        /// Same as `done_trap` but with an explicit `TrapKind`.
989        #[cold]
990        pub fn done_trap_kind<I: Encode>(&mut self, kind: Option<TrapKind>) -> ControlFlow<Done> {
991            let pc = self.current_pc::<I>();
992            self.state.done_reason = Some(DoneReason::Trap { pc, kind });
993            ControlFlow::Break(Done { _priv: () })
994        }
995
996        /// Finishes execution by recording `DoneReason::CallIndirectHost`.
997        #[cold]
998        pub fn done_call_indirect_host(&mut self, id: u8) -> ControlFlow<Done> {
999            self.state.done_reason = Some(DoneReason::CallIndirectHost {
1000                id,
1001                resume: self.pc.as_ptr(),
1002            });
1003            ControlFlow::Break(Done { _priv: () })
1004        }
1005
1006        /// Finishes execution by recording `DoneReason::ReturnToHost`.
1007        #[cold]
1008        pub fn done_return_to_host(&mut self) -> ControlFlow<Done> {
1009            self.state.done_reason = Some(DoneReason::ReturnToHost(()));
1010            ControlFlow::Break(Done { _priv: () })
1011        }
1012    }
1013}
1014
1015use done::Done;
1016pub use done::{DoneReason, TrapKind};
1017
1018struct Interpreter<'a> {
1019    state: &'a mut MachineState,
1020    pc: UnsafeBytecodeStream,
1021    executing_pc: ExecutingPcRef<'a>,
1022}
1023
1024impl Interpreter<'_> {
1025    /// Calculates the `offset` for the current instruction `I`.
1026    #[inline]
1027    fn pc_rel<I: Encode>(&mut self, offset: PcRelOffset) -> NonNull<u8> {
1028        let offset = isize::try_from(i32::from(offset)).unwrap();
1029        unsafe { self.current_pc::<I>().offset(offset) }
1030    }
1031
1032    /// Performs a relative jump of `offset` bytes from the current instruction.
1033    ///
1034    /// This will jump from the start of the current instruction, identified by
1035    /// `I`, `offset` bytes away. Note that the `self.pc` at the start of this
1036    /// function actually points to the instruction after this one so `I` is
1037    /// necessary to go back to ourselves after which we then go `offset` away.
1038    #[inline]
1039    fn pc_rel_jump<I: Encode>(&mut self, offset: PcRelOffset) -> ControlFlow<Done> {
1040        let new_pc = self.pc_rel::<I>(offset);
1041        self.pc = unsafe { UnsafeBytecodeStream::new(new_pc) };
1042        ControlFlow::Continue(())
1043    }
1044
1045    /// Returns the PC of the current instruction where `I` is the static type
1046    /// representing the current instruction.
1047    fn current_pc<I: Encode>(&self) -> NonNull<u8> {
1048        unsafe { self.pc.offset(-isize::from(I::WIDTH)).as_ptr() }
1049    }
1050
1051    /// `sp -= size_of::<T>(); *sp = val;`
1052    ///
1053    /// Note that `I` is the instruction which is pushing data to use if a trap
1054    /// is generated.
1055    #[must_use]
1056    fn push<I: Encode, T>(&mut self, val: T) -> ControlFlow<Done> {
1057        let new_sp = self.state[XReg::sp].get_ptr::<T>().wrapping_sub(1);
1058        self.set_sp::<I>(new_sp.cast())?;
1059        unsafe {
1060            new_sp.write_unaligned(val);
1061        }
1062        ControlFlow::Continue(())
1063    }
1064
1065    /// `ret = *sp; sp -= size_of::<T>()`
1066    fn pop<T>(&mut self) -> T {
1067        let sp = self.state[XReg::sp].get_ptr::<T>();
1068        let val = unsafe { sp.read_unaligned() };
1069        self.set_sp_unchecked(sp.wrapping_add(1));
1070        val
1071    }
1072
1073    /// Sets the stack pointer to the `sp` provided.
1074    ///
1075    /// Returns a trap if this would result in stack overflow, or if `sp` is
1076    /// beneath the base pointer of `self.state.stack`.
1077    ///
1078    /// The `I` parameter here is the instruction that is setting the stack
1079    /// pointer and is used to calculate this instruction's own `pc` if this
1080    /// instruction traps.
1081    #[must_use]
1082    fn set_sp<I: Encode>(&mut self, sp: *mut u8) -> ControlFlow<Done> {
1083        let sp_raw = sp as usize;
1084        let base_raw = self.state.stack.base() as usize;
1085        if sp_raw < base_raw {
1086            return self.done_trap_kind::<I>(Some(TrapKind::StackOverflow));
1087        }
1088        self.set_sp_unchecked(sp);
1089        ControlFlow::Continue(())
1090    }
1091
1092    /// Shared implementation of `push_frame_save` and `push_frame_full`.
1093    ///
1094    /// This allocates `pre` bytes of stack, pushes `lr` and `fp`, sets `fp`
1095    /// to the new frame, allocates `amt` more bytes of stack, and then saves
1096    /// `regs` at the top of the `amt` bytes just allocated.
1097    #[inline]
1098    fn push_frame_full_impl<I: Encode>(
1099        &mut self,
1100        pre: usize,
1101        amt: usize,
1102        regs: UpperRegSet<XReg>,
1103    ) -> ControlFlow<Done> {
1104        let ptr_size = size_of::<usize>();
1105        let full_amt = pre + 2 * ptr_size + amt;
1106        let new_sp = self.state[XReg::sp].get_ptr::<u8>().wrapping_sub(full_amt);
1107        self.set_sp::<I>(new_sp)?;
1108
1109        let mut offset = i32::try_from(amt).unwrap();
1110        let ptr_size = ptr_size as i32;
1111
1112        // SAFETY: all stores below are within the `full_amt` bytes of stack
1113        // just allocated above.
1114        unsafe {
1115            // Emulate `push_frame` by placing `lr` and `fp` onto the stack, in
1116            // that order, just below the `pre` area.
1117            self.store_ne::<_, I>(
1118                AddrO32 {
1119                    addr: XReg::sp,
1120                    offset: offset + ptr_size,
1121                },
1122                self.state.lr,
1123            )?;
1124            self.store_ne::<_, I>(
1125                AddrO32 {
1126                    addr: XReg::sp,
1127                    offset,
1128                },
1129                self.state.fp,
1130            )?;
1131
1132            // Set `fp` to the top of our frame, where `fp` is stored.
1133            self.state.fp = new_sp.add(amt);
1134
1135            // Next save any registers in `regs` to the stack, just below `fp`.
1136            for reg in regs {
1137                offset -= 8;
1138                self.store_ne::<_, I>(
1139                    AddrO32 {
1140                        addr: XReg::sp,
1141                        offset,
1142                    },
1143                    self.state[reg].get_u64(),
1144                )?;
1145            }
1146        }
1147        ControlFlow::Continue(())
1148    }
1149
1150    /// Same as `set_sp` but does not check to see if `sp` is in-bounds. Should
1151    /// only be used with stack increment operations such as `pop`.
1152    fn set_sp_unchecked<T>(&mut self, sp: *mut T) {
1153        if cfg!(debug_assertions) {
1154            let sp_raw = sp as usize;
1155            let base = self.state.stack.base() as usize;
1156            let end = base + self.state.stack.len();
1157            assert!(base <= sp_raw && sp_raw <= end);
1158        }
1159        self.state[XReg::sp].set_ptr(sp);
1160    }
1161
1162    /// Loads a value of `T` using native-endian byte ordering from the `addr`
1163    /// specified.
1164    ///
1165    /// The `I` type parameter is the instruction issuing this load which is
1166    /// used in case of traps to calculate the trapping pc.
1167    ///
1168    /// Returns `ControlFlow::Break` if a trap happens or
1169    /// `ControlFlow::Continue` if the value was loaded successfully.
1170    ///
1171    /// # Unsafety
1172    ///
1173    /// Safety of this method relies on the safety of the original bytecode
1174    /// itself and correctly annotating both `T` and `I`.
1175    #[must_use]
1176    unsafe fn load_ne<T, I: Encode>(&mut self, addr: impl AddressingMode) -> ControlFlow<Done, T> {
1177        unsafe { addr.load_ne::<T, I>(self) }
1178    }
1179
1180    /// Stores a `val` to the `addr` specified.
1181    ///
1182    /// The `I` type parameter is the instruction issuing this store which is
1183    /// used in case of traps to calculate the trapping pc.
1184    ///
1185    /// Returns `ControlFlow::Break` if a trap happens or
1186    /// `ControlFlow::Continue` if the value was stored successfully.
1187    ///
1188    /// # Unsafety
1189    ///
1190    /// Safety of this method relies on the safety of the original bytecode
1191    /// itself and correctly annotating both `T` and `I`.
1192    #[must_use]
1193    unsafe fn store_ne<T, I: Encode>(
1194        &mut self,
1195        addr: impl AddressingMode,
1196        val: T,
1197    ) -> ControlFlow<Done> {
1198        unsafe { addr.store_ne::<T, I>(self, val) }
1199    }
1200
1201    fn check_xnn_from_f32<I: Encode>(
1202        &mut self,
1203        val: f32,
1204        (lo, hi): (f32, f32),
1205    ) -> ControlFlow<Done> {
1206        self.check_xnn_from_f64::<I>(val.into(), (lo.into(), hi.into()))
1207    }
1208
1209    fn check_xnn_from_f64<I: Encode>(
1210        &mut self,
1211        val: f64,
1212        (lo, hi): (f64, f64),
1213    ) -> ControlFlow<Done> {
1214        if val != val {
1215            return self.done_trap_kind::<I>(Some(TrapKind::BadConversionToInteger));
1216        }
1217        let val = val.wasm_trunc();
1218        if val <= lo || val >= hi {
1219            return self.done_trap_kind::<I>(Some(TrapKind::IntegerOverflow));
1220        }
1221        ControlFlow::Continue(())
1222    }
1223
1224    #[cfg(not(pulley_disable_interp_simd))]
1225    fn get_i128(&self, lo: XReg, hi: XReg) -> i128 {
1226        let lo = self.state[lo].get_u64();
1227        let hi = self.state[hi].get_i64();
1228        i128::from(lo) | (i128::from(hi) << 64)
1229    }
1230
1231    #[cfg(not(pulley_disable_interp_simd))]
1232    fn set_i128(&mut self, lo: XReg, hi: XReg, val: i128) {
1233        self.state[lo].set_u64(val as u64);
1234        self.state[hi].set_u64((val >> 64) as u64);
1235    }
1236
1237    fn record_executing_pc_for_profiling(&mut self) {
1238        // Note that this is a no-op if `feature = "profile"` is disabled.
1239        self.executing_pc.record(self.pc.as_ptr().as_ptr() as usize);
1240    }
1241}
1242
1243/// Helper trait to encompass the various addressing modes of Pulley.
1244trait AddressingMode: Sized {
1245    /// Calculates the native host address `*mut T` corresponding to this
1246    /// addressing mode.
1247    ///
1248    /// # Safety
1249    ///
1250    /// Relies on the original bytecode being safe to execute as this will
1251    /// otherwise perform unsafe byte offsets for example which requires the
1252    /// original bytecode to be correct.
1253    #[must_use]
1254    unsafe fn addr<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, *mut T>;
1255
1256    /// Loads a value of `T` from this address, using native-endian byte order.
1257    ///
1258    /// For more information see [`Interpreter::load_ne`].
1259    #[must_use]
1260    unsafe fn load_ne<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, T> {
1261        let ret = unsafe { self.addr::<T, I>(i)?.read_unaligned() };
1262        ControlFlow::Continue(ret)
1263    }
1264
1265    /// Stores a `val` to this address, using native-endian byte order.
1266    ///
1267    /// For more information see [`Interpreter::store_ne`].
1268    #[must_use]
1269    unsafe fn store_ne<T, I: Encode>(self, i: &mut Interpreter<'_>, val: T) -> ControlFlow<Done> {
1270        unsafe {
1271            self.addr::<T, I>(i)?.write_unaligned(val);
1272        }
1273        ControlFlow::Continue(())
1274    }
1275}
1276
1277impl AddressingMode for AddrO32 {
1278    unsafe fn addr<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, *mut T> {
1279        // Note that this addressing mode cannot return `ControlFlow::Break`
1280        // which is intentional. It's expected that LLVM optimizes away any
1281        // branches callers have.
1282        unsafe {
1283            ControlFlow::Continue(
1284                i.state[self.addr]
1285                    .get_ptr::<T>()
1286                    .byte_offset(self.offset as isize),
1287            )
1288        }
1289    }
1290}
1291
1292impl AddressingMode for AddrZ {
1293    unsafe fn addr<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, *mut T> {
1294        // This addressing mode defines loading/storing to the null address as
1295        // a trap, but all other addresses are allowed.
1296        let host_addr = i.state[self.addr].get_ptr::<T>();
1297        if host_addr.is_null() {
1298            i.done_trap_kind::<I>(None)?;
1299            unreachable!();
1300        }
1301        unsafe {
1302            let addr = host_addr.byte_offset(self.offset as isize);
1303            ControlFlow::Continue(addr)
1304        }
1305    }
1306}
1307
1308impl AddressingMode for AddrG32 {
1309    unsafe fn addr<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, *mut T> {
1310        // Test if `bound - offset - T` is less than the wasm address to
1311        // generate a trap. It's a guarantee of this instruction that these
1312        // subtractions don't overflow.
1313        let bound = i.state[self.host_heap_bound].get_u64() as usize;
1314        let offset = usize::from(self.offset);
1315        let wasm_addr = i.state[self.wasm_addr].get_u32() as usize;
1316        if wasm_addr > bound - offset - size_of::<T>() {
1317            i.done_trap_kind::<I>(Some(TrapKind::MemoryOutOfBounds))?;
1318            unreachable!();
1319        }
1320        unsafe {
1321            let addr = i.state[self.host_heap_base]
1322                .get_ptr::<T>()
1323                .byte_add(wasm_addr)
1324                .byte_add(offset);
1325            ControlFlow::Continue(addr)
1326        }
1327    }
1328}
1329
1330impl AddressingMode for AddrG32Bne {
1331    unsafe fn addr<T, I: Encode>(self, i: &mut Interpreter<'_>) -> ControlFlow<Done, *mut T> {
1332        // Same as `AddrG32` above except that the bound is loaded from memory.
1333        let bound = unsafe {
1334            *i.state[self.host_heap_bound_addr]
1335                .get_ptr::<usize>()
1336                .byte_add(usize::from(self.host_heap_bound_offset))
1337        };
1338        let wasm_addr = i.state[self.wasm_addr].get_u32() as usize;
1339        let offset = usize::from(self.offset);
1340        if wasm_addr > bound - offset - size_of::<T>() {
1341            i.done_trap_kind::<I>(Some(TrapKind::MemoryOutOfBounds))?;
1342            unreachable!();
1343        }
1344        unsafe {
1345            let addr = i.state[self.host_heap_base]
1346                .get_ptr::<T>()
1347                .byte_add(wasm_addr)
1348                .byte_add(offset);
1349            ControlFlow::Continue(addr)
1350        }
1351    }
1352}
1353
1354#[test]
1355fn simple_push_pop() {
1356    let mut state = MachineState::with_stack(16).unwrap();
1357    let pc = ExecutingPc::default();
1358    unsafe {
1359        let mut bytecode = [0; 10];
1360        let mut i = Interpreter {
1361            state: &mut state,
1362            // this isn't actually read so just manufacture a dummy one
1363            pc: UnsafeBytecodeStream::new(NonNull::new(bytecode.as_mut_ptr().offset(4)).unwrap()),
1364            executing_pc: pc.as_ref(),
1365        };
1366        assert!(i.push::<crate::Ret, _>(0_i32).is_continue());
1367        assert_eq!(i.pop::<i32>(), 0_i32);
1368        assert!(i.push::<crate::Ret, _>(1_i32).is_continue());
1369        assert!(i.push::<crate::Ret, _>(2_i32).is_continue());
1370        assert!(i.push::<crate::Ret, _>(3_i32).is_continue());
1371        assert!(i.push::<crate::Ret, _>(4_i32).is_continue());
1372        assert!(i.push::<crate::Ret, _>(5_i32).is_break());
1373        assert!(i.push::<crate::Ret, _>(6_i32).is_break());
1374        assert_eq!(i.pop::<i32>(), 4_i32);
1375        assert_eq!(i.pop::<i32>(), 3_i32);
1376        assert_eq!(i.pop::<i32>(), 2_i32);
1377        assert_eq!(i.pop::<i32>(), 1_i32);
1378    }
1379}
1380
1381macro_rules! br_if_imm {
1382    ($(
1383        fn $snake:ident(&mut self, a: XReg, b: $imm:ident, offset: PcRelOffset)
1384            = $camel:ident / $op:tt / $get:ident;
1385    )*) => {$(
1386        fn $snake(&mut self, a: XReg, b: $imm, offset: PcRelOffset) -> ControlFlow<Done> {
1387            let a = self.state[a].$get();
1388            if a $op b.into() {
1389                self.pc_rel_jump::<crate::$camel>(offset)
1390            } else {
1391                ControlFlow::Continue(())
1392            }
1393        }
1394    )*};
1395}
1396
1397impl OpVisitor for Interpreter<'_> {
1398    type BytecodeStream = UnsafeBytecodeStream;
1399    type Return = ControlFlow<Done>;
1400
1401    fn bytecode(&mut self) -> &mut UnsafeBytecodeStream {
1402        &mut self.pc
1403    }
1404
1405    fn nop(&mut self) -> ControlFlow<Done> {
1406        ControlFlow::Continue(())
1407    }
1408
1409    fn ret(&mut self) -> ControlFlow<Done> {
1410        let lr = self.state.lr;
1411        if lr == HOST_RETURN_ADDR {
1412            self.done_return_to_host()
1413        } else {
1414            self.pc = unsafe { UnsafeBytecodeStream::new(NonNull::new_unchecked(lr)) };
1415            ControlFlow::Continue(())
1416        }
1417    }
1418
1419    fn call(&mut self, offset: PcRelOffset) -> ControlFlow<Done> {
1420        let return_addr = self.pc.as_ptr();
1421        self.state.lr = return_addr.as_ptr();
1422        self.pc_rel_jump::<crate::Call>(offset)
1423    }
1424
1425    fn call1(&mut self, arg1: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1426        let return_addr = self.pc.as_ptr();
1427        self.state.lr = return_addr.as_ptr();
1428        self.state[XReg::x0] = self.state[arg1];
1429        self.pc_rel_jump::<crate::Call1>(offset)
1430    }
1431
1432    fn call2(&mut self, arg1: XReg, arg2: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1433        let return_addr = self.pc.as_ptr();
1434        self.state.lr = return_addr.as_ptr();
1435        let (x0, x1) = (self.state[arg1], self.state[arg2]);
1436        self.state[XReg::x0] = x0;
1437        self.state[XReg::x1] = x1;
1438        self.pc_rel_jump::<crate::Call2>(offset)
1439    }
1440
1441    fn call3(
1442        &mut self,
1443        arg1: XReg,
1444        arg2: XReg,
1445        arg3: XReg,
1446        offset: PcRelOffset,
1447    ) -> ControlFlow<Done> {
1448        let return_addr = self.pc.as_ptr();
1449        self.state.lr = return_addr.as_ptr();
1450        let (x0, x1, x2) = (self.state[arg1], self.state[arg2], self.state[arg3]);
1451        self.state[XReg::x0] = x0;
1452        self.state[XReg::x1] = x1;
1453        self.state[XReg::x2] = x2;
1454        self.pc_rel_jump::<crate::Call3>(offset)
1455    }
1456
1457    fn call4(
1458        &mut self,
1459        arg1: XReg,
1460        arg2: XReg,
1461        arg3: XReg,
1462        arg4: XReg,
1463        offset: PcRelOffset,
1464    ) -> ControlFlow<Done> {
1465        let return_addr = self.pc.as_ptr();
1466        self.state.lr = return_addr.as_ptr();
1467        let (x0, x1, x2, x3) = (
1468            self.state[arg1],
1469            self.state[arg2],
1470            self.state[arg3],
1471            self.state[arg4],
1472        );
1473        self.state[XReg::x0] = x0;
1474        self.state[XReg::x1] = x1;
1475        self.state[XReg::x2] = x2;
1476        self.state[XReg::x3] = x3;
1477        self.pc_rel_jump::<crate::Call4>(offset)
1478    }
1479
1480    fn call_indirect(&mut self, dst: XReg) -> ControlFlow<Done> {
1481        let return_addr = self.pc.as_ptr();
1482        self.state.lr = return_addr.as_ptr();
1483        // SAFETY: part of the unsafe contract of the interpreter is only valid
1484        // bytecode is interpreted, so the jump destination is part of the validity
1485        // of the bytecode itself.
1486        unsafe {
1487            self.pc = UnsafeBytecodeStream::new(NonNull::new_unchecked(self.state[dst].get_ptr()));
1488        }
1489        ControlFlow::Continue(())
1490    }
1491
1492    fn jump(&mut self, offset: PcRelOffset) -> ControlFlow<Done> {
1493        self.pc_rel_jump::<crate::Jump>(offset)
1494    }
1495
1496    fn xjump(&mut self, reg: XReg) -> ControlFlow<Done> {
1497        unsafe {
1498            self.pc = UnsafeBytecodeStream::new(NonNull::new_unchecked(self.state[reg].get_ptr()));
1499        }
1500        ControlFlow::Continue(())
1501    }
1502
1503    fn br_if32(&mut self, cond: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1504        let cond = self.state[cond].get_u32();
1505        if cond != 0 {
1506            self.pc_rel_jump::<crate::BrIf>(offset)
1507        } else {
1508            ControlFlow::Continue(())
1509        }
1510    }
1511
1512    fn br_if_not32(&mut self, cond: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1513        let cond = self.state[cond].get_u32();
1514        if cond == 0 {
1515            self.pc_rel_jump::<crate::BrIfNot>(offset)
1516        } else {
1517            ControlFlow::Continue(())
1518        }
1519    }
1520
1521    fn br_if_xeq32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1522        let a = self.state[a].get_u32();
1523        let b = self.state[b].get_u32();
1524        if a == b {
1525            self.pc_rel_jump::<crate::BrIfXeq32>(offset)
1526        } else {
1527            ControlFlow::Continue(())
1528        }
1529    }
1530
1531    fn br_if_xneq32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1532        let a = self.state[a].get_u32();
1533        let b = self.state[b].get_u32();
1534        if a != b {
1535            self.pc_rel_jump::<crate::BrIfXneq32>(offset)
1536        } else {
1537            ControlFlow::Continue(())
1538        }
1539    }
1540
1541    fn br_if_xslt32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1542        let a = self.state[a].get_i32();
1543        let b = self.state[b].get_i32();
1544        if a < b {
1545            self.pc_rel_jump::<crate::BrIfXslt32>(offset)
1546        } else {
1547            ControlFlow::Continue(())
1548        }
1549    }
1550
1551    fn br_if_xslteq32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1552        let a = self.state[a].get_i32();
1553        let b = self.state[b].get_i32();
1554        if a <= b {
1555            self.pc_rel_jump::<crate::BrIfXslteq32>(offset)
1556        } else {
1557            ControlFlow::Continue(())
1558        }
1559    }
1560
1561    fn br_if_xult32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1562        let a = self.state[a].get_u32();
1563        let b = self.state[b].get_u32();
1564        if a < b {
1565            self.pc_rel_jump::<crate::BrIfXult32>(offset)
1566        } else {
1567            ControlFlow::Continue(())
1568        }
1569    }
1570
1571    fn br_if_xulteq32(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1572        let a = self.state[a].get_u32();
1573        let b = self.state[b].get_u32();
1574        if a <= b {
1575            self.pc_rel_jump::<crate::BrIfXulteq32>(offset)
1576        } else {
1577            ControlFlow::Continue(())
1578        }
1579    }
1580
1581    fn br_if_xeq64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1582        let a = self.state[a].get_u64();
1583        let b = self.state[b].get_u64();
1584        if a == b {
1585            self.pc_rel_jump::<crate::BrIfXeq64>(offset)
1586        } else {
1587            ControlFlow::Continue(())
1588        }
1589    }
1590
1591    fn br_if_xneq64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1592        let a = self.state[a].get_u64();
1593        let b = self.state[b].get_u64();
1594        if a != b {
1595            self.pc_rel_jump::<crate::BrIfXneq64>(offset)
1596        } else {
1597            ControlFlow::Continue(())
1598        }
1599    }
1600
1601    fn br_if_xslt64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1602        let a = self.state[a].get_i64();
1603        let b = self.state[b].get_i64();
1604        if a < b {
1605            self.pc_rel_jump::<crate::BrIfXslt64>(offset)
1606        } else {
1607            ControlFlow::Continue(())
1608        }
1609    }
1610
1611    fn br_if_xslteq64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1612        let a = self.state[a].get_i64();
1613        let b = self.state[b].get_i64();
1614        if a <= b {
1615            self.pc_rel_jump::<crate::BrIfXslteq64>(offset)
1616        } else {
1617            ControlFlow::Continue(())
1618        }
1619    }
1620
1621    fn br_if_xult64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1622        let a = self.state[a].get_u64();
1623        let b = self.state[b].get_u64();
1624        if a < b {
1625            self.pc_rel_jump::<crate::BrIfXult64>(offset)
1626        } else {
1627            ControlFlow::Continue(())
1628        }
1629    }
1630
1631    fn br_if_xulteq64(&mut self, a: XReg, b: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
1632        let a = self.state[a].get_u64();
1633        let b = self.state[b].get_u64();
1634        if a <= b {
1635            self.pc_rel_jump::<crate::BrIfXulteq64>(offset)
1636        } else {
1637            ControlFlow::Continue(())
1638        }
1639    }
1640
1641    br_if_imm! {
1642        fn br_if_xeq32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1643            = BrIfXeq32I8 / == / get_i32;
1644        fn br_if_xeq32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1645            = BrIfXeq32I32 / == / get_i32;
1646        fn br_if_xneq32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1647            = BrIfXneq32I8 / != / get_i32;
1648        fn br_if_xneq32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1649            = BrIfXneq32I32 / != / get_i32;
1650
1651        fn br_if_xslt32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1652            = BrIfXslt32I8 / < / get_i32;
1653        fn br_if_xslt32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1654            = BrIfXslt32I32 / < / get_i32;
1655        fn br_if_xsgt32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1656            = BrIfXsgt32I8 / > / get_i32;
1657        fn br_if_xsgt32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1658            = BrIfXsgt32I32 / > / get_i32;
1659        fn br_if_xslteq32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1660            = BrIfXslteq32I8 / <= / get_i32;
1661        fn br_if_xslteq32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1662            = BrIfXslteq32I32 / <= / get_i32;
1663        fn br_if_xsgteq32_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1664            = BrIfXsgteq32I8 / >= / get_i32;
1665        fn br_if_xsgteq32_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1666            = BrIfXsgteq32I32 / >= / get_i32;
1667
1668        fn br_if_xult32_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1669            = BrIfXult32U8 / < / get_u32;
1670        fn br_if_xult32_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1671            = BrIfXult32U32 / < / get_u32;
1672        fn br_if_xugt32_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1673            = BrIfXugt32U8 / > / get_u32;
1674        fn br_if_xugt32_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1675            = BrIfXugt32U32 / > / get_u32;
1676        fn br_if_xulteq32_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1677            = BrIfXulteq32U8 / <= / get_u32;
1678        fn br_if_xulteq32_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1679            = BrIfXulteq32U32 / <= / get_u32;
1680        fn br_if_xugteq32_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1681            = BrIfXugteq32U8 / >= / get_u32;
1682        fn br_if_xugteq32_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1683            = BrIfXugteq32U32 / >= / get_u32;
1684
1685        fn br_if_xeq64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1686            = BrIfXeq64I8 / == / get_i64;
1687        fn br_if_xeq64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1688            = BrIfXeq64I32 / == / get_i64;
1689        fn br_if_xneq64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1690            = BrIfXneq64I8 / != / get_i64;
1691        fn br_if_xneq64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1692            = BrIfXneq64I32 / != / get_i64;
1693
1694        fn br_if_xslt64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1695            = BrIfXslt64I8 / < / get_i64;
1696        fn br_if_xslt64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1697            = BrIfXslt64I32 / < / get_i64;
1698        fn br_if_xsgt64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1699            = BrIfXsgt64I8 / > / get_i64;
1700        fn br_if_xsgt64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1701            = BrIfXsgt64I32 / > / get_i64;
1702        fn br_if_xslteq64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1703            = BrIfXslteq64I8 / <= / get_i64;
1704        fn br_if_xslteq64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1705            = BrIfXslteq64I32 / <= / get_i64;
1706        fn br_if_xsgteq64_i8(&mut self, a: XReg, b: i8, offset: PcRelOffset)
1707            = BrIfXsgteq64I8 / >= / get_i64;
1708        fn br_if_xsgteq64_i32(&mut self, a: XReg, b: i32, offset: PcRelOffset)
1709            = BrIfXsgteq64I32 / >= / get_i64;
1710
1711        fn br_if_xult64_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1712            = BrIfXult64U8 / < / get_u64;
1713        fn br_if_xult64_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1714            = BrIfXult64U32 / < / get_u64;
1715        fn br_if_xugt64_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1716            = BrIfXugt64U8 / > / get_u64;
1717        fn br_if_xugt64_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1718            = BrIfXugt64U32 / > / get_u64;
1719        fn br_if_xulteq64_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1720            = BrIfXulteq64U8 / <= / get_u64;
1721        fn br_if_xulteq64_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1722            = BrIfXulteq64U32 / <= / get_u64;
1723        fn br_if_xugteq64_u8(&mut self, a: XReg, b: u8, offset: PcRelOffset)
1724            = BrIfXugteq64U8 / >= / get_u64;
1725        fn br_if_xugteq64_u32(&mut self, a: XReg, b: u32, offset: PcRelOffset)
1726            = BrIfXugteq64U32 / >= / get_u64;
1727    }
1728
1729    fn xmov(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
1730        let val = self.state[src];
1731        self.state[dst] = val;
1732        ControlFlow::Continue(())
1733    }
1734
1735    fn xconst8(&mut self, dst: XReg, imm: i8) -> ControlFlow<Done> {
1736        self.state[dst].set_i64(i64::from(imm));
1737        ControlFlow::Continue(())
1738    }
1739
1740    fn xzero(&mut self, dst: XReg) -> ControlFlow<Done> {
1741        self.state[dst].set_i64(0);
1742        ControlFlow::Continue(())
1743    }
1744
1745    fn xone(&mut self, dst: XReg) -> ControlFlow<Done> {
1746        self.state[dst].set_i64(1);
1747        ControlFlow::Continue(())
1748    }
1749
1750    fn xconst16(&mut self, dst: XReg, imm: i16) -> ControlFlow<Done> {
1751        self.state[dst].set_i64(i64::from(imm));
1752        ControlFlow::Continue(())
1753    }
1754
1755    fn xconst32(&mut self, dst: XReg, imm: i32) -> ControlFlow<Done> {
1756        self.state[dst].set_i64(i64::from(imm));
1757        ControlFlow::Continue(())
1758    }
1759
1760    fn xconst64(&mut self, dst: XReg, imm: i64) -> ControlFlow<Done> {
1761        self.state[dst].set_i64(imm);
1762        ControlFlow::Continue(())
1763    }
1764
1765    fn xadd32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1766        let a = self.state[operands.src1].get_u32();
1767        let b = self.state[operands.src2].get_u32();
1768        self.state[operands.dst].set_u32(a.wrapping_add(b));
1769        ControlFlow::Continue(())
1770    }
1771
1772    fn xadd32_u8(&mut self, dst: XReg, src1: XReg, src2: u8) -> ControlFlow<Done> {
1773        self.xadd32_u32(dst, src1, src2.into())
1774    }
1775
1776    fn xadd32_u32(&mut self, dst: XReg, src1: XReg, src2: u32) -> ControlFlow<Done> {
1777        let a = self.state[src1].get_u32();
1778        self.state[dst].set_u32(a.wrapping_add(src2));
1779        ControlFlow::Continue(())
1780    }
1781
1782    fn xadd64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1783        let a = self.state[operands.src1].get_u64();
1784        let b = self.state[operands.src2].get_u64();
1785        self.state[operands.dst].set_u64(a.wrapping_add(b));
1786        ControlFlow::Continue(())
1787    }
1788
1789    fn xadd64_u8(&mut self, dst: XReg, src1: XReg, src2: u8) -> ControlFlow<Done> {
1790        self.xadd64_u32(dst, src1, src2.into())
1791    }
1792
1793    fn xadd64_u32(&mut self, dst: XReg, src1: XReg, src2: u32) -> ControlFlow<Done> {
1794        let a = self.state[src1].get_u64();
1795        self.state[dst].set_u64(a.wrapping_add(src2.into()));
1796        ControlFlow::Continue(())
1797    }
1798
1799    fn xmadd32(&mut self, dst: XReg, src1: XReg, src2: XReg, src3: XReg) -> ControlFlow<Done> {
1800        let a = self.state[src1].get_u32();
1801        let b = self.state[src2].get_u32();
1802        let c = self.state[src3].get_u32();
1803        self.state[dst].set_u32(a.wrapping_mul(b).wrapping_add(c));
1804        ControlFlow::Continue(())
1805    }
1806
1807    fn xmadd64(&mut self, dst: XReg, src1: XReg, src2: XReg, src3: XReg) -> ControlFlow<Done> {
1808        let a = self.state[src1].get_u64();
1809        let b = self.state[src2].get_u64();
1810        let c = self.state[src3].get_u64();
1811        self.state[dst].set_u64(a.wrapping_mul(b).wrapping_add(c));
1812        ControlFlow::Continue(())
1813    }
1814
1815    fn xsub32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1816        let a = self.state[operands.src1].get_u32();
1817        let b = self.state[operands.src2].get_u32();
1818        self.state[operands.dst].set_u32(a.wrapping_sub(b));
1819        ControlFlow::Continue(())
1820    }
1821
1822    fn xsub32_u8(&mut self, dst: XReg, src1: XReg, src2: u8) -> ControlFlow<Done> {
1823        self.xsub32_u32(dst, src1, src2.into())
1824    }
1825
1826    fn xsub32_u32(&mut self, dst: XReg, src1: XReg, src2: u32) -> ControlFlow<Done> {
1827        let a = self.state[src1].get_u32();
1828        self.state[dst].set_u32(a.wrapping_sub(src2));
1829        ControlFlow::Continue(())
1830    }
1831
1832    fn xsub64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1833        let a = self.state[operands.src1].get_u64();
1834        let b = self.state[operands.src2].get_u64();
1835        self.state[operands.dst].set_u64(a.wrapping_sub(b));
1836        ControlFlow::Continue(())
1837    }
1838
1839    fn xsub64_u8(&mut self, dst: XReg, src1: XReg, src2: u8) -> ControlFlow<Done> {
1840        self.xsub64_u32(dst, src1, src2.into())
1841    }
1842
1843    fn xsub64_u32(&mut self, dst: XReg, src1: XReg, src2: u32) -> ControlFlow<Done> {
1844        let a = self.state[src1].get_u64();
1845        self.state[dst].set_u64(a.wrapping_sub(src2.into()));
1846        ControlFlow::Continue(())
1847    }
1848
1849    fn xmul32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1850        let a = self.state[operands.src1].get_u32();
1851        let b = self.state[operands.src2].get_u32();
1852        self.state[operands.dst].set_u32(a.wrapping_mul(b));
1853        ControlFlow::Continue(())
1854    }
1855
1856    fn xmul32_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
1857        self.xmul32_s32(dst, src1, src2.into())
1858    }
1859
1860    fn xmul32_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
1861        let a = self.state[src1].get_i32();
1862        self.state[dst].set_i32(a.wrapping_mul(src2));
1863        ControlFlow::Continue(())
1864    }
1865
1866    fn xmul64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1867        let a = self.state[operands.src1].get_u64();
1868        let b = self.state[operands.src2].get_u64();
1869        self.state[operands.dst].set_u64(a.wrapping_mul(b));
1870        ControlFlow::Continue(())
1871    }
1872
1873    fn xmul64_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
1874        self.xmul64_s32(dst, src1, src2.into())
1875    }
1876
1877    fn xmul64_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
1878        let a = self.state[src1].get_i64();
1879        self.state[dst].set_i64(a.wrapping_mul(src2.into()));
1880        ControlFlow::Continue(())
1881    }
1882
1883    fn xshl32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1884        let a = self.state[operands.src1].get_u32();
1885        let b = self.state[operands.src2].get_u32();
1886        self.state[operands.dst].set_u32(a.wrapping_shl(b));
1887        ControlFlow::Continue(())
1888    }
1889
1890    fn xshr32_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1891        let a = self.state[operands.src1].get_u32();
1892        let b = self.state[operands.src2].get_u32();
1893        self.state[operands.dst].set_u32(a.wrapping_shr(b));
1894        ControlFlow::Continue(())
1895    }
1896
1897    fn xshr32_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1898        let a = self.state[operands.src1].get_i32();
1899        let b = self.state[operands.src2].get_u32();
1900        self.state[operands.dst].set_i32(a.wrapping_shr(b));
1901        ControlFlow::Continue(())
1902    }
1903
1904    fn xshl64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1905        let a = self.state[operands.src1].get_u64();
1906        let b = self.state[operands.src2].get_u32();
1907        self.state[operands.dst].set_u64(a.wrapping_shl(b));
1908        ControlFlow::Continue(())
1909    }
1910
1911    fn xshr64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1912        let a = self.state[operands.src1].get_u64();
1913        let b = self.state[operands.src2].get_u32();
1914        self.state[operands.dst].set_u64(a.wrapping_shr(b));
1915        ControlFlow::Continue(())
1916    }
1917
1918    fn xshr64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1919        let a = self.state[operands.src1].get_i64();
1920        let b = self.state[operands.src2].get_u32();
1921        self.state[operands.dst].set_i64(a.wrapping_shr(b));
1922        ControlFlow::Continue(())
1923    }
1924
1925    fn xshl32_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1926        let a = self.state[operands.src1].get_u32();
1927        let b = u32::from(u8::from(operands.src2));
1928        self.state[operands.dst].set_u32(a.wrapping_shl(b));
1929        ControlFlow::Continue(())
1930    }
1931
1932    fn xshr32_u_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1933        let a = self.state[operands.src1].get_u32();
1934        let b = u32::from(u8::from(operands.src2));
1935        self.state[operands.dst].set_u32(a.wrapping_shr(b));
1936        ControlFlow::Continue(())
1937    }
1938
1939    fn xshr32_s_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1940        let a = self.state[operands.src1].get_i32();
1941        let b = u32::from(u8::from(operands.src2));
1942        self.state[operands.dst].set_i32(a.wrapping_shr(b));
1943        ControlFlow::Continue(())
1944    }
1945
1946    fn xshl64_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1947        let a = self.state[operands.src1].get_u64();
1948        let b = u32::from(u8::from(operands.src2));
1949        self.state[operands.dst].set_u64(a.wrapping_shl(b));
1950        ControlFlow::Continue(())
1951    }
1952
1953    fn xshr64_u_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1954        let a = self.state[operands.src1].get_u64();
1955        let b = u32::from(u8::from(operands.src2));
1956        self.state[operands.dst].set_u64(a.wrapping_shr(b));
1957        ControlFlow::Continue(())
1958    }
1959
1960    fn xshr64_s_u6(&mut self, operands: BinaryOperands<XReg, XReg, U6>) -> ControlFlow<Done> {
1961        let a = self.state[operands.src1].get_i64();
1962        let b = u32::from(u8::from(operands.src2));
1963        self.state[operands.dst].set_i64(a.wrapping_shr(b));
1964        ControlFlow::Continue(())
1965    }
1966
1967    fn xneg32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
1968        let a = self.state[src].get_i32();
1969        self.state[dst].set_i32(a.wrapping_neg());
1970        ControlFlow::Continue(())
1971    }
1972
1973    fn xneg64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
1974        let a = self.state[src].get_i64();
1975        self.state[dst].set_i64(a.wrapping_neg());
1976        ControlFlow::Continue(())
1977    }
1978
1979    fn xeq64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1980        let a = self.state[operands.src1].get_u64();
1981        let b = self.state[operands.src2].get_u64();
1982        self.state[operands.dst].set_u32(u32::from(a == b));
1983        ControlFlow::Continue(())
1984    }
1985
1986    fn xneq64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1987        let a = self.state[operands.src1].get_u64();
1988        let b = self.state[operands.src2].get_u64();
1989        self.state[operands.dst].set_u32(u32::from(a != b));
1990        ControlFlow::Continue(())
1991    }
1992
1993    fn xslt64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
1994        let a = self.state[operands.src1].get_i64();
1995        let b = self.state[operands.src2].get_i64();
1996        self.state[operands.dst].set_u32(u32::from(a < b));
1997        ControlFlow::Continue(())
1998    }
1999
2000    fn xslteq64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2001        let a = self.state[operands.src1].get_i64();
2002        let b = self.state[operands.src2].get_i64();
2003        self.state[operands.dst].set_u32(u32::from(a <= b));
2004        ControlFlow::Continue(())
2005    }
2006
2007    fn xult64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2008        let a = self.state[operands.src1].get_u64();
2009        let b = self.state[operands.src2].get_u64();
2010        self.state[operands.dst].set_u32(u32::from(a < b));
2011        ControlFlow::Continue(())
2012    }
2013
2014    fn xulteq64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2015        let a = self.state[operands.src1].get_u64();
2016        let b = self.state[operands.src2].get_u64();
2017        self.state[operands.dst].set_u32(u32::from(a <= b));
2018        ControlFlow::Continue(())
2019    }
2020
2021    fn xeq32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2022        let a = self.state[operands.src1].get_u32();
2023        let b = self.state[operands.src2].get_u32();
2024        self.state[operands.dst].set_u32(u32::from(a == b));
2025        ControlFlow::Continue(())
2026    }
2027
2028    fn xneq32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2029        let a = self.state[operands.src1].get_u32();
2030        let b = self.state[operands.src2].get_u32();
2031        self.state[operands.dst].set_u32(u32::from(a != b));
2032        ControlFlow::Continue(())
2033    }
2034
2035    fn xslt32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2036        let a = self.state[operands.src1].get_i32();
2037        let b = self.state[operands.src2].get_i32();
2038        self.state[operands.dst].set_u32(u32::from(a < b));
2039        ControlFlow::Continue(())
2040    }
2041
2042    fn xslteq32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2043        let a = self.state[operands.src1].get_i32();
2044        let b = self.state[operands.src2].get_i32();
2045        self.state[operands.dst].set_u32(u32::from(a <= b));
2046        ControlFlow::Continue(())
2047    }
2048
2049    fn xult32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2050        let a = self.state[operands.src1].get_u32();
2051        let b = self.state[operands.src2].get_u32();
2052        self.state[operands.dst].set_u32(u32::from(a < b));
2053        ControlFlow::Continue(())
2054    }
2055
2056    fn xulteq32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2057        let a = self.state[operands.src1].get_u32();
2058        let b = self.state[operands.src2].get_u32();
2059        self.state[operands.dst].set_u32(u32::from(a <= b));
2060        ControlFlow::Continue(())
2061    }
2062
2063    fn push_frame(&mut self) -> ControlFlow<Done> {
2064        self.push::<crate::PushFrame, _>(self.state.lr)?;
2065        self.push::<crate::PushFrame, _>(self.state.fp)?;
2066        self.state.fp = self.state[XReg::sp].get_ptr();
2067        ControlFlow::Continue(())
2068    }
2069
2070    #[inline]
2071    fn push_frame_save(&mut self, amt: u16, regs: UpperRegSet<XReg>) -> ControlFlow<Done> {
2072        self.push_frame_full_impl::<crate::PushFrameSave>(0, usize::from(amt), regs)
2073    }
2074
2075    fn pop_frame_restore(&mut self, amt: u16, regs: UpperRegSet<XReg>) -> ControlFlow<Done> {
2076        // Restore all registers in `regs`, followed by the normal `pop_frame`
2077        // opcode below to restore fp/lr.
2078        unsafe {
2079            let mut offset = i32::from(amt);
2080            for reg in regs {
2081                offset -= 8;
2082                let val = self.load_ne::<_, crate::PopFrameRestore>(AddrO32 {
2083                    addr: XReg::sp,
2084                    offset,
2085                })?;
2086                self.state[reg].set_u64(val);
2087            }
2088        }
2089        self.pop_frame()
2090    }
2091
2092    fn pop_frame(&mut self) -> ControlFlow<Done> {
2093        self.set_sp_unchecked(self.state.fp);
2094        let fp = self.pop();
2095        let lr = self.pop();
2096        self.state.fp = fp;
2097        self.state.lr = lr;
2098        ControlFlow::Continue(())
2099    }
2100
2101    fn br_table32(&mut self, idx: XReg, amt: u32) -> ControlFlow<Done> {
2102        let idx = self.state[idx].get_u32().min(amt - 1) as isize;
2103        // SAFETY: part of the contract of the interpreter is only dealing with
2104        // valid bytecode, so this offset should be safe.
2105        self.pc = unsafe { self.pc.offset(idx * 4) };
2106
2107        // Decode the `PcRelOffset` without tampering with `self.pc` as the
2108        // jump is relative to `self.pc`.
2109        let mut tmp = self.pc;
2110        let Ok(rel) = PcRelOffset::decode(&mut tmp);
2111        let offset = isize::try_from(i32::from(rel)).unwrap();
2112        self.pc = unsafe { self.pc.offset(offset) };
2113        ControlFlow::Continue(())
2114    }
2115
2116    fn stack_alloc32(&mut self, amt: u32) -> ControlFlow<Done> {
2117        let amt = usize::try_from(amt).unwrap();
2118        let new_sp = self.state[XReg::sp].get_ptr::<u8>().wrapping_sub(amt);
2119        self.set_sp::<crate::StackAlloc32>(new_sp)?;
2120        ControlFlow::Continue(())
2121    }
2122
2123    fn stack_free32(&mut self, amt: u32) -> ControlFlow<Done> {
2124        let amt = usize::try_from(amt).unwrap();
2125        let new_sp = self.state[XReg::sp].get_ptr::<u8>().wrapping_add(amt);
2126        self.set_sp_unchecked(new_sp);
2127        ControlFlow::Continue(())
2128    }
2129
2130    fn zext8(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2131        let src = self.state[src].get_u64() as u8;
2132        self.state[dst].set_u64(src.into());
2133        ControlFlow::Continue(())
2134    }
2135
2136    fn zext16(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2137        let src = self.state[src].get_u64() as u16;
2138        self.state[dst].set_u64(src.into());
2139        ControlFlow::Continue(())
2140    }
2141
2142    fn zext32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2143        let src = self.state[src].get_u64() as u32;
2144        self.state[dst].set_u64(src.into());
2145        ControlFlow::Continue(())
2146    }
2147
2148    fn sext8(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2149        let src = self.state[src].get_i64() as i8;
2150        self.state[dst].set_i64(src.into());
2151        ControlFlow::Continue(())
2152    }
2153
2154    fn sext16(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2155        let src = self.state[src].get_i64() as i16;
2156        self.state[dst].set_i64(src.into());
2157        ControlFlow::Continue(())
2158    }
2159
2160    fn sext32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2161        let src = self.state[src].get_i64() as i32;
2162        self.state[dst].set_i64(src.into());
2163        ControlFlow::Continue(())
2164    }
2165
2166    fn xdiv32_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2167        let a = self.state[operands.src1].get_i32();
2168        let b = self.state[operands.src2].get_i32();
2169        match a.checked_div(b) {
2170            Some(result) => {
2171                self.state[operands.dst].set_i32(result);
2172                ControlFlow::Continue(())
2173            }
2174            None => {
2175                let kind = if b == 0 {
2176                    TrapKind::DivideByZero
2177                } else {
2178                    TrapKind::IntegerOverflow
2179                };
2180                self.done_trap_kind::<crate::XDiv32S>(Some(kind))
2181            }
2182        }
2183    }
2184
2185    fn xdiv64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2186        let a = self.state[operands.src1].get_i64();
2187        let b = self.state[operands.src2].get_i64();
2188        match a.checked_div(b) {
2189            Some(result) => {
2190                self.state[operands.dst].set_i64(result);
2191                ControlFlow::Continue(())
2192            }
2193            None => {
2194                let kind = if b == 0 {
2195                    TrapKind::DivideByZero
2196                } else {
2197                    TrapKind::IntegerOverflow
2198                };
2199                self.done_trap_kind::<crate::XDiv64S>(Some(kind))
2200            }
2201        }
2202    }
2203
2204    fn xdiv32_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2205        let a = self.state[operands.src1].get_u32();
2206        let b = self.state[operands.src2].get_u32();
2207        match a.checked_div(b) {
2208            Some(result) => {
2209                self.state[operands.dst].set_u32(result);
2210                ControlFlow::Continue(())
2211            }
2212            None => self.done_trap_kind::<crate::XDiv32U>(Some(TrapKind::DivideByZero)),
2213        }
2214    }
2215
2216    fn xdiv64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2217        let a = self.state[operands.src1].get_u64();
2218        let b = self.state[operands.src2].get_u64();
2219        match a.checked_div(b) {
2220            Some(result) => {
2221                self.state[operands.dst].set_u64(result);
2222                ControlFlow::Continue(())
2223            }
2224            None => self.done_trap_kind::<crate::XDiv64U>(Some(TrapKind::DivideByZero)),
2225        }
2226    }
2227
2228    fn xrem32_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2229        let a = self.state[operands.src1].get_i32();
2230        let b = self.state[operands.src2].get_i32();
2231        let result = if a == i32::MIN && b == -1 {
2232            Some(0)
2233        } else {
2234            a.checked_rem(b)
2235        };
2236        match result {
2237            Some(result) => {
2238                self.state[operands.dst].set_i32(result);
2239                ControlFlow::Continue(())
2240            }
2241            None => self.done_trap_kind::<crate::XRem32S>(Some(TrapKind::DivideByZero)),
2242        }
2243    }
2244
2245    fn xrem64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2246        let a = self.state[operands.src1].get_i64();
2247        let b = self.state[operands.src2].get_i64();
2248        let result = if a == i64::MIN && b == -1 {
2249            Some(0)
2250        } else {
2251            a.checked_rem(b)
2252        };
2253        match result {
2254            Some(result) => {
2255                self.state[operands.dst].set_i64(result);
2256                ControlFlow::Continue(())
2257            }
2258            None => self.done_trap_kind::<crate::XRem64S>(Some(TrapKind::DivideByZero)),
2259        }
2260    }
2261
2262    fn xrem32_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2263        let a = self.state[operands.src1].get_u32();
2264        let b = self.state[operands.src2].get_u32();
2265        match a.checked_rem(b) {
2266            Some(result) => {
2267                self.state[operands.dst].set_u32(result);
2268                ControlFlow::Continue(())
2269            }
2270            None => self.done_trap_kind::<crate::XRem32U>(Some(TrapKind::DivideByZero)),
2271        }
2272    }
2273
2274    fn xrem64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2275        let a = self.state[operands.src1].get_u64();
2276        let b = self.state[operands.src2].get_u64();
2277        match a.checked_rem(b) {
2278            Some(result) => {
2279                self.state[operands.dst].set_u64(result);
2280                ControlFlow::Continue(())
2281            }
2282            None => self.done_trap_kind::<crate::XRem64U>(Some(TrapKind::DivideByZero)),
2283        }
2284    }
2285
2286    fn xband32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2287        let a = self.state[operands.src1].get_u32();
2288        let b = self.state[operands.src2].get_u32();
2289        self.state[operands.dst].set_u32(a & b);
2290        ControlFlow::Continue(())
2291    }
2292
2293    fn xband32_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2294        self.xband32_s32(dst, src1, src2.into())
2295    }
2296
2297    fn xband32_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2298        let a = self.state[src1].get_i32();
2299        self.state[dst].set_i32(a & src2);
2300        ControlFlow::Continue(())
2301    }
2302
2303    fn xband64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2304        let a = self.state[operands.src1].get_u64();
2305        let b = self.state[operands.src2].get_u64();
2306        self.state[operands.dst].set_u64(a & b);
2307        ControlFlow::Continue(())
2308    }
2309
2310    fn xband64_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2311        self.xband64_s32(dst, src1, src2.into())
2312    }
2313
2314    fn xband64_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2315        let a = self.state[src1].get_i64();
2316        self.state[dst].set_i64(a & i64::from(src2));
2317        ControlFlow::Continue(())
2318    }
2319
2320    fn xbor32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2321        let a = self.state[operands.src1].get_u32();
2322        let b = self.state[operands.src2].get_u32();
2323        self.state[operands.dst].set_u32(a | b);
2324        ControlFlow::Continue(())
2325    }
2326
2327    fn xbor32_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2328        self.xbor32_s32(dst, src1, src2.into())
2329    }
2330
2331    fn xbor32_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2332        let a = self.state[src1].get_i32();
2333        self.state[dst].set_i32(a | src2);
2334        ControlFlow::Continue(())
2335    }
2336
2337    fn xbor64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2338        let a = self.state[operands.src1].get_u64();
2339        let b = self.state[operands.src2].get_u64();
2340        self.state[operands.dst].set_u64(a | b);
2341        ControlFlow::Continue(())
2342    }
2343
2344    fn xbor64_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2345        self.xbor64_s32(dst, src1, src2.into())
2346    }
2347
2348    fn xbor64_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2349        let a = self.state[src1].get_i64();
2350        self.state[dst].set_i64(a | i64::from(src2));
2351        ControlFlow::Continue(())
2352    }
2353
2354    fn xbxor32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2355        let a = self.state[operands.src1].get_u32();
2356        let b = self.state[operands.src2].get_u32();
2357        self.state[operands.dst].set_u32(a ^ b);
2358        ControlFlow::Continue(())
2359    }
2360
2361    fn xbxor32_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2362        self.xbxor32_s32(dst, src1, src2.into())
2363    }
2364
2365    fn xbxor32_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2366        let a = self.state[src1].get_i32();
2367        self.state[dst].set_i32(a ^ src2);
2368        ControlFlow::Continue(())
2369    }
2370
2371    fn xbxor64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2372        let a = self.state[operands.src1].get_u64();
2373        let b = self.state[operands.src2].get_u64();
2374        self.state[operands.dst].set_u64(a ^ b);
2375        ControlFlow::Continue(())
2376    }
2377
2378    fn xbxor64_s8(&mut self, dst: XReg, src1: XReg, src2: i8) -> ControlFlow<Done> {
2379        self.xbxor64_s32(dst, src1, src2.into())
2380    }
2381
2382    fn xbxor64_s32(&mut self, dst: XReg, src1: XReg, src2: i32) -> ControlFlow<Done> {
2383        let a = self.state[src1].get_i64();
2384        self.state[dst].set_i64(a ^ i64::from(src2));
2385        ControlFlow::Continue(())
2386    }
2387
2388    fn xbnot32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2389        let a = self.state[src].get_u32();
2390        self.state[dst].set_u32(!a);
2391        ControlFlow::Continue(())
2392    }
2393
2394    fn xbnot64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2395        let a = self.state[src].get_u64();
2396        self.state[dst].set_u64(!a);
2397        ControlFlow::Continue(())
2398    }
2399
2400    fn xmin32_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2401        let a = self.state[operands.src1].get_u32();
2402        let b = self.state[operands.src2].get_u32();
2403        self.state[operands.dst].set_u32(a.min(b));
2404        ControlFlow::Continue(())
2405    }
2406
2407    fn xmin32_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2408        let a = self.state[operands.src1].get_i32();
2409        let b = self.state[operands.src2].get_i32();
2410        self.state[operands.dst].set_i32(a.min(b));
2411        ControlFlow::Continue(())
2412    }
2413
2414    fn xmax32_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2415        let a = self.state[operands.src1].get_u32();
2416        let b = self.state[operands.src2].get_u32();
2417        self.state[operands.dst].set_u32(a.max(b));
2418        ControlFlow::Continue(())
2419    }
2420
2421    fn xmax32_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2422        let a = self.state[operands.src1].get_i32();
2423        let b = self.state[operands.src2].get_i32();
2424        self.state[operands.dst].set_i32(a.max(b));
2425        ControlFlow::Continue(())
2426    }
2427
2428    fn xmin64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2429        let a = self.state[operands.src1].get_u64();
2430        let b = self.state[operands.src2].get_u64();
2431        self.state[operands.dst].set_u64(a.min(b));
2432        ControlFlow::Continue(())
2433    }
2434
2435    fn xmin64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2436        let a = self.state[operands.src1].get_i64();
2437        let b = self.state[operands.src2].get_i64();
2438        self.state[operands.dst].set_i64(a.min(b));
2439        ControlFlow::Continue(())
2440    }
2441
2442    fn xmax64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2443        let a = self.state[operands.src1].get_u64();
2444        let b = self.state[operands.src2].get_u64();
2445        self.state[operands.dst].set_u64(a.max(b));
2446        ControlFlow::Continue(())
2447    }
2448
2449    fn xmax64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2450        let a = self.state[operands.src1].get_i64();
2451        let b = self.state[operands.src2].get_i64();
2452        self.state[operands.dst].set_i64(a.max(b));
2453        ControlFlow::Continue(())
2454    }
2455
2456    fn xctz32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2457        let a = self.state[src].get_u32();
2458        self.state[dst].set_u32(a.trailing_zeros());
2459        ControlFlow::Continue(())
2460    }
2461
2462    fn xctz64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2463        let a = self.state[src].get_u64();
2464        self.state[dst].set_u64(a.trailing_zeros().into());
2465        ControlFlow::Continue(())
2466    }
2467
2468    fn xclz32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2469        let a = self.state[src].get_u32();
2470        self.state[dst].set_u32(a.leading_zeros());
2471        ControlFlow::Continue(())
2472    }
2473
2474    fn xclz64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2475        let a = self.state[src].get_u64();
2476        self.state[dst].set_u64(a.leading_zeros().into());
2477        ControlFlow::Continue(())
2478    }
2479
2480    fn xpopcnt32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2481        let a = self.state[src].get_u32();
2482        self.state[dst].set_u32(a.count_ones());
2483        ControlFlow::Continue(())
2484    }
2485
2486    fn xpopcnt64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2487        let a = self.state[src].get_u64();
2488        self.state[dst].set_u64(a.count_ones().into());
2489        ControlFlow::Continue(())
2490    }
2491
2492    fn xrotl32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2493        let a = self.state[operands.src1].get_u32();
2494        let b = self.state[operands.src2].get_u32();
2495        self.state[operands.dst].set_u32(a.rotate_left(b));
2496        ControlFlow::Continue(())
2497    }
2498
2499    fn xrotl64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2500        let a = self.state[operands.src1].get_u64();
2501        let b = self.state[operands.src2].get_u32();
2502        self.state[operands.dst].set_u64(a.rotate_left(b));
2503        ControlFlow::Continue(())
2504    }
2505
2506    fn xrotr32(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2507        let a = self.state[operands.src1].get_u32();
2508        let b = self.state[operands.src2].get_u32();
2509        self.state[operands.dst].set_u32(a.rotate_right(b));
2510        ControlFlow::Continue(())
2511    }
2512
2513    fn xrotr64(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2514        let a = self.state[operands.src1].get_u64();
2515        let b = self.state[operands.src2].get_u32();
2516        self.state[operands.dst].set_u64(a.rotate_right(b));
2517        ControlFlow::Continue(())
2518    }
2519
2520    fn xselect32(
2521        &mut self,
2522        dst: XReg,
2523        cond: XReg,
2524        if_nonzero: XReg,
2525        if_zero: XReg,
2526    ) -> ControlFlow<Done> {
2527        let result = if self.state[cond].get_u32() != 0 {
2528            self.state[if_nonzero].get_u32()
2529        } else {
2530            self.state[if_zero].get_u32()
2531        };
2532        self.state[dst].set_u32(result);
2533        ControlFlow::Continue(())
2534    }
2535
2536    fn xselect64(
2537        &mut self,
2538        dst: XReg,
2539        cond: XReg,
2540        if_nonzero: XReg,
2541        if_zero: XReg,
2542    ) -> ControlFlow<Done> {
2543        let result = if self.state[cond].get_u32() != 0 {
2544            self.state[if_nonzero].get_u64()
2545        } else {
2546            self.state[if_zero].get_u64()
2547        };
2548        self.state[dst].set_u64(result);
2549        ControlFlow::Continue(())
2550    }
2551
2552    fn xabs32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2553        let a = self.state[src].get_i32();
2554        self.state[dst].set_i32(a.wrapping_abs());
2555        ControlFlow::Continue(())
2556    }
2557
2558    fn xabs64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2559        let a = self.state[src].get_i64();
2560        self.state[dst].set_i64(a.wrapping_abs());
2561        ControlFlow::Continue(())
2562    }
2563
2564    // =========================================================================
2565    // o32 addressing modes
2566
2567    fn xload8_u32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2568        let result = unsafe { self.load_ne::<u8, crate::XLoad8U32O32>(addr)? };
2569        self.state[dst].set_u32(result.into());
2570        ControlFlow::Continue(())
2571    }
2572
2573    fn xload8_s32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2574        let result = unsafe { self.load_ne::<i8, crate::XLoad8S32O32>(addr)? };
2575        self.state[dst].set_i32(result.into());
2576        ControlFlow::Continue(())
2577    }
2578
2579    fn xload16le_u32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2580        let result = unsafe { self.load_ne::<u16, crate::XLoad16LeU32O32>(addr)? };
2581        self.state[dst].set_u32(u16::from_le(result).into());
2582        ControlFlow::Continue(())
2583    }
2584
2585    fn xload16le_s32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2586        let result = unsafe { self.load_ne::<i16, crate::XLoad16LeS32O32>(addr)? };
2587        self.state[dst].set_i32(i16::from_le(result).into());
2588        ControlFlow::Continue(())
2589    }
2590
2591    fn xload32le_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2592        let result = unsafe { self.load_ne::<i32, crate::XLoad32LeO32>(addr)? };
2593        self.state[dst].set_i32(i32::from_le(result));
2594        ControlFlow::Continue(())
2595    }
2596
2597    fn xload64le_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2598        let result = unsafe { self.load_ne::<i64, crate::XLoad64LeO32>(addr)? };
2599        self.state[dst].set_i64(i64::from_le(result));
2600        ControlFlow::Continue(())
2601    }
2602
2603    fn xstore8_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2604        let val = self.state[val].get_u32() as u8;
2605        unsafe {
2606            self.store_ne::<u8, crate::XStore8O32>(addr, val)?;
2607        }
2608        ControlFlow::Continue(())
2609    }
2610
2611    fn xstore16le_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2612        let val = self.state[val].get_u32() as u16;
2613        unsafe {
2614            self.store_ne::<u16, crate::XStore16LeO32>(addr, val.to_le())?;
2615        }
2616        ControlFlow::Continue(())
2617    }
2618
2619    fn xstore32le_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2620        let val = self.state[val].get_u32();
2621        unsafe {
2622            self.store_ne::<u32, crate::XStore32LeO32>(addr, val.to_le())?;
2623        }
2624        ControlFlow::Continue(())
2625    }
2626
2627    fn xstore64le_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2628        let val = self.state[val].get_u64();
2629        unsafe {
2630            self.store_ne::<u64, crate::XStore64LeO32>(addr, val.to_le())?;
2631        }
2632        ControlFlow::Continue(())
2633    }
2634
2635    // =========================================================================
2636    // g32 addressing modes
2637
2638    fn xload8_u32_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2639        let result = unsafe { self.load_ne::<u8, crate::XLoad8U32G32>(addr)? };
2640        self.state[dst].set_u32(result.into());
2641        ControlFlow::Continue(())
2642    }
2643
2644    fn xload8_s32_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2645        let result = unsafe { self.load_ne::<i8, crate::XLoad8S32G32>(addr)? };
2646        self.state[dst].set_i32(result.into());
2647        ControlFlow::Continue(())
2648    }
2649
2650    fn xload16le_u32_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2651        let result = unsafe { self.load_ne::<u16, crate::XLoad16LeU32G32>(addr)? };
2652        self.state[dst].set_u32(u16::from_le(result).into());
2653        ControlFlow::Continue(())
2654    }
2655
2656    fn xload16le_s32_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2657        let result = unsafe { self.load_ne::<i16, crate::XLoad16LeS32G32>(addr)? };
2658        self.state[dst].set_i32(i16::from_le(result).into());
2659        ControlFlow::Continue(())
2660    }
2661
2662    fn xload32le_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2663        let result = unsafe { self.load_ne::<i32, crate::XLoad32LeG32>(addr)? };
2664        self.state[dst].set_i32(i32::from_le(result));
2665        ControlFlow::Continue(())
2666    }
2667
2668    fn xload64le_g32(&mut self, dst: XReg, addr: AddrG32) -> ControlFlow<Done> {
2669        let result = unsafe { self.load_ne::<i64, crate::XLoad64LeG32>(addr)? };
2670        self.state[dst].set_i64(i64::from_le(result));
2671        ControlFlow::Continue(())
2672    }
2673
2674    fn xstore8_g32(&mut self, addr: AddrG32, val: XReg) -> ControlFlow<Done> {
2675        let val = self.state[val].get_u32() as u8;
2676        unsafe {
2677            self.store_ne::<u8, crate::XStore8G32>(addr, val)?;
2678        }
2679        ControlFlow::Continue(())
2680    }
2681
2682    fn xstore16le_g32(&mut self, addr: AddrG32, val: XReg) -> ControlFlow<Done> {
2683        let val = self.state[val].get_u32() as u16;
2684        unsafe {
2685            self.store_ne::<u16, crate::XStore16LeG32>(addr, val.to_le())?;
2686        }
2687        ControlFlow::Continue(())
2688    }
2689
2690    fn xstore32le_g32(&mut self, addr: AddrG32, val: XReg) -> ControlFlow<Done> {
2691        let val = self.state[val].get_u32();
2692        unsafe {
2693            self.store_ne::<u32, crate::XStore32LeG32>(addr, val.to_le())?;
2694        }
2695        ControlFlow::Continue(())
2696    }
2697
2698    fn xstore64le_g32(&mut self, addr: AddrG32, val: XReg) -> ControlFlow<Done> {
2699        let val = self.state[val].get_u64();
2700        unsafe {
2701            self.store_ne::<u64, crate::XStore64LeG32>(addr, val.to_le())?;
2702        }
2703        ControlFlow::Continue(())
2704    }
2705
2706    // =========================================================================
2707    // z addressing modes
2708
2709    fn xload8_u32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2710        let result = unsafe { self.load_ne::<u8, crate::XLoad8U32Z>(addr)? };
2711        self.state[dst].set_u32(result.into());
2712        ControlFlow::Continue(())
2713    }
2714
2715    fn xload8_s32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2716        let result = unsafe { self.load_ne::<i8, crate::XLoad8S32Z>(addr)? };
2717        self.state[dst].set_i32(result.into());
2718        ControlFlow::Continue(())
2719    }
2720
2721    fn xload16le_u32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2722        let result = unsafe { self.load_ne::<u16, crate::XLoad16LeU32Z>(addr)? };
2723        self.state[dst].set_u32(u16::from_le(result).into());
2724        ControlFlow::Continue(())
2725    }
2726
2727    fn xload16le_s32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2728        let result = unsafe { self.load_ne::<i16, crate::XLoad16LeS32Z>(addr)? };
2729        self.state[dst].set_i32(i16::from_le(result).into());
2730        ControlFlow::Continue(())
2731    }
2732
2733    fn xload32le_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2734        let result = unsafe { self.load_ne::<i32, crate::XLoad32LeZ>(addr)? };
2735        self.state[dst].set_i32(i32::from_le(result));
2736        ControlFlow::Continue(())
2737    }
2738
2739    fn xload64le_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
2740        let result = unsafe { self.load_ne::<i64, crate::XLoad64LeZ>(addr)? };
2741        self.state[dst].set_i64(i64::from_le(result));
2742        ControlFlow::Continue(())
2743    }
2744
2745    fn xstore8_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
2746        let val = self.state[val].get_u32() as u8;
2747        unsafe {
2748            self.store_ne::<u8, crate::XStore8Z>(addr, val)?;
2749        }
2750        ControlFlow::Continue(())
2751    }
2752
2753    fn xstore16le_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
2754        let val = self.state[val].get_u32() as u16;
2755        unsafe {
2756            self.store_ne::<u16, crate::XStore16LeZ>(addr, val.to_le())?;
2757        }
2758        ControlFlow::Continue(())
2759    }
2760
2761    fn xstore32le_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
2762        let val = self.state[val].get_u32();
2763        unsafe {
2764            self.store_ne::<u32, crate::XStore32LeZ>(addr, val.to_le())?;
2765        }
2766        ControlFlow::Continue(())
2767    }
2768
2769    fn xstore64le_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
2770        let val = self.state[val].get_u64();
2771        unsafe {
2772            self.store_ne::<u64, crate::XStore64LeZ>(addr, val.to_le())?;
2773        }
2774        ControlFlow::Continue(())
2775    }
2776
2777    // =========================================================================
2778    // g32bne addressing modes
2779
2780    fn xload8_u32_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2781        let result = unsafe { self.load_ne::<u8, crate::XLoad8U32G32Bne>(addr)? };
2782        self.state[dst].set_u32(result.into());
2783        ControlFlow::Continue(())
2784    }
2785
2786    fn xload8_s32_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2787        let result = unsafe { self.load_ne::<i8, crate::XLoad8S32G32Bne>(addr)? };
2788        self.state[dst].set_i32(result.into());
2789        ControlFlow::Continue(())
2790    }
2791
2792    fn xload16le_u32_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2793        let result = unsafe { self.load_ne::<u16, crate::XLoad16LeU32G32Bne>(addr)? };
2794        self.state[dst].set_u32(u16::from_le(result).into());
2795        ControlFlow::Continue(())
2796    }
2797
2798    fn xload16le_s32_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2799        let result = unsafe { self.load_ne::<i16, crate::XLoad16LeS32G32Bne>(addr)? };
2800        self.state[dst].set_i32(i16::from_le(result).into());
2801        ControlFlow::Continue(())
2802    }
2803
2804    fn xload32le_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2805        let result = unsafe { self.load_ne::<i32, crate::XLoad32LeG32Bne>(addr)? };
2806        self.state[dst].set_i32(i32::from_le(result));
2807        ControlFlow::Continue(())
2808    }
2809
2810    fn xload64le_g32bne(&mut self, dst: XReg, addr: AddrG32Bne) -> ControlFlow<Done> {
2811        let result = unsafe { self.load_ne::<i64, crate::XLoad64LeG32Bne>(addr)? };
2812        self.state[dst].set_i64(i64::from_le(result));
2813        ControlFlow::Continue(())
2814    }
2815
2816    fn xstore8_g32bne(&mut self, addr: AddrG32Bne, val: XReg) -> ControlFlow<Done> {
2817        let val = self.state[val].get_u32() as u8;
2818        unsafe {
2819            self.store_ne::<u8, crate::XStore8G32Bne>(addr, val)?;
2820        }
2821        ControlFlow::Continue(())
2822    }
2823
2824    fn xstore16le_g32bne(&mut self, addr: AddrG32Bne, val: XReg) -> ControlFlow<Done> {
2825        let val = self.state[val].get_u32() as u16;
2826        unsafe {
2827            self.store_ne::<u16, crate::XStore16LeG32Bne>(addr, val.to_le())?;
2828        }
2829        ControlFlow::Continue(())
2830    }
2831
2832    fn xstore32le_g32bne(&mut self, addr: AddrG32Bne, val: XReg) -> ControlFlow<Done> {
2833        let val = self.state[val].get_u32();
2834        unsafe {
2835            self.store_ne::<u32, crate::XStore32LeG32Bne>(addr, val.to_le())?;
2836        }
2837        ControlFlow::Continue(())
2838    }
2839
2840    fn xstore64le_g32bne(&mut self, addr: AddrG32Bne, val: XReg) -> ControlFlow<Done> {
2841        let val = self.state[val].get_u64();
2842        unsafe {
2843            self.store_ne::<u64, crate::XStore64LeG32Bne>(addr, val.to_le())?;
2844        }
2845        ControlFlow::Continue(())
2846    }
2847}
2848
2849impl ExtendedOpVisitor for Interpreter<'_> {
2850    fn trap(&mut self) -> ControlFlow<Done> {
2851        self.done_trap::<crate::Trap>()
2852    }
2853
2854    fn call_indirect_host(&mut self, id: u8) -> ControlFlow<Done> {
2855        self.done_call_indirect_host(id)
2856    }
2857
2858    fn push_frame_full(
2859        &mut self,
2860        pre: u32,
2861        amt: u32,
2862        regs: UpperRegSet<XReg>,
2863    ) -> ControlFlow<Done> {
2864        let pre = usize::try_from(pre).unwrap();
2865        let amt = usize::try_from(amt).unwrap();
2866        self.push_frame_full_impl::<crate::PushFrameFull>(pre, amt, regs)
2867    }
2868
2869    fn xpcadd(&mut self, dst: XReg, offset: PcRelOffset) -> ControlFlow<Done> {
2870        let pc = self.pc_rel::<crate::Xpcadd>(offset);
2871        self.state[dst].set_ptr(pc.as_ptr());
2872        ControlFlow::Continue(())
2873    }
2874
2875    fn bswap32(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2876        let src = self.state[src].get_u32();
2877        self.state[dst].set_u32(src.swap_bytes());
2878        ControlFlow::Continue(())
2879    }
2880
2881    fn bswap64(&mut self, dst: XReg, src: XReg) -> ControlFlow<Done> {
2882        let src = self.state[src].get_u64();
2883        self.state[dst].set_u64(src.swap_bytes());
2884        ControlFlow::Continue(())
2885    }
2886
2887    fn xbmask32(&mut self, dst: XReg, src: XReg) -> Self::Return {
2888        let a = self.state[src].get_u32();
2889        if a == 0 {
2890            self.state[dst].set_u32(0);
2891        } else {
2892            self.state[dst].set_i32(-1);
2893        }
2894        ControlFlow::Continue(())
2895    }
2896
2897    fn xbmask64(&mut self, dst: XReg, src: XReg) -> Self::Return {
2898        let a = self.state[src].get_u64();
2899        if a == 0 {
2900            self.state[dst].set_u64(0);
2901        } else {
2902            self.state[dst].set_i64(-1);
2903        }
2904        ControlFlow::Continue(())
2905    }
2906
2907    fn xadd32_uoverflow_trap(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2908        let a = self.state[operands.src1].get_u32();
2909        let b = self.state[operands.src2].get_u32();
2910        match a.checked_add(b) {
2911            Some(c) => {
2912                self.state[operands.dst].set_u32(c);
2913                ControlFlow::Continue(())
2914            }
2915            None => self.done_trap::<crate::Xadd32UoverflowTrap>(),
2916        }
2917    }
2918
2919    fn xadd64_uoverflow_trap(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2920        let a = self.state[operands.src1].get_u64();
2921        let b = self.state[operands.src2].get_u64();
2922        match a.checked_add(b) {
2923            Some(c) => {
2924                self.state[operands.dst].set_u64(c);
2925                ControlFlow::Continue(())
2926            }
2927            None => self.done_trap::<crate::Xadd64UoverflowTrap>(),
2928        }
2929    }
2930
2931    fn xmulhi64_s(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2932        let a = self.state[operands.src1].get_i64();
2933        let b = self.state[operands.src2].get_i64();
2934        let result = ((i128::from(a) * i128::from(b)) >> 64) as i64;
2935        self.state[operands.dst].set_i64(result);
2936        ControlFlow::Continue(())
2937    }
2938
2939    fn xmulhi64_u(&mut self, operands: BinaryOperands<XReg>) -> ControlFlow<Done> {
2940        let a = self.state[operands.src1].get_u64();
2941        let b = self.state[operands.src2].get_u64();
2942        let result = ((u128::from(a) * u128::from(b)) >> 64) as u64;
2943        self.state[operands.dst].set_u64(result);
2944        ControlFlow::Continue(())
2945    }
2946
2947    // =========================================================================
2948    // o32 addressing modes for big-endian X-registers
2949
2950    fn xload16be_u32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2951        let result = unsafe { self.load_ne::<u16, crate::XLoad16BeU32O32>(addr)? };
2952        self.state[dst].set_u32(u16::from_be(result).into());
2953        ControlFlow::Continue(())
2954    }
2955
2956    fn xload16be_s32_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2957        let result = unsafe { self.load_ne::<i16, crate::XLoad16BeS32O32>(addr)? };
2958        self.state[dst].set_i32(i16::from_be(result).into());
2959        ControlFlow::Continue(())
2960    }
2961
2962    fn xload32be_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2963        let result = unsafe { self.load_ne::<i32, crate::XLoad32BeO32>(addr)? };
2964        self.state[dst].set_i32(i32::from_be(result));
2965        ControlFlow::Continue(())
2966    }
2967
2968    fn xload64be_o32(&mut self, dst: XReg, addr: AddrO32) -> ControlFlow<Done> {
2969        let result = unsafe { self.load_ne::<i64, crate::XLoad64BeO32>(addr)? };
2970        self.state[dst].set_i64(i64::from_be(result));
2971        ControlFlow::Continue(())
2972    }
2973
2974    fn xstore16be_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2975        let val = self.state[val].get_u32() as u16;
2976        unsafe {
2977            self.store_ne::<u16, crate::XStore16BeO32>(addr, val.to_be())?;
2978        }
2979        ControlFlow::Continue(())
2980    }
2981
2982    fn xstore32be_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2983        let val = self.state[val].get_u32();
2984        unsafe {
2985            self.store_ne::<u32, crate::XStore32BeO32>(addr, val.to_be())?;
2986        }
2987        ControlFlow::Continue(())
2988    }
2989
2990    fn xstore64be_o32(&mut self, addr: AddrO32, val: XReg) -> ControlFlow<Done> {
2991        let val = self.state[val].get_u64();
2992        unsafe {
2993            self.store_ne::<u64, crate::XStore64BeO32>(addr, val.to_be())?;
2994        }
2995        ControlFlow::Continue(())
2996    }
2997
2998    // =========================================================================
2999    // o32 addressing modes for little-endian F-registers
3000
3001    fn fload32le_o32(&mut self, dst: FReg, addr: AddrO32) -> ControlFlow<Done> {
3002        let val = unsafe { self.load_ne::<u32, crate::Fload32LeO32>(addr)? };
3003        self.state[dst].set_f32(f32::from_bits(u32::from_le(val)));
3004        ControlFlow::Continue(())
3005    }
3006
3007    fn fload64le_o32(&mut self, dst: FReg, addr: AddrO32) -> ControlFlow<Done> {
3008        let val = unsafe { self.load_ne::<u64, crate::Fload64LeO32>(addr)? };
3009        self.state[dst].set_f64(f64::from_bits(u64::from_le(val)));
3010        ControlFlow::Continue(())
3011    }
3012
3013    fn fstore32le_o32(&mut self, addr: AddrO32, src: FReg) -> ControlFlow<Done> {
3014        let val = self.state[src].get_f32();
3015        unsafe {
3016            self.store_ne::<u32, crate::Fstore32LeO32>(addr, val.to_bits().to_le())?;
3017        }
3018        ControlFlow::Continue(())
3019    }
3020
3021    fn fstore64le_o32(&mut self, addr: AddrO32, src: FReg) -> ControlFlow<Done> {
3022        let val = self.state[src].get_f64();
3023        unsafe {
3024            self.store_ne::<u64, crate::Fstore64LeO32>(addr, val.to_bits().to_le())?;
3025        }
3026        ControlFlow::Continue(())
3027    }
3028
3029    // =========================================================================
3030    // o32 addressing modes for big-endian F-registers
3031
3032    fn fload32be_o32(&mut self, dst: FReg, addr: AddrO32) -> ControlFlow<Done> {
3033        let val = unsafe { self.load_ne::<u32, crate::Fload32BeO32>(addr)? };
3034        self.state[dst].set_f32(f32::from_bits(u32::from_be(val)));
3035        ControlFlow::Continue(())
3036    }
3037
3038    fn fload64be_o32(&mut self, dst: FReg, addr: AddrO32) -> ControlFlow<Done> {
3039        let val = unsafe { self.load_ne::<u64, crate::Fload64BeO32>(addr)? };
3040        self.state[dst].set_f64(f64::from_bits(u64::from_be(val)));
3041        ControlFlow::Continue(())
3042    }
3043
3044    fn fstore32be_o32(&mut self, addr: AddrO32, src: FReg) -> ControlFlow<Done> {
3045        let val = self.state[src].get_f32();
3046        unsafe {
3047            self.store_ne::<u32, crate::Fstore32BeO32>(addr, val.to_bits().to_be())?;
3048        }
3049        ControlFlow::Continue(())
3050    }
3051
3052    fn fstore64be_o32(&mut self, addr: AddrO32, src: FReg) -> ControlFlow<Done> {
3053        let val = self.state[src].get_f64();
3054        unsafe {
3055            self.store_ne::<u64, crate::Fstore64BeO32>(addr, val.to_bits().to_be())?;
3056        }
3057        ControlFlow::Continue(())
3058    }
3059
3060    // =========================================================================
3061    // z addressing modes for little-endian F-registers
3062
3063    fn fload32le_z(&mut self, dst: FReg, addr: AddrZ) -> ControlFlow<Done> {
3064        let val = unsafe { self.load_ne::<u32, crate::Fload32LeZ>(addr)? };
3065        self.state[dst].set_f32(f32::from_bits(u32::from_le(val)));
3066        ControlFlow::Continue(())
3067    }
3068
3069    fn fload64le_z(&mut self, dst: FReg, addr: AddrZ) -> ControlFlow<Done> {
3070        let val = unsafe { self.load_ne::<u64, crate::Fload64LeZ>(addr)? };
3071        self.state[dst].set_f64(f64::from_bits(u64::from_le(val)));
3072        ControlFlow::Continue(())
3073    }
3074
3075    fn fstore32le_z(&mut self, addr: AddrZ, src: FReg) -> ControlFlow<Done> {
3076        let val = self.state[src].get_f32();
3077        unsafe {
3078            self.store_ne::<u32, crate::Fstore32LeZ>(addr, val.to_bits().to_le())?;
3079        }
3080        ControlFlow::Continue(())
3081    }
3082
3083    fn fstore64le_z(&mut self, addr: AddrZ, src: FReg) -> ControlFlow<Done> {
3084        let val = self.state[src].get_f64();
3085        unsafe {
3086            self.store_ne::<u64, crate::Fstore64LeZ>(addr, val.to_bits().to_le())?;
3087        }
3088        ControlFlow::Continue(())
3089    }
3090
3091    // =========================================================================
3092    // g32 addressing modes for little-endian F-registers
3093
3094    fn fload32le_g32(&mut self, dst: FReg, addr: AddrG32) -> ControlFlow<Done> {
3095        let val = unsafe { self.load_ne::<u32, crate::Fload32LeG32>(addr)? };
3096        self.state[dst].set_f32(f32::from_bits(u32::from_le(val)));
3097        ControlFlow::Continue(())
3098    }
3099
3100    fn fload64le_g32(&mut self, dst: FReg, addr: AddrG32) -> ControlFlow<Done> {
3101        let val = unsafe { self.load_ne::<u64, crate::Fload64LeG32>(addr)? };
3102        self.state[dst].set_f64(f64::from_bits(u64::from_le(val)));
3103        ControlFlow::Continue(())
3104    }
3105
3106    fn fstore32le_g32(&mut self, addr: AddrG32, src: FReg) -> ControlFlow<Done> {
3107        let val = self.state[src].get_f32();
3108        unsafe {
3109            self.store_ne::<u32, crate::Fstore32LeG32>(addr, val.to_bits().to_le())?;
3110        }
3111        ControlFlow::Continue(())
3112    }
3113
3114    fn fstore64le_g32(&mut self, addr: AddrG32, src: FReg) -> ControlFlow<Done> {
3115        let val = self.state[src].get_f64();
3116        unsafe {
3117            self.store_ne::<u64, crate::Fstore64LeG32>(addr, val.to_bits().to_le())?;
3118        }
3119        ControlFlow::Continue(())
3120    }
3121
3122    // =========================================================================
3123    // o32 addressing modes for little-endian V-registers
3124
3125    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3126    fn vload128le_o32(&mut self, dst: VReg, addr: AddrO32) -> ControlFlow<Done> {
3127        let val = unsafe { self.load_ne::<u128, crate::VLoad128O32>(addr)? };
3128        self.state[dst].set_u128(u128::from_le(val));
3129        ControlFlow::Continue(())
3130    }
3131
3132    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3133    fn vstore128le_o32(&mut self, addr: AddrO32, src: VReg) -> ControlFlow<Done> {
3134        let val = self.state[src].get_u128();
3135        unsafe {
3136            self.store_ne::<u128, crate::Vstore128LeO32>(addr, val.to_le())?;
3137        }
3138        ControlFlow::Continue(())
3139    }
3140
3141    // =========================================================================
3142    // z addressing modes for little-endian V-registers
3143
3144    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3145    fn vload128le_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
3146        let val = unsafe { self.load_ne::<u128, crate::VLoad128LeZ>(addr)? };
3147        self.state[dst].set_u128(u128::from_le(val));
3148        ControlFlow::Continue(())
3149    }
3150
3151    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3152    fn vstore128le_z(&mut self, addr: AddrZ, src: VReg) -> ControlFlow<Done> {
3153        let val = self.state[src].get_u128();
3154        unsafe {
3155            self.store_ne::<u128, crate::Vstore128LeZ>(addr, val.to_le())?;
3156        }
3157        ControlFlow::Continue(())
3158    }
3159
3160    // =========================================================================
3161    // g32 addressing modes for little-endian V-registers
3162
3163    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3164    fn vload128le_g32(&mut self, dst: VReg, addr: AddrG32) -> ControlFlow<Done> {
3165        let val = unsafe { self.load_ne::<u128, crate::VLoad128G32>(addr)? };
3166        self.state[dst].set_u128(u128::from_le(val));
3167        ControlFlow::Continue(())
3168    }
3169
3170    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3171    fn vstore128le_g32(&mut self, addr: AddrG32, src: VReg) -> ControlFlow<Done> {
3172        let val = self.state[src].get_u128();
3173        unsafe {
3174            self.store_ne::<u128, crate::Vstore128LeG32>(addr, val.to_le())?;
3175        }
3176        ControlFlow::Continue(())
3177    }
3178
3179    fn xmov_fp(&mut self, dst: XReg) -> ControlFlow<Done> {
3180        let fp = self.state.fp;
3181        self.state[dst].set_ptr(fp);
3182        ControlFlow::Continue(())
3183    }
3184
3185    fn xmov_lr(&mut self, dst: XReg) -> ControlFlow<Done> {
3186        let lr = self.state.lr;
3187        self.state[dst].set_ptr(lr);
3188        ControlFlow::Continue(())
3189    }
3190
3191    fn fmov(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3192        let val = self.state[src];
3193        self.state[dst] = val;
3194        ControlFlow::Continue(())
3195    }
3196
3197    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3198    fn vmov(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3199        let val = self.state[src];
3200        self.state[dst] = val;
3201        ControlFlow::Continue(())
3202    }
3203
3204    fn fconst32(&mut self, dst: FReg, bits: u32) -> ControlFlow<Done> {
3205        self.state[dst].set_f32(f32::from_bits(bits));
3206        ControlFlow::Continue(())
3207    }
3208
3209    fn fconst64(&mut self, dst: FReg, bits: u64) -> ControlFlow<Done> {
3210        self.state[dst].set_f64(f64::from_bits(bits));
3211        ControlFlow::Continue(())
3212    }
3213
3214    fn bitcast_int_from_float_32(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3215        let val = self.state[src].get_f32();
3216        self.state[dst].set_u32(val.to_bits());
3217        ControlFlow::Continue(())
3218    }
3219
3220    fn bitcast_int_from_float_64(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3221        let val = self.state[src].get_f64();
3222        self.state[dst].set_u64(val.to_bits());
3223        ControlFlow::Continue(())
3224    }
3225
3226    fn bitcast_float_from_int_32(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3227        let val = self.state[src].get_u32();
3228        self.state[dst].set_f32(f32::from_bits(val));
3229        ControlFlow::Continue(())
3230    }
3231
3232    fn bitcast_float_from_int_64(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3233        let val = self.state[src].get_u64();
3234        self.state[dst].set_f64(f64::from_bits(val));
3235        ControlFlow::Continue(())
3236    }
3237
3238    fn feq32(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3239        let a = self.state[src1].get_f32();
3240        let b = self.state[src2].get_f32();
3241        self.state[dst].set_u32(u32::from(a == b));
3242        ControlFlow::Continue(())
3243    }
3244
3245    fn fneq32(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3246        let a = self.state[src1].get_f32();
3247        let b = self.state[src2].get_f32();
3248        self.state[dst].set_u32(u32::from(a != b));
3249        ControlFlow::Continue(())
3250    }
3251
3252    fn flt32(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3253        let a = self.state[src1].get_f32();
3254        let b = self.state[src2].get_f32();
3255        self.state[dst].set_u32(u32::from(a < b));
3256        ControlFlow::Continue(())
3257    }
3258
3259    fn flteq32(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3260        let a = self.state[src1].get_f32();
3261        let b = self.state[src2].get_f32();
3262        self.state[dst].set_u32(u32::from(a <= b));
3263        ControlFlow::Continue(())
3264    }
3265
3266    fn feq64(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3267        let a = self.state[src1].get_f64();
3268        let b = self.state[src2].get_f64();
3269        self.state[dst].set_u32(u32::from(a == b));
3270        ControlFlow::Continue(())
3271    }
3272
3273    fn fneq64(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3274        let a = self.state[src1].get_f64();
3275        let b = self.state[src2].get_f64();
3276        self.state[dst].set_u32(u32::from(a != b));
3277        ControlFlow::Continue(())
3278    }
3279
3280    fn flt64(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3281        let a = self.state[src1].get_f64();
3282        let b = self.state[src2].get_f64();
3283        self.state[dst].set_u32(u32::from(a < b));
3284        ControlFlow::Continue(())
3285    }
3286
3287    fn flteq64(&mut self, dst: XReg, src1: FReg, src2: FReg) -> ControlFlow<Done> {
3288        let a = self.state[src1].get_f64();
3289        let b = self.state[src2].get_f64();
3290        self.state[dst].set_u32(u32::from(a <= b));
3291        ControlFlow::Continue(())
3292    }
3293
3294    fn fselect32(
3295        &mut self,
3296        dst: FReg,
3297        cond: XReg,
3298        if_nonzero: FReg,
3299        if_zero: FReg,
3300    ) -> ControlFlow<Done> {
3301        let result = if self.state[cond].get_u32() != 0 {
3302            self.state[if_nonzero].get_f32()
3303        } else {
3304            self.state[if_zero].get_f32()
3305        };
3306        self.state[dst].set_f32(result);
3307        ControlFlow::Continue(())
3308    }
3309
3310    fn fselect64(
3311        &mut self,
3312        dst: FReg,
3313        cond: XReg,
3314        if_nonzero: FReg,
3315        if_zero: FReg,
3316    ) -> ControlFlow<Done> {
3317        let result = if self.state[cond].get_u32() != 0 {
3318            self.state[if_nonzero].get_f64()
3319        } else {
3320            self.state[if_zero].get_f64()
3321        };
3322        self.state[dst].set_f64(result);
3323        ControlFlow::Continue(())
3324    }
3325
3326    fn f32_from_x32_s(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3327        let a = self.state[src].get_i32();
3328        self.state[dst].set_f32(a as f32);
3329        ControlFlow::Continue(())
3330    }
3331
3332    fn f32_from_x32_u(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3333        let a = self.state[src].get_u32();
3334        self.state[dst].set_f32(a as f32);
3335        ControlFlow::Continue(())
3336    }
3337
3338    fn f32_from_x64_s(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3339        let a = self.state[src].get_i64();
3340        self.state[dst].set_f32(a as f32);
3341        ControlFlow::Continue(())
3342    }
3343
3344    fn f32_from_x64_u(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3345        let a = self.state[src].get_u64();
3346        self.state[dst].set_f32(a as f32);
3347        ControlFlow::Continue(())
3348    }
3349
3350    fn f64_from_x32_s(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3351        let a = self.state[src].get_i32();
3352        self.state[dst].set_f64(a as f64);
3353        ControlFlow::Continue(())
3354    }
3355
3356    fn f64_from_x32_u(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3357        let a = self.state[src].get_u32();
3358        self.state[dst].set_f64(a as f64);
3359        ControlFlow::Continue(())
3360    }
3361
3362    fn f64_from_x64_s(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3363        let a = self.state[src].get_i64();
3364        self.state[dst].set_f64(a as f64);
3365        ControlFlow::Continue(())
3366    }
3367
3368    fn f64_from_x64_u(&mut self, dst: FReg, src: XReg) -> ControlFlow<Done> {
3369        let a = self.state[src].get_u64();
3370        self.state[dst].set_f64(a as f64);
3371        ControlFlow::Continue(())
3372    }
3373
3374    fn x32_from_f32_s(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3375        let a = self.state[src].get_f32();
3376        self.check_xnn_from_f32::<crate::X32FromF32S>(a, f32_cvt_to_int_bounds(true, 32))?;
3377        self.state[dst].set_i32(a as i32);
3378        ControlFlow::Continue(())
3379    }
3380
3381    fn x32_from_f32_u(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3382        let a = self.state[src].get_f32();
3383        self.check_xnn_from_f32::<crate::X32FromF32U>(a, f32_cvt_to_int_bounds(false, 32))?;
3384        self.state[dst].set_u32(a as u32);
3385        ControlFlow::Continue(())
3386    }
3387
3388    fn x64_from_f32_s(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3389        let a = self.state[src].get_f32();
3390        self.check_xnn_from_f32::<crate::X64FromF32S>(a, f32_cvt_to_int_bounds(true, 64))?;
3391        self.state[dst].set_i64(a as i64);
3392        ControlFlow::Continue(())
3393    }
3394
3395    fn x64_from_f32_u(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3396        let a = self.state[src].get_f32();
3397        self.check_xnn_from_f32::<crate::X64FromF32U>(a, f32_cvt_to_int_bounds(false, 64))?;
3398        self.state[dst].set_u64(a as u64);
3399        ControlFlow::Continue(())
3400    }
3401
3402    fn x32_from_f64_s(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3403        let a = self.state[src].get_f64();
3404        self.check_xnn_from_f64::<crate::X32FromF64S>(a, f64_cvt_to_int_bounds(true, 32))?;
3405        self.state[dst].set_i32(a as i32);
3406        ControlFlow::Continue(())
3407    }
3408
3409    fn x32_from_f64_u(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3410        let a = self.state[src].get_f64();
3411        self.check_xnn_from_f64::<crate::X32FromF64U>(a, f64_cvt_to_int_bounds(false, 32))?;
3412        self.state[dst].set_u32(a as u32);
3413        ControlFlow::Continue(())
3414    }
3415
3416    fn x64_from_f64_s(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3417        let a = self.state[src].get_f64();
3418        self.check_xnn_from_f64::<crate::X64FromF64S>(a, f64_cvt_to_int_bounds(true, 64))?;
3419        self.state[dst].set_i64(a as i64);
3420        ControlFlow::Continue(())
3421    }
3422
3423    fn x64_from_f64_u(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3424        let a = self.state[src].get_f64();
3425        self.check_xnn_from_f64::<crate::X64FromF64U>(a, f64_cvt_to_int_bounds(false, 64))?;
3426        self.state[dst].set_u64(a as u64);
3427        ControlFlow::Continue(())
3428    }
3429
3430    fn x32_from_f32_s_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3431        let a = self.state[src].get_f32();
3432        self.state[dst].set_i32(a as i32);
3433        ControlFlow::Continue(())
3434    }
3435
3436    fn x32_from_f32_u_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3437        let a = self.state[src].get_f32();
3438        self.state[dst].set_u32(a as u32);
3439        ControlFlow::Continue(())
3440    }
3441
3442    fn x64_from_f32_s_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3443        let a = self.state[src].get_f32();
3444        self.state[dst].set_i64(a as i64);
3445        ControlFlow::Continue(())
3446    }
3447
3448    fn x64_from_f32_u_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3449        let a = self.state[src].get_f32();
3450        self.state[dst].set_u64(a as u64);
3451        ControlFlow::Continue(())
3452    }
3453
3454    fn x32_from_f64_s_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3455        let a = self.state[src].get_f64();
3456        self.state[dst].set_i32(a as i32);
3457        ControlFlow::Continue(())
3458    }
3459
3460    fn x32_from_f64_u_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3461        let a = self.state[src].get_f64();
3462        self.state[dst].set_u32(a as u32);
3463        ControlFlow::Continue(())
3464    }
3465
3466    fn x64_from_f64_s_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3467        let a = self.state[src].get_f64();
3468        self.state[dst].set_i64(a as i64);
3469        ControlFlow::Continue(())
3470    }
3471
3472    fn x64_from_f64_u_sat(&mut self, dst: XReg, src: FReg) -> ControlFlow<Done> {
3473        let a = self.state[src].get_f64();
3474        self.state[dst].set_u64(a as u64);
3475        ControlFlow::Continue(())
3476    }
3477
3478    fn f32_from_f64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3479        let a = self.state[src].get_f64();
3480        self.state[dst].set_f32(a as f32);
3481        ControlFlow::Continue(())
3482    }
3483
3484    fn f64_from_f32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3485        let a = self.state[src].get_f32();
3486        self.state[dst].set_f64(a.into());
3487        ControlFlow::Continue(())
3488    }
3489
3490    fn fcopysign32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3491        let a = self.state[operands.src1].get_f32();
3492        let b = self.state[operands.src2].get_f32();
3493        self.state[operands.dst].set_f32(a.wasm_copysign(b));
3494        ControlFlow::Continue(())
3495    }
3496
3497    fn fcopysign64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3498        let a = self.state[operands.src1].get_f64();
3499        let b = self.state[operands.src2].get_f64();
3500        self.state[operands.dst].set_f64(a.wasm_copysign(b));
3501        ControlFlow::Continue(())
3502    }
3503
3504    fn fadd32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3505        let a = self.state[operands.src1].get_f32();
3506        let b = self.state[operands.src2].get_f32();
3507        self.state[operands.dst].set_f32(a + b);
3508        ControlFlow::Continue(())
3509    }
3510
3511    fn fsub32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3512        let a = self.state[operands.src1].get_f32();
3513        let b = self.state[operands.src2].get_f32();
3514        self.state[operands.dst].set_f32(a - b);
3515        ControlFlow::Continue(())
3516    }
3517
3518    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3519    fn vsubf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3520        let mut a = self.state[operands.src1].get_f32x4();
3521        let b = self.state[operands.src2].get_f32x4();
3522        for (a, b) in a.iter_mut().zip(b) {
3523            *a = *a - b;
3524        }
3525        self.state[operands.dst].set_f32x4(a);
3526        ControlFlow::Continue(())
3527    }
3528
3529    fn fmul32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3530        let a = self.state[operands.src1].get_f32();
3531        let b = self.state[operands.src2].get_f32();
3532        self.state[operands.dst].set_f32(a * b);
3533        ControlFlow::Continue(())
3534    }
3535
3536    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3537    fn vmulf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3538        let mut a = self.state[operands.src1].get_f32x4();
3539        let b = self.state[operands.src2].get_f32x4();
3540        for (a, b) in a.iter_mut().zip(b) {
3541            *a = *a * b;
3542        }
3543        self.state[operands.dst].set_f32x4(a);
3544        ControlFlow::Continue(())
3545    }
3546
3547    fn fdiv32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3548        let a = self.state[operands.src1].get_f32();
3549        let b = self.state[operands.src2].get_f32();
3550        self.state[operands.dst].set_f32(a / b);
3551        ControlFlow::Continue(())
3552    }
3553
3554    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3555    fn vdivf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3556        let a = self.state[operands.src1].get_f32x4();
3557        let b = self.state[operands.src2].get_f32x4();
3558        let mut result = [0.0f32; 4];
3559
3560        for i in 0..4 {
3561            result[i] = a[i] / b[i];
3562        }
3563
3564        self.state[operands.dst].set_f32x4(result);
3565        ControlFlow::Continue(())
3566    }
3567
3568    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3569    fn vdivf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3570        let a = self.state[operands.src1].get_f64x2();
3571        let b = self.state[operands.src2].get_f64x2();
3572        let mut result = [0.0f64; 2];
3573
3574        for i in 0..2 {
3575            result[i] = a[i] / b[i];
3576        }
3577
3578        self.state[operands.dst].set_f64x2(result);
3579        ControlFlow::Continue(())
3580    }
3581
3582    fn fmaximum32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3583        let a = self.state[operands.src1].get_f32();
3584        let b = self.state[operands.src2].get_f32();
3585        self.state[operands.dst].set_f32(a.wasm_maximum(b));
3586        ControlFlow::Continue(())
3587    }
3588
3589    fn fminimum32(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3590        let a = self.state[operands.src1].get_f32();
3591        let b = self.state[operands.src2].get_f32();
3592        self.state[operands.dst].set_f32(a.wasm_minimum(b));
3593        ControlFlow::Continue(())
3594    }
3595
3596    fn ftrunc32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3597        let a = self.state[src].get_f32();
3598        self.state[dst].set_f32(a.wasm_trunc());
3599        ControlFlow::Continue(())
3600    }
3601
3602    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3603    fn vtrunc32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3604        let mut a = self.state[src].get_f32x4();
3605        for elem in a.iter_mut() {
3606            *elem = elem.wasm_trunc();
3607        }
3608        self.state[dst].set_f32x4(a);
3609        ControlFlow::Continue(())
3610    }
3611
3612    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3613    fn vtrunc64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3614        let mut a = self.state[src].get_f64x2();
3615        for elem in a.iter_mut() {
3616            *elem = elem.wasm_trunc();
3617        }
3618        self.state[dst].set_f64x2(a);
3619        ControlFlow::Continue(())
3620    }
3621
3622    fn ffloor32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3623        let a = self.state[src].get_f32();
3624        self.state[dst].set_f32(a.wasm_floor());
3625        ControlFlow::Continue(())
3626    }
3627
3628    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3629    fn vfloor32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3630        let mut a = self.state[src].get_f32x4();
3631        for elem in a.iter_mut() {
3632            *elem = elem.wasm_floor();
3633        }
3634        self.state[dst].set_f32x4(a);
3635        ControlFlow::Continue(())
3636    }
3637
3638    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3639    fn vfloor64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3640        let mut a = self.state[src].get_f64x2();
3641        for elem in a.iter_mut() {
3642            *elem = elem.wasm_floor();
3643        }
3644        self.state[dst].set_f64x2(a);
3645        ControlFlow::Continue(())
3646    }
3647
3648    fn fceil32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3649        let a = self.state[src].get_f32();
3650        self.state[dst].set_f32(a.wasm_ceil());
3651        ControlFlow::Continue(())
3652    }
3653
3654    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3655    fn vceil32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3656        let mut a = self.state[src].get_f32x4();
3657        for elem in a.iter_mut() {
3658            *elem = elem.wasm_ceil();
3659        }
3660        self.state[dst].set_f32x4(a);
3661
3662        ControlFlow::Continue(())
3663    }
3664
3665    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3666    fn vceil64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3667        let mut a = self.state[src].get_f64x2();
3668        for elem in a.iter_mut() {
3669            *elem = elem.wasm_ceil();
3670        }
3671        self.state[dst].set_f64x2(a);
3672
3673        ControlFlow::Continue(())
3674    }
3675
3676    fn fnearest32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3677        let a = self.state[src].get_f32();
3678        self.state[dst].set_f32(a.wasm_nearest());
3679        ControlFlow::Continue(())
3680    }
3681
3682    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3683    fn vnearest32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3684        let mut a = self.state[src].get_f32x4();
3685        for elem in a.iter_mut() {
3686            *elem = elem.wasm_nearest();
3687        }
3688        self.state[dst].set_f32x4(a);
3689        ControlFlow::Continue(())
3690    }
3691
3692    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3693    fn vnearest64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3694        let mut a = self.state[src].get_f64x2();
3695        for elem in a.iter_mut() {
3696            *elem = elem.wasm_nearest();
3697        }
3698        self.state[dst].set_f64x2(a);
3699        ControlFlow::Continue(())
3700    }
3701
3702    fn fsqrt32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3703        let a = self.state[src].get_f32();
3704        self.state[dst].set_f32(a.wasm_sqrt());
3705        ControlFlow::Continue(())
3706    }
3707
3708    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3709    fn vsqrt32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3710        let mut a = self.state[src].get_f32x4();
3711        for elem in a.iter_mut() {
3712            *elem = elem.wasm_sqrt();
3713        }
3714        self.state[dst].set_f32x4(a);
3715        ControlFlow::Continue(())
3716    }
3717
3718    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3719    fn vsqrt64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3720        let mut a = self.state[src].get_f64x2();
3721        for elem in a.iter_mut() {
3722            *elem = elem.wasm_sqrt();
3723        }
3724        self.state[dst].set_f64x2(a);
3725        ControlFlow::Continue(())
3726    }
3727
3728    fn fneg32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3729        let a = self.state[src].get_f32();
3730        self.state[dst].set_f32(-a);
3731        ControlFlow::Continue(())
3732    }
3733
3734    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3735    fn vnegf32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
3736        let mut a = self.state[src].get_f32x4();
3737        for elem in a.iter_mut() {
3738            *elem = -*elem;
3739        }
3740        self.state[dst].set_f32x4(a);
3741        ControlFlow::Continue(())
3742    }
3743
3744    fn fabs32(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3745        let a = self.state[src].get_f32();
3746        self.state[dst].set_f32(a.wasm_abs());
3747        ControlFlow::Continue(())
3748    }
3749
3750    fn fadd64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3751        let a = self.state[operands.src1].get_f64();
3752        let b = self.state[operands.src2].get_f64();
3753        self.state[operands.dst].set_f64(a + b);
3754        ControlFlow::Continue(())
3755    }
3756
3757    fn fsub64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3758        let a = self.state[operands.src1].get_f64();
3759        let b = self.state[operands.src2].get_f64();
3760        self.state[operands.dst].set_f64(a - b);
3761        ControlFlow::Continue(())
3762    }
3763
3764    fn fmul64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3765        let a = self.state[operands.src1].get_f64();
3766        let b = self.state[operands.src2].get_f64();
3767        self.state[operands.dst].set_f64(a * b);
3768        ControlFlow::Continue(())
3769    }
3770
3771    fn fdiv64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3772        let a = self.state[operands.src1].get_f64();
3773        let b = self.state[operands.src2].get_f64();
3774        self.state[operands.dst].set_f64(a / b);
3775        ControlFlow::Continue(())
3776    }
3777
3778    fn fmaximum64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3779        let a = self.state[operands.src1].get_f64();
3780        let b = self.state[operands.src2].get_f64();
3781        self.state[operands.dst].set_f64(a.wasm_maximum(b));
3782        ControlFlow::Continue(())
3783    }
3784
3785    fn fminimum64(&mut self, operands: BinaryOperands<FReg>) -> ControlFlow<Done> {
3786        let a = self.state[operands.src1].get_f64();
3787        let b = self.state[operands.src2].get_f64();
3788        self.state[operands.dst].set_f64(a.wasm_minimum(b));
3789        ControlFlow::Continue(())
3790    }
3791
3792    fn ftrunc64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3793        let a = self.state[src].get_f64();
3794        self.state[dst].set_f64(a.wasm_trunc());
3795        ControlFlow::Continue(())
3796    }
3797
3798    fn ffloor64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3799        let a = self.state[src].get_f64();
3800        self.state[dst].set_f64(a.wasm_floor());
3801        ControlFlow::Continue(())
3802    }
3803
3804    fn fceil64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3805        let a = self.state[src].get_f64();
3806        self.state[dst].set_f64(a.wasm_ceil());
3807        ControlFlow::Continue(())
3808    }
3809
3810    fn fnearest64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3811        let a = self.state[src].get_f64();
3812        self.state[dst].set_f64(a.wasm_nearest());
3813        ControlFlow::Continue(())
3814    }
3815
3816    fn fsqrt64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3817        let a = self.state[src].get_f64();
3818        self.state[dst].set_f64(a.wasm_sqrt());
3819        ControlFlow::Continue(())
3820    }
3821
3822    fn fneg64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3823        let a = self.state[src].get_f64();
3824        self.state[dst].set_f64(-a);
3825        ControlFlow::Continue(())
3826    }
3827
3828    fn fabs64(&mut self, dst: FReg, src: FReg) -> ControlFlow<Done> {
3829        let a = self.state[src].get_f64();
3830        self.state[dst].set_f64(a.wasm_abs());
3831        ControlFlow::Continue(())
3832    }
3833
3834    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3835    fn vaddi8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3836        let mut a = self.state[operands.src1].get_i8x16();
3837        let b = self.state[operands.src2].get_i8x16();
3838        for (a, b) in a.iter_mut().zip(b) {
3839            *a = a.wrapping_add(b);
3840        }
3841        self.state[operands.dst].set_i8x16(a);
3842        ControlFlow::Continue(())
3843    }
3844
3845    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3846    fn vaddi16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3847        let mut a = self.state[operands.src1].get_i16x8();
3848        let b = self.state[operands.src2].get_i16x8();
3849        for (a, b) in a.iter_mut().zip(b) {
3850            *a = a.wrapping_add(b);
3851        }
3852        self.state[operands.dst].set_i16x8(a);
3853        ControlFlow::Continue(())
3854    }
3855
3856    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3857    fn vaddi32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3858        let mut a = self.state[operands.src1].get_i32x4();
3859        let b = self.state[operands.src2].get_i32x4();
3860        for (a, b) in a.iter_mut().zip(b) {
3861            *a = a.wrapping_add(b);
3862        }
3863        self.state[operands.dst].set_i32x4(a);
3864        ControlFlow::Continue(())
3865    }
3866
3867    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3868    fn vaddi64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3869        let mut a = self.state[operands.src1].get_i64x2();
3870        let b = self.state[operands.src2].get_i64x2();
3871        for (a, b) in a.iter_mut().zip(b) {
3872            *a = a.wrapping_add(b);
3873        }
3874        self.state[operands.dst].set_i64x2(a);
3875        ControlFlow::Continue(())
3876    }
3877
3878    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3879    fn vaddf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3880        let mut a = self.state[operands.src1].get_f32x4();
3881        let b = self.state[operands.src2].get_f32x4();
3882        for (a, b) in a.iter_mut().zip(b) {
3883            *a += b;
3884        }
3885        self.state[operands.dst].set_f32x4(a);
3886        ControlFlow::Continue(())
3887    }
3888
3889    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3890    fn vaddf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3891        let mut a = self.state[operands.src1].get_f64x2();
3892        let b = self.state[operands.src2].get_f64x2();
3893        for (a, b) in a.iter_mut().zip(b) {
3894            *a += b;
3895        }
3896        self.state[operands.dst].set_f64x2(a);
3897        ControlFlow::Continue(())
3898    }
3899
3900    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3901    fn vaddi8x16_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3902        let mut a = self.state[operands.src1].get_i8x16();
3903        let b = self.state[operands.src2].get_i8x16();
3904        for (a, b) in a.iter_mut().zip(b) {
3905            *a = (*a).saturating_add(b);
3906        }
3907        self.state[operands.dst].set_i8x16(a);
3908        ControlFlow::Continue(())
3909    }
3910
3911    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3912    fn vaddu8x16_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3913        let mut a = self.state[operands.src1].get_u8x16();
3914        let b = self.state[operands.src2].get_u8x16();
3915        for (a, b) in a.iter_mut().zip(b) {
3916            *a = (*a).saturating_add(b);
3917        }
3918        self.state[operands.dst].set_u8x16(a);
3919        ControlFlow::Continue(())
3920    }
3921
3922    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3923    fn vaddi16x8_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3924        let mut a = self.state[operands.src1].get_i16x8();
3925        let b = self.state[operands.src2].get_i16x8();
3926        for (a, b) in a.iter_mut().zip(b) {
3927            *a = (*a).saturating_add(b);
3928        }
3929        self.state[operands.dst].set_i16x8(a);
3930        ControlFlow::Continue(())
3931    }
3932
3933    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3934    fn vaddu16x8_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3935        let mut a = self.state[operands.src1].get_u16x8();
3936        let b = self.state[operands.src2].get_u16x8();
3937        for (a, b) in a.iter_mut().zip(b) {
3938            *a = (*a).saturating_add(b);
3939        }
3940        self.state[operands.dst].set_u16x8(a);
3941        ControlFlow::Continue(())
3942    }
3943
3944    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3945    fn vaddpairwisei16x8_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3946        let a = self.state[operands.src1].get_i16x8();
3947        let b = self.state[operands.src2].get_i16x8();
3948        let mut result = [0i16; 8];
3949        let half = result.len() / 2;
3950        for i in 0..half {
3951            result[i] = a[2 * i].wrapping_add(a[2 * i + 1]);
3952            result[i + half] = b[2 * i].wrapping_add(b[2 * i + 1]);
3953        }
3954        self.state[operands.dst].set_i16x8(result);
3955        ControlFlow::Continue(())
3956    }
3957
3958    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3959    fn vaddpairwisei32x4_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
3960        let a = self.state[operands.src1].get_i32x4();
3961        let b = self.state[operands.src2].get_i32x4();
3962        let mut result = [0i32; 4];
3963        result[0] = a[0].wrapping_add(a[1]);
3964        result[1] = a[2].wrapping_add(a[3]);
3965        result[2] = b[0].wrapping_add(b[1]);
3966        result[3] = b[2].wrapping_add(b[3]);
3967        self.state[operands.dst].set_i32x4(result);
3968        ControlFlow::Continue(())
3969    }
3970
3971    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3972    fn vshli8x16(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
3973        let a = self.state[operands.src1].get_i8x16();
3974        let b = self.state[operands.src2].get_u32();
3975        self.state[operands.dst].set_i8x16(a.map(|a| a.wrapping_shl(b)));
3976        ControlFlow::Continue(())
3977    }
3978
3979    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3980    fn vshli16x8(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
3981        let a = self.state[operands.src1].get_i16x8();
3982        let b = self.state[operands.src2].get_u32();
3983        self.state[operands.dst].set_i16x8(a.map(|a| a.wrapping_shl(b)));
3984        ControlFlow::Continue(())
3985    }
3986
3987    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3988    fn vshli32x4(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
3989        let a = self.state[operands.src1].get_i32x4();
3990        let b = self.state[operands.src2].get_u32();
3991        self.state[operands.dst].set_i32x4(a.map(|a| a.wrapping_shl(b)));
3992        ControlFlow::Continue(())
3993    }
3994
3995    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
3996    fn vshli64x2(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
3997        let a = self.state[operands.src1].get_i64x2();
3998        let b = self.state[operands.src2].get_u32();
3999        self.state[operands.dst].set_i64x2(a.map(|a| a.wrapping_shl(b)));
4000        ControlFlow::Continue(())
4001    }
4002
4003    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4004    fn vshri8x16_s(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4005        let a = self.state[operands.src1].get_i8x16();
4006        let b = self.state[operands.src2].get_u32();
4007        self.state[operands.dst].set_i8x16(a.map(|a| a.wrapping_shr(b)));
4008        ControlFlow::Continue(())
4009    }
4010
4011    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4012    fn vshri16x8_s(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4013        let a = self.state[operands.src1].get_i16x8();
4014        let b = self.state[operands.src2].get_u32();
4015        self.state[operands.dst].set_i16x8(a.map(|a| a.wrapping_shr(b)));
4016        ControlFlow::Continue(())
4017    }
4018
4019    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4020    fn vshri32x4_s(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4021        let a = self.state[operands.src1].get_i32x4();
4022        let b = self.state[operands.src2].get_u32();
4023        self.state[operands.dst].set_i32x4(a.map(|a| a.wrapping_shr(b)));
4024        ControlFlow::Continue(())
4025    }
4026
4027    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4028    fn vshri64x2_s(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4029        let a = self.state[operands.src1].get_i64x2();
4030        let b = self.state[operands.src2].get_u32();
4031        self.state[operands.dst].set_i64x2(a.map(|a| a.wrapping_shr(b)));
4032        ControlFlow::Continue(())
4033    }
4034
4035    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4036    fn vshri8x16_u(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4037        let a = self.state[operands.src1].get_u8x16();
4038        let b = self.state[operands.src2].get_u32();
4039        self.state[operands.dst].set_u8x16(a.map(|a| a.wrapping_shr(b)));
4040        ControlFlow::Continue(())
4041    }
4042
4043    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4044    fn vshri16x8_u(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4045        let a = self.state[operands.src1].get_u16x8();
4046        let b = self.state[operands.src2].get_u32();
4047        self.state[operands.dst].set_u16x8(a.map(|a| a.wrapping_shr(b)));
4048        ControlFlow::Continue(())
4049    }
4050
4051    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4052    fn vshri32x4_u(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4053        let a = self.state[operands.src1].get_u32x4();
4054        let b = self.state[operands.src2].get_u32();
4055        self.state[operands.dst].set_u32x4(a.map(|a| a.wrapping_shr(b)));
4056        ControlFlow::Continue(())
4057    }
4058
4059    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4060    fn vshri64x2_u(&mut self, operands: BinaryOperands<VReg, VReg, XReg>) -> ControlFlow<Done> {
4061        let a = self.state[operands.src1].get_u64x2();
4062        let b = self.state[operands.src2].get_u32();
4063        self.state[operands.dst].set_u64x2(a.map(|a| a.wrapping_shr(b)));
4064        ControlFlow::Continue(())
4065    }
4066
4067    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4068    fn vconst128(&mut self, dst: VReg, val: u128) -> ControlFlow<Done> {
4069        self.state[dst].set_u128(val);
4070        ControlFlow::Continue(())
4071    }
4072
4073    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4074    fn vsplatx8(&mut self, dst: VReg, src: XReg) -> ControlFlow<Done> {
4075        let val = self.state[src].get_u32() as u8;
4076        self.state[dst].set_u8x16([val; 16]);
4077        ControlFlow::Continue(())
4078    }
4079
4080    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4081    fn vsplatx16(&mut self, dst: VReg, src: XReg) -> ControlFlow<Done> {
4082        let val = self.state[src].get_u32() as u16;
4083        self.state[dst].set_u16x8([val; 8]);
4084        ControlFlow::Continue(())
4085    }
4086
4087    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4088    fn vsplatx32(&mut self, dst: VReg, src: XReg) -> ControlFlow<Done> {
4089        let val = self.state[src].get_u32();
4090        self.state[dst].set_u32x4([val; 4]);
4091        ControlFlow::Continue(())
4092    }
4093
4094    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4095    fn vsplatx64(&mut self, dst: VReg, src: XReg) -> ControlFlow<Done> {
4096        let val = self.state[src].get_u64();
4097        self.state[dst].set_u64x2([val; 2]);
4098        ControlFlow::Continue(())
4099    }
4100
4101    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4102    fn vsplatf32(&mut self, dst: VReg, src: FReg) -> ControlFlow<Done> {
4103        let val = self.state[src].get_f32();
4104        self.state[dst].set_f32x4([val; 4]);
4105        ControlFlow::Continue(())
4106    }
4107
4108    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4109    fn vsplatf64(&mut self, dst: VReg, src: FReg) -> ControlFlow<Done> {
4110        let val = self.state[src].get_f64();
4111        self.state[dst].set_f64x2([val; 2]);
4112        ControlFlow::Continue(())
4113    }
4114
4115    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4116    fn vload8x8_s_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4117        let val = unsafe { self.load_ne::<[i8; 8], crate::VLoad8x8SZ>(addr)? };
4118        self.state[dst].set_i16x8(val.map(|i| i.into()));
4119        ControlFlow::Continue(())
4120    }
4121
4122    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4123    fn vload8x8_u_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4124        let val = unsafe { self.load_ne::<[u8; 8], crate::VLoad8x8UZ>(addr)? };
4125        self.state[dst].set_u16x8(val.map(|i| i.into()));
4126        ControlFlow::Continue(())
4127    }
4128
4129    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4130    fn vload16x4le_s_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4131        let val = unsafe { self.load_ne::<[i16; 4], crate::VLoad16x4LeSZ>(addr)? };
4132        self.state[dst].set_i32x4(val.map(|i| i16::from_le(i).into()));
4133        ControlFlow::Continue(())
4134    }
4135
4136    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4137    fn vload16x4le_u_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4138        let val = unsafe { self.load_ne::<[u16; 4], crate::VLoad16x4LeUZ>(addr)? };
4139        self.state[dst].set_u32x4(val.map(|i| u16::from_le(i).into()));
4140        ControlFlow::Continue(())
4141    }
4142
4143    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4144    fn vload32x2le_s_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4145        let val = unsafe { self.load_ne::<[i32; 2], crate::VLoad32x2LeSZ>(addr)? };
4146        self.state[dst].set_i64x2(val.map(|i| i32::from_le(i).into()));
4147        ControlFlow::Continue(())
4148    }
4149
4150    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4151    fn vload32x2le_u_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
4152        let val = unsafe { self.load_ne::<[u32; 2], crate::VLoad32x2LeUZ>(addr)? };
4153        self.state[dst].set_u64x2(val.map(|i| u32::from_le(i).into()));
4154        ControlFlow::Continue(())
4155    }
4156
4157    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4158    fn vband128(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4159        let a = self.state[operands.src1].get_u128();
4160        let b = self.state[operands.src2].get_u128();
4161        self.state[operands.dst].set_u128(a & b);
4162        ControlFlow::Continue(())
4163    }
4164
4165    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4166    fn vbor128(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4167        let a = self.state[operands.src1].get_u128();
4168        let b = self.state[operands.src2].get_u128();
4169        self.state[operands.dst].set_u128(a | b);
4170        ControlFlow::Continue(())
4171    }
4172
4173    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4174    fn vbxor128(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4175        let a = self.state[operands.src1].get_u128();
4176        let b = self.state[operands.src2].get_u128();
4177        self.state[operands.dst].set_u128(a ^ b);
4178        ControlFlow::Continue(())
4179    }
4180
4181    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4182    fn vbnot128(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4183        let a = self.state[src].get_u128();
4184        self.state[dst].set_u128(!a);
4185        ControlFlow::Continue(())
4186    }
4187
4188    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4189    fn vbitselect128(&mut self, dst: VReg, c: VReg, x: VReg, y: VReg) -> ControlFlow<Done> {
4190        let c = self.state[c].get_u128();
4191        let x = self.state[x].get_u128();
4192        let y = self.state[y].get_u128();
4193        self.state[dst].set_u128((c & x) | (!c & y));
4194        ControlFlow::Continue(())
4195    }
4196
4197    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4198    fn vbitmask8x16(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4199        let a = self.state[src].get_u8x16();
4200        let mut result = 0;
4201        for item in a.iter().rev() {
4202            result <<= 1;
4203            result |= (*item >> 7) as u32;
4204        }
4205        self.state[dst].set_u32(result);
4206        ControlFlow::Continue(())
4207    }
4208
4209    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4210    fn vbitmask16x8(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4211        let a = self.state[src].get_u16x8();
4212        let mut result = 0;
4213        for item in a.iter().rev() {
4214            result <<= 1;
4215            result |= (*item >> 15) as u32;
4216        }
4217        self.state[dst].set_u32(result);
4218        ControlFlow::Continue(())
4219    }
4220
4221    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4222    fn vbitmask32x4(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4223        let a = self.state[src].get_u32x4();
4224        let mut result = 0;
4225        for item in a.iter().rev() {
4226            result <<= 1;
4227            result |= *item >> 31;
4228        }
4229        self.state[dst].set_u32(result);
4230        ControlFlow::Continue(())
4231    }
4232
4233    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4234    fn vbitmask64x2(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4235        let a = self.state[src].get_u64x2();
4236        let mut result = 0;
4237        for item in a.iter().rev() {
4238            result <<= 1;
4239            result |= (*item >> 63) as u32;
4240        }
4241        self.state[dst].set_u32(result);
4242        ControlFlow::Continue(())
4243    }
4244
4245    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4246    fn valltrue8x16(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4247        let a = self.state[src].get_u8x16();
4248        let result = a.iter().all(|a| *a != 0);
4249        self.state[dst].set_u32(u32::from(result));
4250        ControlFlow::Continue(())
4251    }
4252
4253    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4254    fn valltrue16x8(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4255        let a = self.state[src].get_u16x8();
4256        let result = a.iter().all(|a| *a != 0);
4257        self.state[dst].set_u32(u32::from(result));
4258        ControlFlow::Continue(())
4259    }
4260
4261    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4262    fn valltrue32x4(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4263        let a = self.state[src].get_u32x4();
4264        let result = a.iter().all(|a| *a != 0);
4265        self.state[dst].set_u32(u32::from(result));
4266        ControlFlow::Continue(())
4267    }
4268
4269    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4270    fn valltrue64x2(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4271        let a = self.state[src].get_u64x2();
4272        let result = a.iter().all(|a| *a != 0);
4273        self.state[dst].set_u32(u32::from(result));
4274        ControlFlow::Continue(())
4275    }
4276
4277    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4278    fn vanytrue8x16(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4279        let a = self.state[src].get_u8x16();
4280        let result = a.iter().any(|a| *a != 0);
4281        self.state[dst].set_u32(u32::from(result));
4282        ControlFlow::Continue(())
4283    }
4284
4285    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4286    fn vanytrue16x8(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4287        let a = self.state[src].get_u16x8();
4288        let result = a.iter().any(|a| *a != 0);
4289        self.state[dst].set_u32(u32::from(result));
4290        ControlFlow::Continue(())
4291    }
4292
4293    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4294    fn vanytrue32x4(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4295        let a = self.state[src].get_u32x4();
4296        let result = a.iter().any(|a| *a != 0);
4297        self.state[dst].set_u32(u32::from(result));
4298        ControlFlow::Continue(())
4299    }
4300
4301    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4302    fn vanytrue64x2(&mut self, dst: XReg, src: VReg) -> ControlFlow<Done> {
4303        let a = self.state[src].get_u64x2();
4304        let result = a.iter().any(|a| *a != 0);
4305        self.state[dst].set_u32(u32::from(result));
4306        ControlFlow::Continue(())
4307    }
4308
4309    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4310    fn vf32x4_from_i32x4_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4311        let a = self.state[src].get_i32x4();
4312        self.state[dst].set_f32x4(a.map(|i| i as f32));
4313        ControlFlow::Continue(())
4314    }
4315
4316    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4317    fn vf32x4_from_i32x4_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4318        let a = self.state[src].get_u32x4();
4319        self.state[dst].set_f32x4(a.map(|i| i as f32));
4320        ControlFlow::Continue(())
4321    }
4322
4323    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4324    fn vf64x2_from_i64x2_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4325        let a = self.state[src].get_i64x2();
4326        self.state[dst].set_f64x2(a.map(|i| i as f64));
4327        ControlFlow::Continue(())
4328    }
4329
4330    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4331    fn vf64x2_from_i64x2_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4332        let a = self.state[src].get_u64x2();
4333        self.state[dst].set_f64x2(a.map(|i| i as f64));
4334        ControlFlow::Continue(())
4335    }
4336
4337    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4338    fn vi32x4_from_f32x4_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4339        let a = self.state[src].get_f32x4();
4340        self.state[dst].set_i32x4(a.map(|f| f as i32));
4341        ControlFlow::Continue(())
4342    }
4343
4344    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4345    fn vi32x4_from_f32x4_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4346        let a = self.state[src].get_f32x4();
4347        self.state[dst].set_u32x4(a.map(|f| f as u32));
4348        ControlFlow::Continue(())
4349    }
4350
4351    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4352    fn vi64x2_from_f64x2_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4353        let a = self.state[src].get_f64x2();
4354        self.state[dst].set_i64x2(a.map(|f| f as i64));
4355        ControlFlow::Continue(())
4356    }
4357
4358    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4359    fn vi64x2_from_f64x2_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4360        let a = self.state[src].get_f64x2();
4361        self.state[dst].set_u64x2(a.map(|f| f as u64));
4362        ControlFlow::Continue(())
4363    }
4364
4365    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4366    fn vwidenlow8x16_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4367        let a = *self.state[src].get_i8x16().first_chunk().unwrap();
4368        self.state[dst].set_i16x8(a.map(|i| i.into()));
4369        ControlFlow::Continue(())
4370    }
4371
4372    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4373    fn vwidenlow8x16_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4374        let a = *self.state[src].get_u8x16().first_chunk().unwrap();
4375        self.state[dst].set_u16x8(a.map(|i| i.into()));
4376        ControlFlow::Continue(())
4377    }
4378
4379    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4380    fn vwidenlow16x8_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4381        let a = *self.state[src].get_i16x8().first_chunk().unwrap();
4382        self.state[dst].set_i32x4(a.map(|i| i.into()));
4383        ControlFlow::Continue(())
4384    }
4385
4386    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4387    fn vwidenlow16x8_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4388        let a = *self.state[src].get_u16x8().first_chunk().unwrap();
4389        self.state[dst].set_u32x4(a.map(|i| i.into()));
4390        ControlFlow::Continue(())
4391    }
4392
4393    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4394    fn vwidenlow32x4_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4395        let a = *self.state[src].get_i32x4().first_chunk().unwrap();
4396        self.state[dst].set_i64x2(a.map(|i| i.into()));
4397        ControlFlow::Continue(())
4398    }
4399
4400    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4401    fn vwidenlow32x4_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4402        let a = *self.state[src].get_u32x4().first_chunk().unwrap();
4403        self.state[dst].set_u64x2(a.map(|i| i.into()));
4404        ControlFlow::Continue(())
4405    }
4406
4407    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4408    fn vwidenhigh8x16_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4409        let a = *self.state[src].get_i8x16().last_chunk().unwrap();
4410        self.state[dst].set_i16x8(a.map(|i| i.into()));
4411        ControlFlow::Continue(())
4412    }
4413
4414    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4415    fn vwidenhigh8x16_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4416        let a = *self.state[src].get_u8x16().last_chunk().unwrap();
4417        self.state[dst].set_u16x8(a.map(|i| i.into()));
4418        ControlFlow::Continue(())
4419    }
4420
4421    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4422    fn vwidenhigh16x8_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4423        let a = *self.state[src].get_i16x8().last_chunk().unwrap();
4424        self.state[dst].set_i32x4(a.map(|i| i.into()));
4425        ControlFlow::Continue(())
4426    }
4427
4428    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4429    fn vwidenhigh16x8_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4430        let a = *self.state[src].get_u16x8().last_chunk().unwrap();
4431        self.state[dst].set_u32x4(a.map(|i| i.into()));
4432        ControlFlow::Continue(())
4433    }
4434
4435    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4436    fn vwidenhigh32x4_s(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4437        let a = *self.state[src].get_i32x4().last_chunk().unwrap();
4438        self.state[dst].set_i64x2(a.map(|i| i.into()));
4439        ControlFlow::Continue(())
4440    }
4441
4442    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4443    fn vwidenhigh32x4_u(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4444        let a = *self.state[src].get_u32x4().last_chunk().unwrap();
4445        self.state[dst].set_u64x2(a.map(|i| i.into()));
4446        ControlFlow::Continue(())
4447    }
4448
4449    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4450    fn vnarrow16x8_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4451        let a = self.state[operands.src1].get_i16x8();
4452        let b = self.state[operands.src2].get_i16x8();
4453        let mut result = [0; 16];
4454        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4455            *d = (*i)
4456                .try_into()
4457                .unwrap_or(if *i < 0 { i8::MIN } else { i8::MAX });
4458        }
4459        self.state[operands.dst].set_i8x16(result);
4460        ControlFlow::Continue(())
4461    }
4462
4463    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4464    fn vnarrow16x8_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4465        let a = self.state[operands.src1].get_i16x8();
4466        let b = self.state[operands.src2].get_i16x8();
4467        let mut result = [0; 16];
4468        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4469            *d = (*i)
4470                .try_into()
4471                .unwrap_or(if *i < 0 { u8::MIN } else { u8::MAX });
4472        }
4473        self.state[operands.dst].set_u8x16(result);
4474        ControlFlow::Continue(())
4475    }
4476
4477    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4478    fn vnarrow32x4_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4479        let a = self.state[operands.src1].get_i32x4();
4480        let b = self.state[operands.src2].get_i32x4();
4481        let mut result = [0; 8];
4482        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4483            *d = (*i)
4484                .try_into()
4485                .unwrap_or(if *i < 0 { i16::MIN } else { i16::MAX });
4486        }
4487        self.state[operands.dst].set_i16x8(result);
4488        ControlFlow::Continue(())
4489    }
4490
4491    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4492    fn vnarrow32x4_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4493        let a = self.state[operands.src1].get_i32x4();
4494        let b = self.state[operands.src2].get_i32x4();
4495        let mut result = [0; 8];
4496        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4497            *d = (*i)
4498                .try_into()
4499                .unwrap_or(if *i < 0 { u16::MIN } else { u16::MAX });
4500        }
4501        self.state[operands.dst].set_u16x8(result);
4502        ControlFlow::Continue(())
4503    }
4504
4505    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4506    fn vnarrow64x2_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4507        let a = self.state[operands.src1].get_i64x2();
4508        let b = self.state[operands.src2].get_i64x2();
4509        let mut result = [0; 4];
4510        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4511            *d = (*i)
4512                .try_into()
4513                .unwrap_or(if *i < 0 { i32::MIN } else { i32::MAX });
4514        }
4515        self.state[operands.dst].set_i32x4(result);
4516        ControlFlow::Continue(())
4517    }
4518
4519    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4520    fn vnarrow64x2_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4521        let a = self.state[operands.src1].get_i64x2();
4522        let b = self.state[operands.src2].get_i64x2();
4523        let mut result = [0; 4];
4524        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4525            *d = (*i)
4526                .try_into()
4527                .unwrap_or(if *i < 0 { u32::MIN } else { u32::MAX });
4528        }
4529        self.state[operands.dst].set_u32x4(result);
4530        ControlFlow::Continue(())
4531    }
4532
4533    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4534    fn vunarrow64x2_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4535        let a = self.state[operands.src1].get_u64x2();
4536        let b = self.state[operands.src2].get_u64x2();
4537        let mut result = [0; 4];
4538        for (i, d) in a.iter().chain(&b).zip(&mut result) {
4539            *d = (*i).try_into().unwrap_or(u32::MAX);
4540        }
4541        self.state[operands.dst].set_u32x4(result);
4542        ControlFlow::Continue(())
4543    }
4544
4545    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4546    fn vfpromotelow(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4547        let a = self.state[src].get_f32x4();
4548        self.state[dst].set_f64x2([a[0].into(), a[1].into()]);
4549        ControlFlow::Continue(())
4550    }
4551
4552    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4553    fn vfdemote(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4554        let a = self.state[src].get_f64x2();
4555        self.state[dst].set_f32x4([a[0] as f32, a[1] as f32, 0.0, 0.0]);
4556        ControlFlow::Continue(())
4557    }
4558
4559    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4560    fn vsubi8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4561        let mut a = self.state[operands.src1].get_i8x16();
4562        let b = self.state[operands.src2].get_i8x16();
4563        for (a, b) in a.iter_mut().zip(b) {
4564            *a = a.wrapping_sub(b);
4565        }
4566        self.state[operands.dst].set_i8x16(a);
4567        ControlFlow::Continue(())
4568    }
4569
4570    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4571    fn vsubi16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4572        let mut a = self.state[operands.src1].get_i16x8();
4573        let b = self.state[operands.src2].get_i16x8();
4574        for (a, b) in a.iter_mut().zip(b) {
4575            *a = a.wrapping_sub(b);
4576        }
4577        self.state[operands.dst].set_i16x8(a);
4578        ControlFlow::Continue(())
4579    }
4580
4581    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4582    fn vsubi32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4583        let mut a = self.state[operands.src1].get_i32x4();
4584        let b = self.state[operands.src2].get_i32x4();
4585        for (a, b) in a.iter_mut().zip(b) {
4586            *a = a.wrapping_sub(b);
4587        }
4588        self.state[operands.dst].set_i32x4(a);
4589        ControlFlow::Continue(())
4590    }
4591
4592    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4593    fn vsubi64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4594        let mut a = self.state[operands.src1].get_i64x2();
4595        let b = self.state[operands.src2].get_i64x2();
4596        for (a, b) in a.iter_mut().zip(b) {
4597            *a = a.wrapping_sub(b);
4598        }
4599        self.state[operands.dst].set_i64x2(a);
4600        ControlFlow::Continue(())
4601    }
4602
4603    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4604    fn vsubi8x16_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4605        let mut a = self.state[operands.src1].get_i8x16();
4606        let b = self.state[operands.src2].get_i8x16();
4607        for (a, b) in a.iter_mut().zip(b) {
4608            *a = a.saturating_sub(b);
4609        }
4610        self.state[operands.dst].set_i8x16(a);
4611        ControlFlow::Continue(())
4612    }
4613
4614    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4615    fn vsubu8x16_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4616        let mut a = self.state[operands.src1].get_u8x16();
4617        let b = self.state[operands.src2].get_u8x16();
4618        for (a, b) in a.iter_mut().zip(b) {
4619            *a = a.saturating_sub(b);
4620        }
4621        self.state[operands.dst].set_u8x16(a);
4622        ControlFlow::Continue(())
4623    }
4624
4625    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4626    fn vsubi16x8_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4627        let mut a = self.state[operands.src1].get_i16x8();
4628        let b = self.state[operands.src2].get_i16x8();
4629        for (a, b) in a.iter_mut().zip(b) {
4630            *a = a.saturating_sub(b);
4631        }
4632        self.state[operands.dst].set_i16x8(a);
4633        ControlFlow::Continue(())
4634    }
4635
4636    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4637    fn vsubu16x8_sat(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4638        let mut a = self.state[operands.src1].get_u16x8();
4639        let b = self.state[operands.src2].get_u16x8();
4640        for (a, b) in a.iter_mut().zip(b) {
4641            *a = a.saturating_sub(b);
4642        }
4643        self.state[operands.dst].set_u16x8(a);
4644        ControlFlow::Continue(())
4645    }
4646
4647    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4648    fn vsubf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4649        let mut a = self.state[operands.src1].get_f64x2();
4650        let b = self.state[operands.src2].get_f64x2();
4651        for (a, b) in a.iter_mut().zip(b) {
4652            *a = *a - b;
4653        }
4654        self.state[operands.dst].set_f64x2(a);
4655        ControlFlow::Continue(())
4656    }
4657
4658    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4659    fn vmuli8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4660        let mut a = self.state[operands.src1].get_i8x16();
4661        let b = self.state[operands.src2].get_i8x16();
4662        for (a, b) in a.iter_mut().zip(b) {
4663            *a = a.wrapping_mul(b);
4664        }
4665        self.state[operands.dst].set_i8x16(a);
4666        ControlFlow::Continue(())
4667    }
4668
4669    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4670    fn vmuli16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4671        let mut a = self.state[operands.src1].get_i16x8();
4672        let b = self.state[operands.src2].get_i16x8();
4673        for (a, b) in a.iter_mut().zip(b) {
4674            *a = a.wrapping_mul(b);
4675        }
4676        self.state[operands.dst].set_i16x8(a);
4677        ControlFlow::Continue(())
4678    }
4679
4680    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4681    fn vmuli32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4682        let mut a = self.state[operands.src1].get_i32x4();
4683        let b = self.state[operands.src2].get_i32x4();
4684        for (a, b) in a.iter_mut().zip(b) {
4685            *a = a.wrapping_mul(b);
4686        }
4687        self.state[operands.dst].set_i32x4(a);
4688        ControlFlow::Continue(())
4689    }
4690
4691    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4692    fn vmuli64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4693        let mut a = self.state[operands.src1].get_i64x2();
4694        let b = self.state[operands.src2].get_i64x2();
4695        for (a, b) in a.iter_mut().zip(b) {
4696            *a = a.wrapping_mul(b);
4697        }
4698        self.state[operands.dst].set_i64x2(a);
4699        ControlFlow::Continue(())
4700    }
4701
4702    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4703    fn vmulf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4704        let mut a = self.state[operands.src1].get_f64x2();
4705        let b = self.state[operands.src2].get_f64x2();
4706        for (a, b) in a.iter_mut().zip(b) {
4707            *a = *a * b;
4708        }
4709        self.state[operands.dst].set_f64x2(a);
4710        ControlFlow::Continue(())
4711    }
4712
4713    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4714    fn vqmulrsi16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4715        let mut a = self.state[operands.src1].get_i16x8();
4716        let b = self.state[operands.src2].get_i16x8();
4717        const MIN: i32 = i16::MIN as i32;
4718        const MAX: i32 = i16::MAX as i32;
4719        for (a, b) in a.iter_mut().zip(b) {
4720            let r = (i32::from(*a) * i32::from(b) + (1 << 14)) >> 15;
4721            *a = r.clamp(MIN, MAX) as i16;
4722        }
4723        self.state[operands.dst].set_i16x8(a);
4724        ControlFlow::Continue(())
4725    }
4726
4727    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4728    fn vpopcnt8x16(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
4729        let a = self.state[src].get_u8x16();
4730        self.state[dst].set_u8x16(a.map(|i| i.count_ones() as u8));
4731        ControlFlow::Continue(())
4732    }
4733
4734    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4735    fn xextractv8x16(&mut self, dst: XReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4736        let a = unsafe { *self.state[src].get_u8x16().get_unchecked(usize::from(lane)) };
4737        self.state[dst].set_u32(u32::from(a));
4738        ControlFlow::Continue(())
4739    }
4740
4741    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4742    fn xextractv16x8(&mut self, dst: XReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4743        let a = unsafe { *self.state[src].get_u16x8().get_unchecked(usize::from(lane)) };
4744        self.state[dst].set_u32(u32::from(a));
4745        ControlFlow::Continue(())
4746    }
4747
4748    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4749    fn xextractv32x4(&mut self, dst: XReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4750        let a = unsafe { *self.state[src].get_u32x4().get_unchecked(usize::from(lane)) };
4751        self.state[dst].set_u32(a);
4752        ControlFlow::Continue(())
4753    }
4754
4755    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4756    fn xextractv64x2(&mut self, dst: XReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4757        let a = unsafe { *self.state[src].get_u64x2().get_unchecked(usize::from(lane)) };
4758        self.state[dst].set_u64(a);
4759        ControlFlow::Continue(())
4760    }
4761
4762    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4763    fn fextractv32x4(&mut self, dst: FReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4764        let a = unsafe { *self.state[src].get_f32x4().get_unchecked(usize::from(lane)) };
4765        self.state[dst].set_f32(a);
4766        ControlFlow::Continue(())
4767    }
4768
4769    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4770    fn fextractv64x2(&mut self, dst: FReg, src: VReg, lane: u8) -> ControlFlow<Done> {
4771        let a = unsafe { *self.state[src].get_f64x2().get_unchecked(usize::from(lane)) };
4772        self.state[dst].set_f64(a);
4773        ControlFlow::Continue(())
4774    }
4775
4776    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4777    fn vinsertx8(
4778        &mut self,
4779        operands: BinaryOperands<VReg, VReg, XReg>,
4780        lane: u8,
4781    ) -> ControlFlow<Done> {
4782        let mut a = self.state[operands.src1].get_u8x16();
4783        let b = self.state[operands.src2].get_u32() as u8;
4784        unsafe {
4785            *a.get_unchecked_mut(usize::from(lane)) = b;
4786        }
4787        self.state[operands.dst].set_u8x16(a);
4788        ControlFlow::Continue(())
4789    }
4790
4791    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4792    fn vinsertx16(
4793        &mut self,
4794        operands: BinaryOperands<VReg, VReg, XReg>,
4795        lane: u8,
4796    ) -> ControlFlow<Done> {
4797        let mut a = self.state[operands.src1].get_u16x8();
4798        let b = self.state[operands.src2].get_u32() as u16;
4799        unsafe {
4800            *a.get_unchecked_mut(usize::from(lane)) = b;
4801        }
4802        self.state[operands.dst].set_u16x8(a);
4803        ControlFlow::Continue(())
4804    }
4805
4806    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4807    fn vinsertx32(
4808        &mut self,
4809        operands: BinaryOperands<VReg, VReg, XReg>,
4810        lane: u8,
4811    ) -> ControlFlow<Done> {
4812        let mut a = self.state[operands.src1].get_u32x4();
4813        let b = self.state[operands.src2].get_u32();
4814        unsafe {
4815            *a.get_unchecked_mut(usize::from(lane)) = b;
4816        }
4817        self.state[operands.dst].set_u32x4(a);
4818        ControlFlow::Continue(())
4819    }
4820
4821    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4822    fn vinsertx64(
4823        &mut self,
4824        operands: BinaryOperands<VReg, VReg, XReg>,
4825        lane: u8,
4826    ) -> ControlFlow<Done> {
4827        let mut a = self.state[operands.src1].get_u64x2();
4828        let b = self.state[operands.src2].get_u64();
4829        unsafe {
4830            *a.get_unchecked_mut(usize::from(lane)) = b;
4831        }
4832        self.state[operands.dst].set_u64x2(a);
4833        ControlFlow::Continue(())
4834    }
4835
4836    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4837    fn vinsertf32(
4838        &mut self,
4839        operands: BinaryOperands<VReg, VReg, FReg>,
4840        lane: u8,
4841    ) -> ControlFlow<Done> {
4842        let mut a = self.state[operands.src1].get_f32x4();
4843        let b = self.state[operands.src2].get_f32();
4844        unsafe {
4845            *a.get_unchecked_mut(usize::from(lane)) = b;
4846        }
4847        self.state[operands.dst].set_f32x4(a);
4848        ControlFlow::Continue(())
4849    }
4850
4851    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4852    fn vinsertf64(
4853        &mut self,
4854        operands: BinaryOperands<VReg, VReg, FReg>,
4855        lane: u8,
4856    ) -> ControlFlow<Done> {
4857        let mut a = self.state[operands.src1].get_f64x2();
4858        let b = self.state[operands.src2].get_f64();
4859        unsafe {
4860            *a.get_unchecked_mut(usize::from(lane)) = b;
4861        }
4862        self.state[operands.dst].set_f64x2(a);
4863        ControlFlow::Continue(())
4864    }
4865
4866    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4867    fn veq8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4868        let a = self.state[operands.src1].get_u8x16();
4869        let b = self.state[operands.src2].get_u8x16();
4870        let mut c = [0; 16];
4871        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4872            *c = if a == b { u8::MAX } else { 0 };
4873        }
4874        self.state[operands.dst].set_u8x16(c);
4875        ControlFlow::Continue(())
4876    }
4877
4878    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4879    fn vneq8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4880        let a = self.state[operands.src1].get_u8x16();
4881        let b = self.state[operands.src2].get_u8x16();
4882        let mut c = [0; 16];
4883        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4884            *c = if a != b { u8::MAX } else { 0 };
4885        }
4886        self.state[operands.dst].set_u8x16(c);
4887        ControlFlow::Continue(())
4888    }
4889
4890    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4891    fn vslt8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4892        let a = self.state[operands.src1].get_i8x16();
4893        let b = self.state[operands.src2].get_i8x16();
4894        let mut c = [0; 16];
4895        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4896            *c = if a < b { u8::MAX } else { 0 };
4897        }
4898        self.state[operands.dst].set_u8x16(c);
4899        ControlFlow::Continue(())
4900    }
4901
4902    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4903    fn vslteq8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4904        let a = self.state[operands.src1].get_i8x16();
4905        let b = self.state[operands.src2].get_i8x16();
4906        let mut c = [0; 16];
4907        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4908            *c = if a <= b { u8::MAX } else { 0 };
4909        }
4910        self.state[operands.dst].set_u8x16(c);
4911        ControlFlow::Continue(())
4912    }
4913
4914    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4915    fn vult8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4916        let a = self.state[operands.src1].get_u8x16();
4917        let b = self.state[operands.src2].get_u8x16();
4918        let mut c = [0; 16];
4919        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4920            *c = if a < b { u8::MAX } else { 0 };
4921        }
4922        self.state[operands.dst].set_u8x16(c);
4923        ControlFlow::Continue(())
4924    }
4925
4926    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4927    fn vulteq8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4928        let a = self.state[operands.src1].get_u8x16();
4929        let b = self.state[operands.src2].get_u8x16();
4930        let mut c = [0; 16];
4931        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4932            *c = if a <= b { u8::MAX } else { 0 };
4933        }
4934        self.state[operands.dst].set_u8x16(c);
4935        ControlFlow::Continue(())
4936    }
4937
4938    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4939    fn veq16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4940        let a = self.state[operands.src1].get_u16x8();
4941        let b = self.state[operands.src2].get_u16x8();
4942        let mut c = [0; 8];
4943        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4944            *c = if a == b { u16::MAX } else { 0 };
4945        }
4946        self.state[operands.dst].set_u16x8(c);
4947        ControlFlow::Continue(())
4948    }
4949
4950    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4951    fn vneq16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4952        let a = self.state[operands.src1].get_u16x8();
4953        let b = self.state[operands.src2].get_u16x8();
4954        let mut c = [0; 8];
4955        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4956            *c = if a != b { u16::MAX } else { 0 };
4957        }
4958        self.state[operands.dst].set_u16x8(c);
4959        ControlFlow::Continue(())
4960    }
4961
4962    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4963    fn vslt16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4964        let a = self.state[operands.src1].get_i16x8();
4965        let b = self.state[operands.src2].get_i16x8();
4966        let mut c = [0; 8];
4967        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4968            *c = if a < b { u16::MAX } else { 0 };
4969        }
4970        self.state[operands.dst].set_u16x8(c);
4971        ControlFlow::Continue(())
4972    }
4973
4974    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4975    fn vslteq16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4976        let a = self.state[operands.src1].get_i16x8();
4977        let b = self.state[operands.src2].get_i16x8();
4978        let mut c = [0; 8];
4979        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4980            *c = if a <= b { u16::MAX } else { 0 };
4981        }
4982        self.state[operands.dst].set_u16x8(c);
4983        ControlFlow::Continue(())
4984    }
4985
4986    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4987    fn vult16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
4988        let a = self.state[operands.src1].get_u16x8();
4989        let b = self.state[operands.src2].get_u16x8();
4990        let mut c = [0; 8];
4991        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
4992            *c = if a < b { u16::MAX } else { 0 };
4993        }
4994        self.state[operands.dst].set_u16x8(c);
4995        ControlFlow::Continue(())
4996    }
4997
4998    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
4999    fn vulteq16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5000        let a = self.state[operands.src1].get_u16x8();
5001        let b = self.state[operands.src2].get_u16x8();
5002        let mut c = [0; 8];
5003        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5004            *c = if a <= b { u16::MAX } else { 0 };
5005        }
5006        self.state[operands.dst].set_u16x8(c);
5007        ControlFlow::Continue(())
5008    }
5009
5010    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5011    fn veq32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5012        let a = self.state[operands.src1].get_u32x4();
5013        let b = self.state[operands.src2].get_u32x4();
5014        let mut c = [0; 4];
5015        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5016            *c = if a == b { u32::MAX } else { 0 };
5017        }
5018        self.state[operands.dst].set_u32x4(c);
5019        ControlFlow::Continue(())
5020    }
5021
5022    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5023    fn vneq32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5024        let a = self.state[operands.src1].get_u32x4();
5025        let b = self.state[operands.src2].get_u32x4();
5026        let mut c = [0; 4];
5027        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5028            *c = if a != b { u32::MAX } else { 0 };
5029        }
5030        self.state[operands.dst].set_u32x4(c);
5031        ControlFlow::Continue(())
5032    }
5033
5034    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5035    fn vslt32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5036        let a = self.state[operands.src1].get_i32x4();
5037        let b = self.state[operands.src2].get_i32x4();
5038        let mut c = [0; 4];
5039        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5040            *c = if a < b { u32::MAX } else { 0 };
5041        }
5042        self.state[operands.dst].set_u32x4(c);
5043        ControlFlow::Continue(())
5044    }
5045
5046    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5047    fn vslteq32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5048        let a = self.state[operands.src1].get_i32x4();
5049        let b = self.state[operands.src2].get_i32x4();
5050        let mut c = [0; 4];
5051        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5052            *c = if a <= b { u32::MAX } else { 0 };
5053        }
5054        self.state[operands.dst].set_u32x4(c);
5055        ControlFlow::Continue(())
5056    }
5057
5058    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5059    fn vult32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5060        let a = self.state[operands.src1].get_u32x4();
5061        let b = self.state[operands.src2].get_u32x4();
5062        let mut c = [0; 4];
5063        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5064            *c = if a < b { u32::MAX } else { 0 };
5065        }
5066        self.state[operands.dst].set_u32x4(c);
5067        ControlFlow::Continue(())
5068    }
5069
5070    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5071    fn vulteq32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5072        let a = self.state[operands.src1].get_u32x4();
5073        let b = self.state[operands.src2].get_u32x4();
5074        let mut c = [0; 4];
5075        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5076            *c = if a <= b { u32::MAX } else { 0 };
5077        }
5078        self.state[operands.dst].set_u32x4(c);
5079        ControlFlow::Continue(())
5080    }
5081
5082    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5083    fn veq64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5084        let a = self.state[operands.src1].get_u64x2();
5085        let b = self.state[operands.src2].get_u64x2();
5086        let mut c = [0; 2];
5087        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5088            *c = if a == b { u64::MAX } else { 0 };
5089        }
5090        self.state[operands.dst].set_u64x2(c);
5091        ControlFlow::Continue(())
5092    }
5093
5094    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5095    fn vneq64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5096        let a = self.state[operands.src1].get_u64x2();
5097        let b = self.state[operands.src2].get_u64x2();
5098        let mut c = [0; 2];
5099        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5100            *c = if a != b { u64::MAX } else { 0 };
5101        }
5102        self.state[operands.dst].set_u64x2(c);
5103        ControlFlow::Continue(())
5104    }
5105
5106    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5107    fn vslt64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5108        let a = self.state[operands.src1].get_i64x2();
5109        let b = self.state[operands.src2].get_i64x2();
5110        let mut c = [0; 2];
5111        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5112            *c = if a < b { u64::MAX } else { 0 };
5113        }
5114        self.state[operands.dst].set_u64x2(c);
5115        ControlFlow::Continue(())
5116    }
5117
5118    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5119    fn vslteq64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5120        let a = self.state[operands.src1].get_i64x2();
5121        let b = self.state[operands.src2].get_i64x2();
5122        let mut c = [0; 2];
5123        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5124            *c = if a <= b { u64::MAX } else { 0 };
5125        }
5126        self.state[operands.dst].set_u64x2(c);
5127        ControlFlow::Continue(())
5128    }
5129
5130    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5131    fn vult64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5132        let a = self.state[operands.src1].get_u64x2();
5133        let b = self.state[operands.src2].get_u64x2();
5134        let mut c = [0; 2];
5135        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5136            *c = if a < b { u64::MAX } else { 0 };
5137        }
5138        self.state[operands.dst].set_u64x2(c);
5139        ControlFlow::Continue(())
5140    }
5141
5142    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5143    fn vulteq64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5144        let a = self.state[operands.src1].get_u64x2();
5145        let b = self.state[operands.src2].get_u64x2();
5146        let mut c = [0; 2];
5147        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5148            *c = if a <= b { u64::MAX } else { 0 };
5149        }
5150        self.state[operands.dst].set_u64x2(c);
5151        ControlFlow::Continue(())
5152    }
5153
5154    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5155    fn vneg8x16(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5156        let a = self.state[src].get_i8x16();
5157        self.state[dst].set_i8x16(a.map(|i| i.wrapping_neg()));
5158        ControlFlow::Continue(())
5159    }
5160
5161    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5162    fn vneg16x8(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5163        let a = self.state[src].get_i16x8();
5164        self.state[dst].set_i16x8(a.map(|i| i.wrapping_neg()));
5165        ControlFlow::Continue(())
5166    }
5167
5168    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5169    fn vneg32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5170        let a = self.state[src].get_i32x4();
5171        self.state[dst].set_i32x4(a.map(|i| i.wrapping_neg()));
5172        ControlFlow::Continue(())
5173    }
5174
5175    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5176    fn vneg64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5177        let a = self.state[src].get_i64x2();
5178        self.state[dst].set_i64x2(a.map(|i| i.wrapping_neg()));
5179        ControlFlow::Continue(())
5180    }
5181
5182    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5183    fn vnegf64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5184        let a = self.state[src].get_f64x2();
5185        self.state[dst].set_f64x2(a.map(|i| -i));
5186        ControlFlow::Continue(())
5187    }
5188
5189    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5190    fn vmin8x16_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5191        let mut a = self.state[operands.src1].get_i8x16();
5192        let b = self.state[operands.src2].get_i8x16();
5193        for (a, b) in a.iter_mut().zip(&b) {
5194            *a = (*a).min(*b);
5195        }
5196        self.state[operands.dst].set_i8x16(a);
5197        ControlFlow::Continue(())
5198    }
5199
5200    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5201    fn vmin8x16_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5202        let mut a = self.state[operands.src1].get_u8x16();
5203        let b = self.state[operands.src2].get_u8x16();
5204        for (a, b) in a.iter_mut().zip(&b) {
5205            *a = (*a).min(*b);
5206        }
5207        self.state[operands.dst].set_u8x16(a);
5208        ControlFlow::Continue(())
5209    }
5210
5211    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5212    fn vmin16x8_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5213        let mut a = self.state[operands.src1].get_i16x8();
5214        let b = self.state[operands.src2].get_i16x8();
5215        for (a, b) in a.iter_mut().zip(&b) {
5216            *a = (*a).min(*b);
5217        }
5218        self.state[operands.dst].set_i16x8(a);
5219        ControlFlow::Continue(())
5220    }
5221
5222    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5223    fn vmin16x8_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5224        let mut a = self.state[operands.src1].get_u16x8();
5225        let b = self.state[operands.src2].get_u16x8();
5226        for (a, b) in a.iter_mut().zip(&b) {
5227            *a = (*a).min(*b);
5228        }
5229        self.state[operands.dst].set_u16x8(a);
5230        ControlFlow::Continue(())
5231    }
5232
5233    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5234    fn vmin32x4_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5235        let mut a = self.state[operands.src1].get_i32x4();
5236        let b = self.state[operands.src2].get_i32x4();
5237        for (a, b) in a.iter_mut().zip(&b) {
5238            *a = (*a).min(*b);
5239        }
5240        self.state[operands.dst].set_i32x4(a);
5241        ControlFlow::Continue(())
5242    }
5243
5244    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5245    fn vmin32x4_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5246        let mut a = self.state[operands.src1].get_u32x4();
5247        let b = self.state[operands.src2].get_u32x4();
5248        for (a, b) in a.iter_mut().zip(&b) {
5249            *a = (*a).min(*b);
5250        }
5251        self.state[operands.dst].set_u32x4(a);
5252        ControlFlow::Continue(())
5253    }
5254
5255    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5256    fn vmax8x16_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5257        let mut a = self.state[operands.src1].get_i8x16();
5258        let b = self.state[operands.src2].get_i8x16();
5259        for (a, b) in a.iter_mut().zip(&b) {
5260            *a = (*a).max(*b);
5261        }
5262        self.state[operands.dst].set_i8x16(a);
5263        ControlFlow::Continue(())
5264    }
5265
5266    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5267    fn vmax8x16_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5268        let mut a = self.state[operands.src1].get_u8x16();
5269        let b = self.state[operands.src2].get_u8x16();
5270        for (a, b) in a.iter_mut().zip(&b) {
5271            *a = (*a).max(*b);
5272        }
5273        self.state[operands.dst].set_u8x16(a);
5274        ControlFlow::Continue(())
5275    }
5276
5277    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5278    fn vmax16x8_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5279        let mut a = self.state[operands.src1].get_i16x8();
5280        let b = self.state[operands.src2].get_i16x8();
5281        for (a, b) in a.iter_mut().zip(&b) {
5282            *a = (*a).max(*b);
5283        }
5284        self.state[operands.dst].set_i16x8(a);
5285        ControlFlow::Continue(())
5286    }
5287
5288    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5289    fn vmax16x8_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5290        let mut a = self.state[operands.src1].get_u16x8();
5291        let b = self.state[operands.src2].get_u16x8();
5292        for (a, b) in a.iter_mut().zip(&b) {
5293            *a = (*a).max(*b);
5294        }
5295        self.state[operands.dst].set_u16x8(a);
5296        ControlFlow::Continue(())
5297    }
5298
5299    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5300    fn vmax32x4_s(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5301        let mut a = self.state[operands.src1].get_i32x4();
5302        let b = self.state[operands.src2].get_i32x4();
5303        for (a, b) in a.iter_mut().zip(&b) {
5304            *a = (*a).max(*b);
5305        }
5306        self.state[operands.dst].set_i32x4(a);
5307        ControlFlow::Continue(())
5308    }
5309
5310    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5311    fn vmax32x4_u(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5312        let mut a = self.state[operands.src1].get_u32x4();
5313        let b = self.state[operands.src2].get_u32x4();
5314        for (a, b) in a.iter_mut().zip(&b) {
5315            *a = (*a).max(*b);
5316        }
5317        self.state[operands.dst].set_u32x4(a);
5318        ControlFlow::Continue(())
5319    }
5320
5321    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5322    fn vabs8x16(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5323        let a = self.state[src].get_i8x16();
5324        self.state[dst].set_i8x16(a.map(|i| i.wrapping_abs()));
5325        ControlFlow::Continue(())
5326    }
5327
5328    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5329    fn vabs16x8(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5330        let a = self.state[src].get_i16x8();
5331        self.state[dst].set_i16x8(a.map(|i| i.wrapping_abs()));
5332        ControlFlow::Continue(())
5333    }
5334
5335    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5336    fn vabs32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5337        let a = self.state[src].get_i32x4();
5338        self.state[dst].set_i32x4(a.map(|i| i.wrapping_abs()));
5339        ControlFlow::Continue(())
5340    }
5341
5342    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5343    fn vabs64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5344        let a = self.state[src].get_i64x2();
5345        self.state[dst].set_i64x2(a.map(|i| i.wrapping_abs()));
5346        ControlFlow::Continue(())
5347    }
5348
5349    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5350    fn vabsf32x4(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5351        let a = self.state[src].get_f32x4();
5352        self.state[dst].set_f32x4(a.map(|i| i.wasm_abs()));
5353        ControlFlow::Continue(())
5354    }
5355
5356    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5357    fn vabsf64x2(&mut self, dst: VReg, src: VReg) -> ControlFlow<Done> {
5358        let a = self.state[src].get_f64x2();
5359        self.state[dst].set_f64x2(a.map(|i| i.wasm_abs()));
5360        ControlFlow::Continue(())
5361    }
5362
5363    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5364    fn vmaximumf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5365        let mut a = self.state[operands.src1].get_f32x4();
5366        let b = self.state[operands.src2].get_f32x4();
5367        for (a, b) in a.iter_mut().zip(&b) {
5368            *a = a.wasm_maximum(*b);
5369        }
5370        self.state[operands.dst].set_f32x4(a);
5371        ControlFlow::Continue(())
5372    }
5373
5374    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5375    fn vmaximumf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5376        let mut a = self.state[operands.src1].get_f64x2();
5377        let b = self.state[operands.src2].get_f64x2();
5378        for (a, b) in a.iter_mut().zip(&b) {
5379            *a = a.wasm_maximum(*b);
5380        }
5381        self.state[operands.dst].set_f64x2(a);
5382        ControlFlow::Continue(())
5383    }
5384
5385    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5386    fn vminimumf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5387        let mut a = self.state[operands.src1].get_f32x4();
5388        let b = self.state[operands.src2].get_f32x4();
5389        for (a, b) in a.iter_mut().zip(&b) {
5390            *a = a.wasm_minimum(*b);
5391        }
5392        self.state[operands.dst].set_f32x4(a);
5393        ControlFlow::Continue(())
5394    }
5395
5396    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5397    fn vminimumf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5398        let mut a = self.state[operands.src1].get_f64x2();
5399        let b = self.state[operands.src2].get_f64x2();
5400        for (a, b) in a.iter_mut().zip(&b) {
5401            *a = a.wasm_minimum(*b);
5402        }
5403        self.state[operands.dst].set_f64x2(a);
5404        ControlFlow::Continue(())
5405    }
5406
5407    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5408    fn vshuffle(&mut self, dst: VReg, src1: VReg, src2: VReg, mask: u128) -> ControlFlow<Done> {
5409        let a = self.state[src1].get_u8x16();
5410        let b = self.state[src2].get_u8x16();
5411        let result = mask.to_le_bytes().map(|m| {
5412            if m < 16 {
5413                a[m as usize]
5414            } else {
5415                b[m as usize - 16]
5416            }
5417        });
5418        self.state[dst].set_u8x16(result);
5419        ControlFlow::Continue(())
5420    }
5421
5422    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5423    fn vswizzlei8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5424        let src1 = self.state[operands.src1].get_i8x16();
5425        let src2 = self.state[operands.src2].get_i8x16();
5426        let mut dst = [0i8; 16];
5427        for (i, &idx) in src2.iter().enumerate() {
5428            if (idx as usize) < 16 {
5429                dst[i] = src1[idx as usize];
5430            } else {
5431                dst[i] = 0
5432            }
5433        }
5434        self.state[operands.dst].set_i8x16(dst);
5435        ControlFlow::Continue(())
5436    }
5437
5438    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5439    fn vavground8x16(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5440        let mut a = self.state[operands.src1].get_u8x16();
5441        let b = self.state[operands.src2].get_u8x16();
5442        for (a, b) in a.iter_mut().zip(&b) {
5443            // use wider precision to avoid overflow
5444            *a = ((u32::from(*a) + u32::from(*b) + 1) / 2) as u8;
5445        }
5446        self.state[operands.dst].set_u8x16(a);
5447        ControlFlow::Continue(())
5448    }
5449
5450    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5451    fn vavground16x8(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5452        let mut a = self.state[operands.src1].get_u16x8();
5453        let b = self.state[operands.src2].get_u16x8();
5454        for (a, b) in a.iter_mut().zip(&b) {
5455            // use wider precision to avoid overflow
5456            *a = ((u32::from(*a) + u32::from(*b) + 1) / 2) as u16;
5457        }
5458        self.state[operands.dst].set_u16x8(a);
5459        ControlFlow::Continue(())
5460    }
5461
5462    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5463    fn veqf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5464        let a = self.state[operands.src1].get_f32x4();
5465        let b = self.state[operands.src2].get_f32x4();
5466        let mut c = [0; 4];
5467        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5468            *c = if a == b { u32::MAX } else { 0 };
5469        }
5470        self.state[operands.dst].set_u32x4(c);
5471        ControlFlow::Continue(())
5472    }
5473
5474    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5475    fn vneqf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5476        let a = self.state[operands.src1].get_f32x4();
5477        let b = self.state[operands.src2].get_f32x4();
5478        let mut c = [0; 4];
5479        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5480            *c = if a != b { u32::MAX } else { 0 };
5481        }
5482        self.state[operands.dst].set_u32x4(c);
5483        ControlFlow::Continue(())
5484    }
5485
5486    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5487    fn vltf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5488        let a = self.state[operands.src1].get_f32x4();
5489        let b = self.state[operands.src2].get_f32x4();
5490        let mut c = [0; 4];
5491        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5492            *c = if a < b { u32::MAX } else { 0 };
5493        }
5494        self.state[operands.dst].set_u32x4(c);
5495        ControlFlow::Continue(())
5496    }
5497
5498    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5499    fn vlteqf32x4(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5500        let a = self.state[operands.src1].get_f32x4();
5501        let b = self.state[operands.src2].get_f32x4();
5502        let mut c = [0; 4];
5503        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5504            *c = if a <= b { u32::MAX } else { 0 };
5505        }
5506        self.state[operands.dst].set_u32x4(c);
5507        ControlFlow::Continue(())
5508    }
5509
5510    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5511    fn veqf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5512        let a = self.state[operands.src1].get_f64x2();
5513        let b = self.state[operands.src2].get_f64x2();
5514        let mut c = [0; 2];
5515        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5516            *c = if a == b { u64::MAX } else { 0 };
5517        }
5518        self.state[operands.dst].set_u64x2(c);
5519        ControlFlow::Continue(())
5520    }
5521
5522    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5523    fn vneqf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5524        let a = self.state[operands.src1].get_f64x2();
5525        let b = self.state[operands.src2].get_f64x2();
5526        let mut c = [0; 2];
5527        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5528            *c = if a != b { u64::MAX } else { 0 };
5529        }
5530        self.state[operands.dst].set_u64x2(c);
5531        ControlFlow::Continue(())
5532    }
5533
5534    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5535    fn vltf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5536        let a = self.state[operands.src1].get_f64x2();
5537        let b = self.state[operands.src2].get_f64x2();
5538        let mut c = [0; 2];
5539        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5540            *c = if a < b { u64::MAX } else { 0 };
5541        }
5542        self.state[operands.dst].set_u64x2(c);
5543        ControlFlow::Continue(())
5544    }
5545
5546    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5547    fn vlteqf64x2(&mut self, operands: BinaryOperands<VReg>) -> ControlFlow<Done> {
5548        let a = self.state[operands.src1].get_f64x2();
5549        let b = self.state[operands.src2].get_f64x2();
5550        let mut c = [0; 2];
5551        for ((a, b), c) in a.iter().zip(&b).zip(&mut c) {
5552            *c = if a <= b { u64::MAX } else { 0 };
5553        }
5554        self.state[operands.dst].set_u64x2(c);
5555        ControlFlow::Continue(())
5556    }
5557
5558    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5559    fn vfma32x4(&mut self, dst: VReg, a: VReg, b: VReg, c: VReg) -> ControlFlow<Done> {
5560        let mut a = self.state[a].get_f32x4();
5561        let b = self.state[b].get_f32x4();
5562        let c = self.state[c].get_f32x4();
5563        for ((a, b), c) in a.iter_mut().zip(b).zip(c) {
5564            *a = a.wasm_mul_add(b, c);
5565        }
5566        self.state[dst].set_f32x4(a);
5567        ControlFlow::Continue(())
5568    }
5569
5570    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5571    fn vfma64x2(&mut self, dst: VReg, a: VReg, b: VReg, c: VReg) -> ControlFlow<Done> {
5572        let mut a = self.state[a].get_f64x2();
5573        let b = self.state[b].get_f64x2();
5574        let c = self.state[c].get_f64x2();
5575        for ((a, b), c) in a.iter_mut().zip(b).zip(c) {
5576            *a = a.wasm_mul_add(b, c);
5577        }
5578        self.state[dst].set_f64x2(a);
5579        ControlFlow::Continue(())
5580    }
5581
5582    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5583    fn vselect(
5584        &mut self,
5585        dst: VReg,
5586        cond: XReg,
5587        if_nonzero: VReg,
5588        if_zero: VReg,
5589    ) -> ControlFlow<Done> {
5590        let result = if self.state[cond].get_u32() != 0 {
5591            self.state[if_nonzero]
5592        } else {
5593            self.state[if_zero]
5594        };
5595        self.state[dst] = result;
5596        ControlFlow::Continue(())
5597    }
5598
5599    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5600    fn xadd128(
5601        &mut self,
5602        dst_lo: XReg,
5603        dst_hi: XReg,
5604        lhs_lo: XReg,
5605        lhs_hi: XReg,
5606        rhs_lo: XReg,
5607        rhs_hi: XReg,
5608    ) -> ControlFlow<Done> {
5609        let lhs = self.get_i128(lhs_lo, lhs_hi);
5610        let rhs = self.get_i128(rhs_lo, rhs_hi);
5611        let result = lhs.wrapping_add(rhs);
5612        self.set_i128(dst_lo, dst_hi, result);
5613        ControlFlow::Continue(())
5614    }
5615
5616    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5617    fn xsub128(
5618        &mut self,
5619        dst_lo: XReg,
5620        dst_hi: XReg,
5621        lhs_lo: XReg,
5622        lhs_hi: XReg,
5623        rhs_lo: XReg,
5624        rhs_hi: XReg,
5625    ) -> ControlFlow<Done> {
5626        let lhs = self.get_i128(lhs_lo, lhs_hi);
5627        let rhs = self.get_i128(rhs_lo, rhs_hi);
5628        let result = lhs.wrapping_sub(rhs);
5629        self.set_i128(dst_lo, dst_hi, result);
5630        ControlFlow::Continue(())
5631    }
5632
5633    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5634    fn xwidemul64_s(
5635        &mut self,
5636        dst_lo: XReg,
5637        dst_hi: XReg,
5638        lhs: XReg,
5639        rhs: XReg,
5640    ) -> ControlFlow<Done> {
5641        let lhs = self.state[lhs].get_i64();
5642        let rhs = self.state[rhs].get_i64();
5643        let result = i128::from(lhs).wrapping_mul(i128::from(rhs));
5644        self.set_i128(dst_lo, dst_hi, result);
5645        ControlFlow::Continue(())
5646    }
5647
5648    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5649    fn xwidemul64_u(
5650        &mut self,
5651        dst_lo: XReg,
5652        dst_hi: XReg,
5653        lhs: XReg,
5654        rhs: XReg,
5655    ) -> ControlFlow<Done> {
5656        let lhs = self.state[lhs].get_u64();
5657        let rhs = self.state[rhs].get_u64();
5658        let result = u128::from(lhs).wrapping_mul(u128::from(rhs));
5659        self.set_i128(dst_lo, dst_hi, result as i128);
5660        ControlFlow::Continue(())
5661    }
5662
5663    // =========================================================================
5664    // z addressing modes (big endian)
5665
5666    fn xload16be_u32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
5667        let result = unsafe { self.load_ne::<u16, crate::XLoad16BeU32Z>(addr)? };
5668        self.state[dst].set_u32(u16::from_be(result).into());
5669        ControlFlow::Continue(())
5670    }
5671
5672    fn xload16be_s32_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
5673        let result = unsafe { self.load_ne::<i16, crate::XLoad16BeS32Z>(addr)? };
5674        self.state[dst].set_i32(i16::from_be(result).into());
5675        ControlFlow::Continue(())
5676    }
5677
5678    fn xload32be_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
5679        let result = unsafe { self.load_ne::<i32, crate::XLoad32BeZ>(addr)? };
5680        self.state[dst].set_i32(i32::from_be(result));
5681        ControlFlow::Continue(())
5682    }
5683
5684    fn xload64be_z(&mut self, dst: XReg, addr: AddrZ) -> ControlFlow<Done> {
5685        let result = unsafe { self.load_ne::<i64, crate::XLoad64BeZ>(addr)? };
5686        self.state[dst].set_i64(i64::from_be(result));
5687        ControlFlow::Continue(())
5688    }
5689
5690    fn xstore16be_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
5691        let val = self.state[val].get_u32() as u16;
5692        unsafe {
5693            self.store_ne::<u16, crate::XStore16BeZ>(addr, val.to_be())?;
5694        }
5695        ControlFlow::Continue(())
5696    }
5697
5698    fn xstore32be_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
5699        let val = self.state[val].get_u32();
5700        unsafe {
5701            self.store_ne::<u32, crate::XStore32BeZ>(addr, val.to_be())?;
5702        }
5703        ControlFlow::Continue(())
5704    }
5705
5706    fn xstore64be_z(&mut self, addr: AddrZ, val: XReg) -> ControlFlow<Done> {
5707        let val = self.state[val].get_u64();
5708        unsafe {
5709            self.store_ne::<u64, crate::XStore64BeZ>(addr, val.to_be())?;
5710        }
5711        ControlFlow::Continue(())
5712    }
5713
5714    fn fload32be_z(&mut self, dst: FReg, addr: AddrZ) -> ControlFlow<Done> {
5715        let val = unsafe { self.load_ne::<u32, crate::Fload32BeZ>(addr)? };
5716        self.state[dst].set_f32(f32::from_bits(u32::from_be(val)));
5717        ControlFlow::Continue(())
5718    }
5719
5720    fn fload64be_z(&mut self, dst: FReg, addr: AddrZ) -> ControlFlow<Done> {
5721        let val = unsafe { self.load_ne::<u64, crate::Fload64BeZ>(addr)? };
5722        self.state[dst].set_f64(f64::from_bits(u64::from_be(val)));
5723        ControlFlow::Continue(())
5724    }
5725
5726    fn fstore32be_z(&mut self, addr: AddrZ, src: FReg) -> ControlFlow<Done> {
5727        let val = self.state[src].get_f32();
5728        unsafe {
5729            self.store_ne::<u32, crate::Fstore32BeZ>(addr, val.to_bits().to_be())?;
5730        }
5731        ControlFlow::Continue(())
5732    }
5733
5734    fn fstore64be_z(&mut self, addr: AddrZ, src: FReg) -> ControlFlow<Done> {
5735        let val = self.state[src].get_f64();
5736        unsafe {
5737            self.store_ne::<u64, crate::Fstore64BeZ>(addr, val.to_bits().to_be())?;
5738        }
5739        ControlFlow::Continue(())
5740    }
5741
5742    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5743    fn vload128be_z(&mut self, dst: VReg, addr: AddrZ) -> ControlFlow<Done> {
5744        let val = unsafe { self.load_ne::<u128, crate::VLoad128BeZ>(addr)? };
5745        self.state[dst].set_u128(u128::from_be(val));
5746        ControlFlow::Continue(())
5747    }
5748
5749    #[interp_disable_if_cfg(pulley_disable_interp_simd)]
5750    fn vstore128be_z(&mut self, addr: AddrZ, src: VReg) -> ControlFlow<Done> {
5751        let val = self.state[src].get_u128();
5752        unsafe {
5753            self.store_ne::<u128, crate::Vstore128BeZ>(addr, val.to_be())?;
5754        }
5755        ControlFlow::Continue(())
5756    }
5757}