APInt

public struct APInt : IRConstant
extension APInt: Comparable
extension APInt: Numeric

A value implementing arbitrary precision integer arithmetic.

APInt is a functional replacement for common case unsigned integer type like UInt or UInt64, but also allows non-byte-width integer sizes and large integer value types such as 3-bits, 15-bits, or more than 64-bits of precision. APInt provides a variety of arithmetic operators and methods to manipulate integer values of any bit-width. It supports both the typical integer arithmetic and comparison operations as well as bitwise manipulation.

  • The underlying word size.

    Declaration

    Swift

    public typealias Word = UInt64
  • The bitwidth of this integer.

    Declaration

    Swift

    public private(set) var bitWidth: Int { get }
  • Declaration

    Swift

    public func asLLVM() -> LLVMValueRef
  • Creates and initializes a new APInt with a bitwidth of one and a value of zero.

    Declaration

    Swift

    public init()
  • Creates and initializes a new APInt with a given bit width, value, and signedness.

    Declaration

    Swift

    public init(width numBits: Int, value: UInt64, signed: Bool = false)

    Parameters

    numBits

    The bit width of the integer.

    value

    The value of the integer.

    signed

    If true, treat the most significant bit at the given bit width as a sign bit. Defaults to false.

  • Creates and initializes a new APInt with a given bit width and an array of word values.

    Declaration

    Swift

    public init(width numBits: Int, values: [Word])

    Parameters

    numBits

    The bit width of the integer.

    values

    An array of words to form the value of the integer.

Comparisons

  • Declaration

    Swift

    public static func == (lhs: APInt, rhs: APInt) -> Bool
  • Declaration

    Swift

    public static func < (lhs: APInt, rhs: APInt) -> Bool

Arithmetic Operations

  • Declaration

    Swift

    public typealias Magnitude = APInt
  • Declaration

    Swift

    public typealias IntegerLiteralType = UInt64
  • Declaration

    Swift

    public init(integerLiteral value: UInt64)
  • Declaration

    Swift

    public init?<T>(exactly source: T) where T : BinaryInteger
  • Returns the result of performing a bitwise AND operation on the two given values.

    Declaration

    Swift

    public static func & (lhs: APInt, rhs: APInt) -> APInt
  • Stores the result of performing a bitwise AND operation on the two given values in the left-hand-side variable.

    Declaration

    Swift

    public static func &= (lhs: inout APInt, rhs: APInt)
  • Returns the result of performing a bitwise OR operation on the two given values.

    Declaration

    Swift

    public static func | (lhs: APInt, rhs: APInt) -> APInt
  • Stores the result of performing a bitwise OR operation on the two given values in the left-hand-side variable.

    Declaration

    Swift

    public static func |= (lhs: inout APInt, rhs: APInt)
  • Returns the result of performing a bitwise XOR operation on the two given values.

    Declaration

    Swift

    public static func ^ (lhs: APInt, rhs: APInt) -> APInt
  • Stores the result of performing a bitwise XOR operation on the two given values in the left-hand-side variable.

    Declaration

    Swift

    public static func ^= (lhs: inout APInt, rhs: APInt)
  • Declaration

    Swift

    public static func + (lhs: APInt, rhs: APInt) -> APInt
  • Declaration

    Swift

    public static func += (lhs: inout APInt, rhs: APInt)
  • Declaration

    Swift

    public static func - (lhs: APInt, rhs: APInt) -> APInt
  • Declaration

    Swift

    public static func -= (lhs: inout APInt, rhs: APInt)
  • Declaration

    Swift

    public var magnitude: APInt { get }
  • Declaration

    Swift

    public static func * (lhs: APInt, rhs: APInt) -> APInt
  • Declaration

    Swift

    public static func *= (lhs: inout APInt, rhs: APInt)
  • Returns the result of shifting a value’s binary representation the specified number of digits to the left.

    Declaration

    Swift

    public static func << (lhs: APInt, amount: UInt64) -> APInt
  • Stores the result of shifting a value’s binary representation the specified number of digits to the left in the left-hand-side variable.

    Declaration

    Swift

    public static func <<= (lhs: inout APInt, amount: UInt64)
  • Logically shift the value right by the given amount.

    Declaration

    Swift

    public func logicallyShiftedRight(by amount: UInt64) -> APInt

    Parameters

    amount

    The amount to shift right by.

    Return Value

    An integer of the same bit width that has been logically right shifted.

  • Logically shift the value right by the given amount.

    Declaration

    Swift

    public mutating func logicallyShiftRight(by amount: UInt64)

    Parameters

    amount

    The amount to shift right by.

  • Attempts to return the value of this integer as a 64-bit unsigned integer that has been zero-extended. If the value cannot be represented faithfully in 64 bits, the result is nil.

    Declaration

    Swift

    public var zeroExtendedValue: UInt64? { get }
  • Attempts to return the value of this integer as a 64-bit signed integer that has been sign-extended. If the value cannot be rerprresented faithfully in 64 bits, the result is nil.

    Declaration

    Swift

    public var signExtendedValue: Int64? { get }
  • Returns true if the most significant bit of this value is set.

    Note: This may return true even if you did not explicitly construct a signed APInt.

    Declaration

    Swift

    public var isNegative: Bool { get }
  • Returns true if all bits are ones.

    Declaration

    Swift

    public var areAllBitsSet: Bool { get }
  • The number of leading zeros in this value’s binary representation.

    Declaration

    Swift

    public var leadingZeroBitCount: Int { get }
  • The number of leading ones in this value’s binary representation.

    Declaration

    Swift

    public var leadingNonZeroBitCount: Int { get }
  • The number of trailing zeros in this value’s binary representation.

    Declaration

    Swift

    public var trailingZeroBitCount: Int { get }
  • The number of trailing ones in this value’s binary representation.

    Declaration

    Swift

    public var trailingNonZeroBitCount: Int { get }
  • The number of bits equal to 1 in this value’s binary representation.

    Declaration

    Swift

    public var nonzeroBitCount: Int { get }

