Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: split ops into arith and bit modules #4989

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/docs/noir/standard_library/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ These traits abstract over addition, subtraction, multiplication, and division r
Implementing these traits for a given type will also allow that type to be used with the corresponding operator
for that trait (`+` for Add, etc) in addition to the normal method names.

#include_code add-trait noir_stdlib/src/ops.nr rust
#include_code sub-trait noir_stdlib/src/ops.nr rust
#include_code mul-trait noir_stdlib/src/ops.nr rust
#include_code div-trait noir_stdlib/src/ops.nr rust
#include_code add-trait noir_stdlib/src/ops/arith.nr rust
#include_code sub-trait noir_stdlib/src/ops/arith.nr rust
#include_code mul-trait noir_stdlib/src/ops/arith.nr rust
#include_code div-trait noir_stdlib/src/ops/arith.nr rust

The implementations block below is given for the `Add` trait, but the same types that implement
`Add` also implement `Sub`, `Mul`, and `Div`.
Expand All @@ -211,7 +211,7 @@ impl Add for u64 { .. }

### `std::ops::Rem`

#include_code rem-trait noir_stdlib/src/ops.nr rust
#include_code rem-trait noir_stdlib/src/ops/arith.nr rust

`Rem::rem(a, b)` is the remainder function returning the result of what is
left after dividing `a` and `b`. Implementing `Rem` allows the `%` operator
Expand All @@ -234,18 +234,18 @@ impl Rem for i64 { fn rem(self, other: i64) -> i64 { self % other } }

### `std::ops::Neg`

#include_code neg-trait noir_stdlib/src/ops.nr rust
#include_code neg-trait noir_stdlib/src/ops/arith.nr rust

`Neg::neg` is equivalent to the unary negation operator `-`.

Implementations:
#include_code neg-trait-impls noir_stdlib/src/ops.nr rust
#include_code neg-trait-impls noir_stdlib/src/ops/arith.nr rust

### `std::ops::{ BitOr, BitAnd, BitXor }`

#include_code bitor-trait noir_stdlib/src/ops.nr rust
#include_code bitand-trait noir_stdlib/src/ops.nr rust
#include_code bitxor-trait noir_stdlib/src/ops.nr rust
#include_code bitor-trait noir_stdlib/src/ops/bit.nr rust
#include_code bitand-trait noir_stdlib/src/ops/bit.nr rust
#include_code bitxor-trait noir_stdlib/src/ops/bit.nr rust

Traits for the bitwise operations `|`, `&`, and `^`.

Expand All @@ -272,8 +272,8 @@ impl BitOr for i64 { fn bitor(self, other: i64) -> i64 { self | other } }

### `std::ops::{ Shl, Shr }`

#include_code shl-trait noir_stdlib/src/ops.nr rust
#include_code shr-trait noir_stdlib/src/ops.nr rust
#include_code shl-trait noir_stdlib/src/ops/bit.nr rust
#include_code shr-trait noir_stdlib/src/ops/bit.nr rust

Traits for a bit shift left and bit shift right.

Expand Down
173 changes: 4 additions & 169 deletions noir_stdlib/src/ops.nr
Original file line number Diff line number Diff line change
@@ -1,170 +1,5 @@
// docs:start:add-trait
trait Add {
fn add(self, other: Self) -> Self;
}
// docs:end:add-trait

impl Add for Field { fn add(self, other: Field) -> Field { self + other } }

impl Add for u64 { fn add(self, other: u64) -> u64 { self + other } }
impl Add for u32 { fn add(self, other: u32) -> u32 { self + other } }
impl Add for u8 { fn add(self, other: u8) -> u8 { self + other } }

impl Add for i8 { fn add(self, other: i8) -> i8 { self + other } }
impl Add for i32 { fn add(self, other: i32) -> i32 { self + other } }
impl Add for i64 { fn add(self, other: i64) -> i64 { self + other } }

// docs:start:sub-trait
trait Sub {
fn sub(self, other: Self) -> Self;
}
// docs:end:sub-trait

impl Sub for Field { fn sub(self, other: Field) -> Field { self - other } }

impl Sub for u64 { fn sub(self, other: u64) -> u64 { self - other } }
impl Sub for u32 { fn sub(self, other: u32) -> u32 { self - other } }
impl Sub for u8 { fn sub(self, other: u8) -> u8 { self - other } }

impl Sub for i8 { fn sub(self, other: i8) -> i8 { self - other } }
impl Sub for i32 { fn sub(self, other: i32) -> i32 { self - other } }
impl Sub for i64 { fn sub(self, other: i64) -> i64 { self - other } }

// docs:start:mul-trait
trait Mul {
fn mul(self, other: Self) -> Self;
}
// docs:end:mul-trait

impl Mul for Field { fn mul(self, other: Field) -> Field { self * other } }

impl Mul for u64 { fn mul(self, other: u64) -> u64 { self * other } }
impl Mul for u32 { fn mul(self, other: u32) -> u32 { self * other } }
impl Mul for u8 { fn mul(self, other: u8) -> u8 { self * other } }

