Skip to content

Commit

Permalink
Provide generic impls for Core.Int and Core.UInt operations. (#4693)
Browse files Browse the repository at this point in the history
Instead of providing operations only for `i32`, provide them for all
`iN` and `uN` types.

For now, this excludes the `*Assign`, `Inc` and `Dec` interfaces,
because the implementations for those are defined as Carbon functions
rather than builtins, and we can't yet lower definitions for specific
functions, so converting those to be generic breaks the build for our
examples.
  • Loading branch information
zygoloid authored Dec 19, 2024
1 parent de41a4b commit 6fe8e0b
Show file tree
Hide file tree
Showing 163 changed files with 1,085 additions and 903 deletions.
180 changes: 102 additions & 78 deletions core/prelude/types/int.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Int(N:! IntLiteral()) {
adapt MakeInt(N);
}

// Conversions.

impl forall [N:! IntLiteral()] IntLiteral() as ImplicitAs(Int(N)) {
fn Convert[self: Self]() -> Int(N) = "int.convert_checked";
}
Expand All @@ -30,138 +32,160 @@ impl forall [N:! IntLiteral()] Int(N) as As(IntLiteral()) {
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
}

// TODO: Make these operations generic.
// Comparisons.

impl forall [N:! IntLiteral()] Int(N) as Eq {
fn Equal[self: Self](other: Self) -> bool = "int.eq";
fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
}

impl forall [N:! IntLiteral()] Int(N) as Ordered {
// TODO: fn Compare
fn Less[self: Self](other: Self) -> bool = "int.less";
fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
fn Greater[self: Self](other: Self) -> bool = "int.greater";
fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
}

// Arithmetic.

impl i32 as Add {
impl forall [N:! IntLiteral()] Int(N) as Add {
fn Op[self: Self](other: Self) -> Self = "int.sadd";
}
impl i32 as AddAssign {
fn Op[addr self: Self*](other: Self) {
// TODO: Once operator lookup works inside Core.
// *self = *self + other;
*self = self->(Add.Op)(other);
}

impl forall [N:! IntLiteral()] Int(N) as Div {
fn Op[self: Self](other: Self) -> Self = "int.sdiv";
}
impl i32 as Inc {
fn Op[addr self: Self*]() {
// *self += 1;
self->(AddAssign.Op)(1);
}

impl forall [N:! IntLiteral()] Int(N) as Mod {
fn Op[self: Self](other: Self) -> Self = "int.smod";
}

impl i32 as BitAnd {
fn Op[self: Self](other: Self) -> Self = "int.and";
impl forall [N:! IntLiteral()] Int(N) as Mul {
fn Op[self: Self](other: Self) -> Self = "int.smul";
}
impl i32 as BitAndAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self & other;
*self = self->(BitAnd.Op)(other);
}

impl forall [N:! IntLiteral()] Int(N) as Negate {
fn Op[self: Self]() -> Self = "int.snegate";
}

impl i32 as BitComplement {
impl forall [N:! IntLiteral()] Int(N) as Sub {
fn Op[self: Self](other: Self) -> Self = "int.ssub";
}

// Bitwise operators.

impl forall [N:! IntLiteral()] Int(N) as BitAnd {
fn Op[self: Self](other: Self) -> Self = "int.and";
}

impl forall [N:! IntLiteral()] Int(N) as BitComplement {
fn Op[self: Self]() -> Self = "int.complement";
}

impl i32 as BitOr {
impl forall [N:! IntLiteral()] Int(N) as BitOr {
fn Op[self: Self](other: Self) -> Self = "int.or";
}
impl i32 as BitOrAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self | other;
*self = self->(BitOr.Op)(other);
}
}

impl i32 as BitXor {
impl forall [N:! IntLiteral()] Int(N) as BitXor {
fn Op[self: Self](other: Self) -> Self = "int.xor";
}
impl i32 as BitXorAssign {

impl forall [N:! IntLiteral()] Int(N) as LeftShift {
fn Op[self: Self](other: Self) -> Self = "int.left_shift";
}

impl forall [N:! IntLiteral()] Int(N) as RightShift {
fn Op[self: Self](other: Self) -> Self = "int.right_shift";
}

// Compound assignments.

// TODO: Once we can lower specific functions, make these generic.
// Each function has a commented out generic signature.

// impl forall [N:! IntLiteral()] Int(N) as AddAssign {
impl i32 as AddAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self ^ other;
*self = self->(BitXor.Op)(other);
*self = *self + other;
}
}

impl i32 as Div {
fn Op[self: Self](other: Self) -> Self = "int.sdiv";
// impl forall [N:! IntLiteral()] Int(N) as BitAndAssign {
impl i32 as BitAndAssign {
fn Op[addr self: Self*](other: Self) {
*self = *self & other;
}
}
impl i32 as DivAssign {

// impl forall [N:! IntLiteral()] Int(N) as BitOrAssign {
impl i32 as BitOrAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self / other;
*self = self->(Div.Op)(other);
*self = *self | other;
}
}

impl i32 as Eq {
fn Equal[self: Self](other: Self) -> bool = "int.eq";
fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
// impl forall [N:! IntLiteral()] Int(N) as BitXorAssign {
impl i32 as BitXorAssign {
fn Op[addr self: Self*](other: Self) {
*self = *self ^ other;
}
}

impl i32 as LeftShift {
fn Op[self: Self](other: Self) -> Self = "int.left_shift";
// impl forall [N:! IntLiteral()] Int(N) as DivAssign {
impl i32 as DivAssign {
fn Op[addr self: Self*](other: Self) {
*self = *self / other;
}
}

// impl forall [N:! IntLiteral()] Int(N) as LeftShiftAssign {
impl i32 as LeftShiftAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self << other;
*self = self->(LeftShift.Op)(other);
*self = *self << other;
}
}

impl i32 as Mod {
fn Op[self: Self](other: Self) -> Self = "int.smod";
}
// impl forall [N:! IntLiteral()] Int(N) as ModAssign {
impl i32 as ModAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self % other;
*self = self->(Mod.Op)(other);
*self = *self % other;
}
}

impl i32 as Mul {
fn Op[self: Self](other: Self) -> Self = "int.smul";
}
// impl forall [N:! IntLiteral()] Int(N) as MulAssign {
impl i32 as MulAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self * other;
*self = self->(Mul.Op)(other);
*self = *self * other;
}
}

