From e4fe2229314411f597e4d20dc64c6fda390e81f2 Mon Sep 17 00:00:00 2001
From: Ben Bridle <bridle.benjamin@gmail.com>
Date: Sat, 7 Sep 2024 18:21:24 +1200
Subject: Implement micro optimisations for unary operators

A 1% speed increase was observed when testing a tight decrementing loop
with these optimisations.
---
 src/stack.rs | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

(limited to 'src/stack.rs')

diff --git a/src/stack.rs b/src/stack.rs
index 14e1799..2862b72 100644
--- a/src/stack.rs
+++ b/src/stack.rs
@@ -50,3 +50,39 @@ impl Stack {
         self.sp = 0;
     }
 }
+
+use std::ops::Not;
+
+impl Stack {
+    pub fn inc_u8(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].wrapping_add(1);
+    }
+
+    pub fn dec_u8(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].wrapping_sub(1);
+    }
+
+    pub fn not_u8(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].not();
+    }
+
+    pub fn not_u16(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].not();
+        let sp = self.sp.wrapping_sub(2) as usize;
+        self.mem[sp] = self.mem[sp].not();
+    }
+
+    pub fn tal_u8(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].count_ones() as u8;
+    }
+
+    pub fn rev_u8(&mut self) {
+        let sp = self.sp.wrapping_sub(1) as usize;
+        self.mem[sp] = self.mem[sp].reverse_bits();
+    }
+}
-- 
cgit v1.2.3-70-g09d2