impl Mul for i8 { fn mul(self, other: i8) -> i8 { self * other } }
impl Mul for i32 { fn mul(self, other: i32) -> i32 { self * other } }
impl Mul for i64 { fn mul(self, other: i64) -> i64 { self * other } }

// docs:start:div-trait
trait Div {
fn div(self, other: Self) -> Self;
}
// docs:end:div-trait

impl Div for Field { fn div(self, other: Field) -> Field { self / other } }

impl Div for u64 { fn div(self, other: u64) -> u64 { self / other } }
impl Div for u32 { fn div(self, other: u32) -> u32 { self / other } }
impl Div for u8 { fn div(self, other: u8) -> u8 { self / other } }

impl Div for i8 { fn div(self, other: i8) -> i8 { self / other } }
impl Div for i32 { fn div(self, other: i32) -> i32 { self / other } }
impl Div for i64 { fn div(self, other: i64) -> i64 { self / other } }

// docs:start:rem-trait
trait Rem{
fn rem(self, other: Self) -> Self;
}
// docs:end:rem-trait

impl Rem for u64 { fn rem(self, other: u64) -> u64 { self % other } }
impl Rem for u32 { fn rem(self, other: u32) -> u32 { self % other } }
impl Rem for u8 { fn rem(self, other: u8) -> u8 { self % other } }

impl Rem for i8 { fn rem(self, other: i8) -> i8 { self % other } }
impl Rem for i32 { fn rem(self, other: i32) -> i32 { self % other } }
impl Rem for i64 { fn rem(self, other: i64) -> i64 { self % other } }

// docs:start:neg-trait
trait Neg {
fn neg(self) -> Self;
}
// docs:end:neg-trait

// docs:start:neg-trait-impls
impl Neg for Field { fn neg(self) -> Field { -self } }

impl Neg for i8 { fn neg(self) -> i8 { -self } }
impl Neg for i32 { fn neg(self) -> i32 { -self } }
impl Neg for i64 { fn neg(self) -> i64 { -self } }
// docs:end:neg-trait-impls

// docs:start:bitor-trait
trait BitOr {
fn bitor(self, other: Self) -> Self;
}
// docs:end:bitor-trait

impl BitOr for bool { fn bitor(self, other: bool) -> bool { self | other } }

impl BitOr for u64 { fn bitor(self, other: u64) -> u64 { self | other } }
impl BitOr for u32 { fn bitor(self, other: u32) -> u32 { self | other } }
impl BitOr for u8 { fn bitor(self, other: u8) -> u8 { self | other } }

impl BitOr for i8 { fn bitor(self, other: i8) -> i8 { self | other } }
impl BitOr for i32 { fn bitor(self, other: i32) -> i32 { self | other } }
impl BitOr for i64 { fn bitor(self, other: i64) -> i64 { self | other } }

// docs:start:bitand-trait
trait BitAnd {
fn bitand(self, other: Self) -> Self;
}
// docs:end:bitand-trait

impl BitAnd for bool { fn bitand(self, other: bool) -> bool { self & other } }

impl BitAnd for u64 { fn bitand(self, other: u64) -> u64 { self & other } }
impl BitAnd for u32 { fn bitand(self, other: u32) -> u32 { self & other } }
impl BitAnd for u8 { fn bitand(self, other: u8) -> u8 { self & other } }

impl BitAnd for i8 { fn bitand(self, other: i8) -> i8 { self & other } }
impl BitAnd for i32 { fn bitand(self, other: i32) -> i32 { self & other } }
impl BitAnd for i64 { fn bitand(self, other: i64) -> i64 { self & other } }

// docs:start:bitxor-trait
trait BitXor {
fn bitxor(self, other: Self) -> Self;
}
// docs:end:bitxor-trait

impl BitXor for bool { fn bitxor(self, other: bool) -> bool { self ^ other } }

impl BitXor for u64 { fn bitxor(self, other: u64) -> u64 { self ^ other } }
impl BitXor for u32 { fn bitxor(self, other: u32) -> u32 { self ^ other } }
impl BitXor for u8 { fn bitxor(self, other: u8) -> u8 { self ^ other } }

impl BitXor for i8 { fn bitxor(self, other: i8) -> i8 { self ^ other } }
impl BitXor for i32 { fn bitxor(self, other: i32) -> i32 { self ^ other } }
impl BitXor for i64 { fn bitxor(self, other: i64) -> i64 { self ^ other } }

// docs:start:shl-trait
trait Shl {
fn shl(self, other: u8) -> Self;
}
// docs:end:shl-trait

impl Shl for u32 { fn shl(self, other: u8) -> u32 { self << other } }
impl Shl for u64 { fn shl(self, other: u8) -> u64 { self << other } }
impl Shl for u8 { fn shl(self, other: u8) -> u8 { self << other } }
impl Shl for u1 { fn shl(self, other: u8) -> u1 { self << other } }

impl Shl for i8 { fn shl(self, other: u8) -> i8 { self << other } }
impl Shl for i32 { fn shl(self, other: u8) -> i32 { self << other } }
impl Shl for i64 { fn shl(self, other: u8) -> i64 { self << other } }