Bit Twiddling Operations

  • Sets all bits to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setAllBits()
  • Sets the bit at the given position to one.

    Declaration

    Swift

    public mutating func setBit(at position: Int)

    Parameters

    position

    The position of the bit to set.

  • Sets the sign bit to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setSignBit()
  • Sets all bits in the given range to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setBits(_ range: ClosedRange<Int>)

    Parameters

    range

    The range of bits to flip.

  • Sets all bits in the given range to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setBits(_ range: Range<Int>)

    Parameters

    range

    The range of bits to flip.

  • Sets all bits in the given range to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setBits(_ range: PartialRangeUpTo<Int>)

    Parameters

    range

    The range of bits to flip.

  • Sets all bits in the given range to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setBits(_ range: PartialRangeThrough<Int>)

    Parameters

    range

    The range of bits to flip.

  • Sets all bits in the given range to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func setBits(_ range: PartialRangeFrom<Int>)

    Parameters

    range

    The range of bits to flip.

  • Clears all bits to one in this value’s binary representation.

    Declaration

    Swift

    public mutating func clearAllBits()
  • Sets the bit at the given position to zero.

    Declaration

    Swift

    public mutating func clearBit(_ position: Int)

    Parameters

    position

    The position of the bit to zero.

  • Clears the sign bit in this value’s binary representation.

    Declaration

    Swift

    public mutating func clearSignBit()
  • Truncates the integer to a given bit width.

    Declaration

    Swift

    public func truncate(to width: Int) -> APInt

    Parameters

    width

    The new width to truncate towards. This width cannot exceed the current width of the integer.

    Return Value

    An integer of the given bit width that has been truncated.

  • Zero extends the integer to a given bit width.

    Declaration

    Swift

    public func zeroExtend(to width: Int) -> APInt

    Parameters

    width

    The width to zero-extend the integer to. This value may not be smaller than the current width of the integer.

    Return Value

    An integer of the given bit width that has been zero-extended.

  • Sign extends the integer to a given bit width.

    Declaration

    Swift

    public func signExtend(to width: Int) -> APInt

    Parameters

    width

    The width to sign-extend the integer to. This value may not be smaller than the current width of the integer.

    Return Value

    An integer of the given bit width that has been zero-extended.

  • Zero extends or truncates the integer to the given width.

    If the given bit width exceeds the current bit width, the value is zero-extended. Else if the given bit width is less than the current bit width, the value is truncated.

    If the given bit width matches the current bit width, this operation is a no-op.

    Declaration

    Swift

    public func zeroExtendOrTruncate(to width: Int) -> APInt

    Parameters

    width

    The width to zero extend or truncate to.

    Return Value

    An integer that has been zero extended or truncated to fit the given bit width.

  • Sign extends or truncates the integer to the given width.

    If the given bit width exceeds the current bit width, the value is sign-extended. Else if the given bit width is less than the current bit width, the value is truncated.

    If the given bit width matches the current bit width, this operation is a no-op.

    Declaration

    Swift

    public func signExtendOrTruncate(to width: Int) -> APInt

    Parameters

    width

    The width to sign extend or truncate to.

    Return Value

    An integer that has been sign extended or truncated to fit the given bit width.

  • Toggle every bit in this integer to its opposite value.

    Declaration

    Swift

    public mutating func flipAll()
  • Computes the complement integer value.

    Declaration

    Swift

    public prefix static func ~ (val: APInt) -> APInt

    Parameters

    val

    The integer value.

    Return Value

    An integer with the value of the bitwise complement.