Skip to content
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
1 change: 1 addition & 0 deletions Sources/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(swiftCore
IteratorProtocol.swift
Lifetime.swift
Never.swift
Numeric.swift
OpaquePointer.swift
Operators.swift
Optional.swift
Expand Down
13 changes: 13 additions & 0 deletions Sources/Core/Int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension Int: AdditiveArithmetic {
}
}

extension Int: Numeric {
@_transparent
public static func * (_ lhs: Int, _ rhs: Int) -> Int {
let (result, overflow) =
Builtin.smul_with_overflow_Word(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return Int(result)
}
}

extension Int: Comparable {
@_transparent
public static func < (_ lhs: Int, _ rhs: Int) -> Bool {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Core/Int32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension Int32: AdditiveArithmetic {
}
}

extension Int32: Numeric {
@_transparent
public static func * (_ lhs: Int32, _ rhs: Int32) -> Int32 {
let (result, overflow) =
Builtin.smul_with_overflow_Int32(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return Int32(result)
}
}

extension Int32: Comparable {
@_transparent
public static func < (_ lhs: Int32, _ rhs: Int32) -> Bool {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Core/Int8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension Int8: AdditiveArithmetic {
}
}

extension Int8: Numeric {
@_transparent
public static func * (_ lhs: Int8, _ rhs: Int8) -> Int8 {
let (result, overflow) =
Builtin.smul_with_overflow_Int8(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return Int8(result)
}
}

extension Int8: Comparable {
@_transparent
public static func < (_ lhs: Int8, _ rhs: Int8) -> Bool {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Core/Numeric.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © 2022 Max Desiatov <max@desiatov.com>.
// All Rights Reserved.
// SPDX-License-Identifier: BSD-3

public protocol Numeric: AdditiveArithmetic, ExpressibleByIntegerLiteral {
static func * (_ lhs: Self, _ rhs: Self) -> Self
static func *= (_ lhs: inout Self, _ rhs: Self)
}

public extension Numeric {
@_alwaysEmitIntoClient
static func *= (_ lhs: inout Self, _ rhs: Self) {
lhs = lhs * rhs
}
}
2 changes: 2 additions & 0 deletions Sources/Core/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ infix operator +: AdditionPrecedence
infix operator -: AdditionPrecedence

infix operator &: MultiplicationPrecedence
infix operator *: MultiplicationPrecedence

infix operator &=: AssignmentPrecedence
infix operator +=: AssignmentPrecedence
infix operator -=: AssignmentPrecedence
infix operator *=: AssignmentPrecedence
13 changes: 13 additions & 0 deletions Sources/Core/UInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension UInt: AdditiveArithmetic {
}
}

extension UInt: Numeric {
@_transparent
public static func * (_ lhs: UInt, _ rhs: UInt) -> UInt {
let (result, overflow) =
Builtin.umul_with_overflow_Word(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return UInt(result)
}
}

extension UInt: Comparable {
@_transparent
public static func < (_ lhs: UInt, _ rhs: UInt) -> Bool {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Core/UInt32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension UInt32: AdditiveArithmetic {
}
}

extension UInt32: Numeric {
@_transparent
public static func * (_ lhs: UInt32, _ rhs: UInt32) -> UInt32 {
let (result, overflow) =
Builtin.umul_with_overflow_Int32(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return UInt32(result)
}
}

extension UInt32: Comparable {
@_transparent
public static func < (_ lhs: UInt32, _ rhs: UInt32) -> Bool {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Core/UInt8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ extension UInt8: AdditiveArithmetic {
}
}

extension UInt8: Numeric {
@_transparent
public static func * (_ lhs: UInt8, _ rhs: UInt8) -> UInt8 {
let (result, overflow) =
Builtin.umul_with_overflow_Int8(lhs._value, rhs._value, true._value)

Builtin.condfail_message(overflow,
StaticString("arithmetic overflow")
.unsafeRawPointer)
return UInt8(result)
}
}

extension UInt8: Comparable {
@_transparent
public static func < (_ lhs: UInt8, _ rhs: UInt8) -> Bool {
Expand Down