impl i32 as Negate {
fn Op[self: Self]() -> Self = "int.snegate";
}

impl i32 as Ordered {
// TODO: fn Compare
fn Less[self: Self](other: Self) -> bool = "int.less";
fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
fn Greater[self: Self](other: Self) -> bool = "int.greater";
fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
}

impl i32 as RightShift {
fn Op[self: Self](other: Self) -> Self = "int.right_shift";
}
// impl forall [N:! IntLiteral()] Int(N) as RightShiftAssign {
impl i32 as RightShiftAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self >> other;
*self = self->(RightShift.Op)(other);
*self = *self >> other;
}
}

impl i32 as Sub {
fn Op[self: Self](other: Self) -> Self = "int.ssub";
}
// impl forall [N:! IntLiteral()] Int(N) as SubAssign {
impl i32 as SubAssign {
fn Op[addr self: Self*](other: Self) {
// *self = *self - other;
*self = self->(Sub.Op)(other);
*self = *self - other;
}
}

// Increment and decrement.

// impl forall [N:! IntLiteral()] Int(N) as Dec {
impl i32 as Dec {
fn Op[addr self: Self*]() {
// *self -= 1;
self->(SubAssign.Op)(1);
*self -= 1;
}
}

// impl forall [N:! IntLiteral()] Int(N) as Inc {
impl i32 as Inc {
fn Op[addr self: Self*]() {
*self += 1;
}
}
Loading

0 comments on commit 6fe8e0b

Please sign in to comment.