zingen/masm/
cmp.rs

1// Comparison Instructions
2
3use crate::{MacroAssembler, Result};
4use opcodes::Cancun as OpCode;
5
6impl MacroAssembler {
7    /// Greater than or equal comparison.
8    ///
9    /// a b ge -> a b-1 gt(lt)
10    ///
11    /// Using lt due to order of stack.
12    pub fn _ge(&mut self) -> Result<()> {
13        self.push(&[1])?;
14        // NOTE: this is the overridden sub but not `self.asm.sub`
15        self._sub()?;
16        self.asm._lt()
17    }
18
19    /// Greater than or equal comparison.
20    ///
21    /// a b sge -> a b-1 sgt(slt)
22    ///
23    /// Using lt due to order of stack.
24    pub fn _sge(&mut self) -> Result<()> {
25        self.push(&[1])?;
26        // NOTE: this is the overridden sub but not `self.asm.sub`
27        self._sub()?;
28        self.asm._slt()
29    }
30
31    /// Greater than or equal comparison.
32    ///
33    /// a b sge -> a b-1 sgt(slt)
34    ///
35    /// Using lt due to order of stack.
36    pub fn _sle(&mut self) -> Result<()> {
37        self.push(&[1])?;
38        // NOTE: this is the overridden sub but not `self.asm.sub`
39        self._sub()?;
40        self.asm._slt()
41    }
42
43    /// Greater than or equal comparison.
44    ///
45    /// a b le -> a b-1 lt(gt)
46    ///
47    /// Using gt due to order of stack.
48    pub fn _le(&mut self) -> Result<()> {
49        self.push(&[1])?;
50        // NOTE: this is the overridden sub but not `self.asm.sub`
51        self._sub()?;
52        self.asm._lt()
53    }
54
55    /// Greater than and equal comparison.
56    ///
57    /// Using slt due to order of stack.
58    pub fn _sgt(&mut self) -> Result<()> {
59        self.asm._slt()
60    }
61
62    /// Greater than comparison.
63    ///
64    /// Using lt due to order of stack.
65    pub fn _gt(&mut self) -> Result<()> {
66        self.asm._lt()
67    }
68
69    /// less than comparison.
70    ///
71    /// Using gt due to order of stack.
72    pub fn _lt(&mut self) -> Result<()> {
73        self.asm._gt()
74    }
75
76    /// less than or equal comparison.
77    ///
78    /// Using gt due to order of stack.
79    pub fn _slt(&mut self) -> Result<()> {
80        self.asm._sgt()
81    }
82
83    /// Sign-agnostic compare unequal.
84    pub fn _ne(&mut self) -> Result<()> {
85        self.emit_op(OpCode::EQ)?;
86        self.emit_op(OpCode::ISZERO)?;
87        Ok(())
88    }
89
90    /// Simple not operator
91    pub fn _eqz(&mut self) -> Result<()> {
92        self.emit_op(OpCode::ISZERO)?;
93        Ok(())
94    }
95}