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

Add a few missing methods to the API of Int8 #1060

Merged
merged 8 commits into from
Oct 4, 2023
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
4 changes: 2 additions & 2 deletions Library/Hylo/Core/Numbers/Integers/Int.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
105 changes: 102 additions & 3 deletions Library/Hylo/Core/Numbers/Integers/Int8.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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<T: BinaryInteger>(truncating_or_extending source: T) {
let w = source.words()
&self.value = Builtin.trunc_word_i8(w[w.start_index()].value)
}

}
30 changes: 30 additions & 0 deletions Library/Hylo/Core/Numbers/Integers/UInt8.hylo
Original file line number Diff line number Diff line change
@@ -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)
}

}
Loading