operators.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 pub use crate::prelude::num_traits::Pow; 17 pub use console::traits::{ 18 arithmetic::*, 19 bitwise::*, 20 from_bits::{SizeInBits, SizeInDataBits}, 21 }; 22 23 use crate::BooleanTrait; 24 25 /// Unary operator for retrieving the inverse value. 26 pub trait Inverse { 27 type Output; 28 29 fn inverse(&self) -> Self::Output; 30 } 31 32 /// Unary operator for retrieving the square root of the value. 33 pub trait SquareRoot { 34 type Output; 35 36 fn square_root(&self) -> Self::Output; 37 } 38 39 /// A single-bit binary adder with a carry bit. 40 /// 41 /// <https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder> 42 /// 43 /// sum = (a XOR b) XOR carry 44 /// carry = a AND b OR carry AND (a XOR b) 45 /// return (sum, carry) 46 /// 47 pub trait Adder { 48 type Carry; 49 type Sum; 50 51 /// Returns the sum of `self` and `other` as a sum bit and carry bit. 52 fn adder(&self, other: &Self, carry: &Self) -> (Self::Sum, Self::Carry); 53 } 54 55 /// A single-bit binary subtractor with a borrow bit. 56 /// 57 /// <https://en.wikipedia.org/wiki/Subtractor#Full_subtractor> 58 /// 59 /// difference = (a XOR b) XOR borrow 60 /// borrow = ((NOT a) AND b) OR (borrow AND (NOT (a XOR b))) 61 /// return (difference, borrow) 62 /// 63 pub trait Subtractor { 64 type Borrow; 65 type Difference; 66 67 /// Returns the difference of `self` and `other` as a difference bit and borrow bit. 68 fn subtractor(&self, other: &Self, borrow: &Self) -> (Self::Difference, Self::Borrow); 69 } 70 71 /// Representation of the zero value. 72 pub trait Zero { 73 type Boolean: BooleanTrait; 74 75 /// Returns a new zero constant. 76 fn zero() -> Self 77 where 78 Self: Sized; 79 80 /// Returns `true` if `self` is zero. 81 fn is_zero(&self) -> Self::Boolean; 82 } 83 84 /// Representation of the one value. 85 pub trait One { 86 type Boolean: BooleanTrait; 87 88 /// Returns a new one constant. 89 fn one() -> Self 90 where 91 Self: Sized; 92 93 /// Returns `true` if `self` is one. 94 fn is_one(&self) -> Self::Boolean; 95 } 96 97 /// Unary operator for retrieving the most-significant bit. 98 pub trait MSB { 99 type Boolean: BooleanTrait; 100 101 /// Returns the MSB of the value. 102 fn msb(&self) -> &Self::Boolean; 103 }