diff --git a/Library/Hylo/Core/Numbers/Integers/Int.hylo b/Library/Hylo/Core/Numbers/Integers/Int.hylo index e05b27dd1..38b2fd7ca 100644 --- a/Library/Hylo/Core/Numbers/Integers/Int.hylo +++ b/Library/Hylo/Core/Numbers/Integers/Int.hylo @@ -30,10 +30,10 @@ public type Int { if r == 0 { return self.copy() } else if r < 0 { - return -(abs() - r); + return -(abs() - r) } - return self + stride - r; + return self + stride - r } /// Returns `self`. diff --git a/Library/Hylo/Core/Numbers/Integers/Int8.hylo b/Library/Hylo/Core/Numbers/Integers/Int8.hylo index 41d32b01f..b9061ca44 100644 --- a/Library/Hylo/Core/Numbers/Integers/Int8.hylo +++ b/Library/Hylo/Core/Numbers/Integers/Int8.hylo @@ -5,9 +5,9 @@ public type Int8 { memberwise init - /// Creates an instance with value `0`. - public init() { - &self.value = Builtin.zeroinitializer_i8() + /// Creates an instance with the same memory representation as `other`. + public init(bit_pattern other: UInt8) { + &self.value = other.value } /// Creates a copy of `other`. @@ -30,3 +30,102 @@ public conformance Int8: Copyable { } } + +public conformance Int8: Equatable { + + public fun infix== (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_eq_i8(value, other.value)) + } + + public fun infix!= (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_ne_i8(value, other.value)) + } + +} + +public conformance Int8: Comparable { + + public fun infix< (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_slt_i8(value, other.value)) + } + + public fun infix<= (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_sle_i8(value, other.value)) + } + + public fun infix> (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_sgt_i8(value, other.value)) + } + + public fun infix>= (_ other: Self) -> Bool { + Bool(value: Builtin.icmp_sge_i8(value, other.value)) + } + +} + +public conformance Int8: AdditiveArithmetic { + + public fun infix+ (_ other: Self) -> Self { + Int8(value: Builtin.add_i8(value, other.value)) + } + + public fun infix+= (_ other: Self) inout { + &self.value = Builtin.add_i8(value, other.value) + } + + public fun infix- (_ other: Self) -> Self { + Int8(value: Builtin.sub_i8(value, other.value)) + } + + public fun infix-= (_ other: Self) inout { + &self.value = Builtin.sub_i8(value, other.value) + } + + public static fun zero() -> Self { + Int8() + } + +} + +public conformance Int8: Numeric { + + public typealias Magnitude = UInt8 + + public fun magnitude() -> UInt8 { + UInt8(bit_pattern: self) + } + + public fun infix* (_ other: Self) -> Self { + Int8(value: Builtin.mul_i8(value, other.value)) + } + + public fun infix*= (_ other: Self) inout { + &self.value = Builtin.mul_i8(value, other.value) + } + +} + +public conformance Int8: SignedNumeric { + + public fun prefix- () -> Self { + Int8() - self + } + + public fun negate() inout { + &self = -self + } + +} + +public extension Int8 { + + public init() { + &self.value = Builtin.zeroinitializer_i8() + } + + public init(truncating_or_extending source: T) { + let w = source.words() + &self.value = Builtin.trunc_word_i8(w[w.start_index()].value) + } + +} diff --git a/Library/Hylo/Core/Numbers/Integers/UInt8.hylo b/Library/Hylo/Core/Numbers/Integers/UInt8.hylo new file mode 100644 index 000000000..cee1987a0 --- /dev/null +++ b/Library/Hylo/Core/Numbers/Integers/UInt8.hylo @@ -0,0 +1,30 @@ +/// A 8-bit unsigned integer value. +public type UInt8 { + + var value: Builtin.i8 + + memberwise init + + /// Creates an instance with value `0`. + public init() { + &self.value = Builtin.zeroinitializer_i8() + } + + /// Creates an instance with the same memory representation as `other`. + public init(bit_pattern other: Int8) { + &self.value = other.value + } + +} + +public conformance UInt8: ExpressibleByIntegerLiteral {} + +public conformance UInt8: Deinitializable {} + +public conformance UInt8: Copyable { + + public fun copy() -> Self { + UInt8(value: value) + } + +}