// docs:start:shr-trait
trait Shr {
fn shr(self, other: u8) -> Self;
}
// docs:end:shr-trait

impl Shr for u64 { fn shr(self, other: u8) -> u64 { self >> other } }
impl Shr for u32 { fn shr(self, other: u8) -> u32 { self >> other } }
impl Shr for u8 { fn shr(self, other: u8) -> u8 { self >> other } }
impl Shr for u1 { fn shr(self, other: u8) -> u1 { self >> other } }

impl Shr for i8 { fn shr(self, other: u8) -> i8 { self >> other } }
impl Shr for i32 { fn shr(self, other: u8) -> i32 { self >> other } }
impl Shr for i64 { fn shr(self, other: u8) -> i64 { self >> other } }
mod arith;
mod bit;

use arith::{Add, Sub, Mul, Div, Rem, Neg};
use bit::{BitOr, BitAnd, BitXor, Shl, Shr};
92 changes: 92 additions & 0 deletions noir_stdlib/src/ops/arith.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// docs:start:add-trait
trait Add {
fn add(self, other: Self) -> Self;
}
// docs:end:add-trait

impl Add for Field { fn add(self, other: Field) -> Field { self + other } }

impl Add for u64 { fn add(self, other: u64) -> u64 { self + other } }
impl Add for u32 { fn add(self, other: u32) -> u32 { self + other } }
impl Add for u8 { fn add(self, other: u8) -> u8 { self + other } }

impl Add for i8 { fn add(self, other: i8) -> i8 { self + other } }
impl Add for i32 { fn add(self, other: i32) -> i32 { self + other } }
impl Add for i64 { fn add(self, other: i64) -> i64 { self + other } }

// docs:start:sub-trait
trait Sub {
fn sub(self, other: Self) -> Self;
}
// docs:end:sub-trait

impl Sub for Field { fn sub(self, other: Field) -> Field { self - other } }

impl Sub for u64 { fn sub(self, other: u64) -> u64 { self - other } }
impl Sub for u32 { fn sub(self, other: u32) -> u32 { self - other } }
impl Sub for u8 { fn sub(self, other: u8) -> u8 { self - other } }

impl Sub for i8 { fn sub(self, other: i8) -> i8 { self - other } }
impl Sub for i32 { fn sub(self, other: i32) -> i32 { self - other } }
impl Sub for i64 { fn sub(self, other: i64) -> i64 { self - other } }

// docs:start:mul-trait
trait Mul {
fn mul(self, other: Self) -> Self;
}
// docs:end:mul-trait

impl Mul for Field { fn mul(self, other: Field) -> Field { self * other } }

impl Mul for u64 { fn mul(self, other: u64) -> u64 { self * other } }
impl Mul for u32 { fn mul(self, other: u32) -> u32 { self * other } }
impl Mul for u8 { fn mul(self, other: u8) -> u8 { self * other } }

impl Mul for i8 { fn mul(self, other: i8) -> i8 { self * other } }
impl Mul for i32 { fn mul(self, other: i32) -> i32 { self * other } }
impl Mul for i64 { fn mul(self, other: i64) -> i64 { self * other } }

// docs:start:div-trait
trait Div {
fn div(self, other: Self) -> Self;
}
// docs:end:div-trait

impl Div for Field { fn div(self, other: Field) -> Field { self / other } }

impl Div for u64 { fn div(self, other: u64) -> u64 { self / other } }
impl Div for u32 { fn div(self, other: u32) -> u32 { self / other } }
impl Div for u8 { fn div(self, other: u8) -> u8 { self / other } }

impl Div for i8 { fn div(self, other: i8) -> i8 { self / other } }
impl Div for i32 { fn div(self, other: i32) -> i32 { self / other } }
impl Div for i64 { fn div(self, other: i64) -> i64 { self / other } }

// docs:start:rem-trait
trait Rem{
fn rem(self, other: Self) -> Self;
}
// docs:end:rem-trait

impl Rem for u64 { fn rem(self, other: u64) -> u64 { self % other } }
impl Rem for u32 { fn rem(self, other: u32) -> u32 { self % other } }
impl Rem for u8 { fn rem(self, other: u8) -> u8 { self % other } }

impl Rem for i8 { fn rem(self, other: i8) -> i8 { self % other } }
impl Rem for i32 { fn rem(self, other: i32) -> i32 { self % other } }
impl Rem for i64 { fn rem(self, other: i64) -> i64 { self % other } }

// docs:start:neg-trait
trait Neg {
fn neg(self) -> Self;
}
// docs:end:neg-trait

// docs:start:neg-trait-impls
impl Neg for Field { fn neg(self) -> Field { -self } }

impl Neg for i8 { fn neg(self) -> i8 { -self } }
impl Neg for i32 { fn neg(self) -> i32 { -self } }
impl Neg for i64 { fn neg(self) -> i64 { -self } }
// docs:end:neg-trait-impls

Loading
Loading