Swift · Scalar Types

Scalar Types

import NumKong 8.0.0-rc.1 · b1f866ab

The output type is intentionally wider than the storage type for most operations. The table below documents the promotion for scalar collection extensions.

Input type.dot().angular().euclidean().sqeuclidean().hamming().jaccard()
Float64Float64Float64Float64Float64……
Float32Float64Float64Float64Float64……
BFloat16Float32Float32Float32Float32……
Float16Float32Float32Float32Float32……
Int8Int32Float32Float32UInt32……
I4x2Int32Float32Float32UInt32……
UInt8UInt32Float32Float32UInt32……
U4x2UInt32Float32Float32UInt32……
U1x8…………UInt32Float32

The matrix kernel output types follow a similar pattern but vary for the mini-float formats:

Input typeDots outputSpatial outputHamming outputJaccard output
Float64Float64Float64……
Float32Float64Float64……
BFloat16Float32Float32……
Float16Float32Float32……
E5M2Float32Float32……
E4M3Float32Float32……
E3M2Float32Float32……
E2M3Float32Float32……
Int8Int32Float32……
I4x2Int32Float32……
UInt8UInt32Float32……
U4x2UInt32Float32……
U1x8UInt32…UInt32Float32

§ Low-Precision Storage Wrappers

Swift has no built-in bf16, mini-float, or packed-bit scalar types. NumKong ships storage wrappers instead.

  • BFloat16 — 1+8+7 bit layout (sign + exponent + mantissa), 2 bytes. Same dynamic range as Float32 with reduced precision. Supports NaN and Inf.
  • E5M2 — 1+5+2 bit layout, 1 byte. Range ±57344. Supports Inf and NaN.
  • E4M3 — 1+4+3 bit layout, 1 byte. Range ±448. No Inf representation; NaN is encoded only as 0x7F or 0xFF.
  • E3M2 — 1+3+2 bit layout, 1 byte (6 bits used). Range ±28. No Inf, no NaN.
  • E2M3 — 1+2+3 bit layout, 1 byte (6 bits used). Range ±7.5. No Inf, no NaN.
  • I4x2 and U4x2 — two 4-bit lanes per byte, high nibble first. Signed lanes span -8…7, unsigned 0…15; first and second unpack them and init(first:second:) packs them, with first in the high nibble. Supports dot, angular, and Euclidean scalar and matrix kernels.
  • U1x8 — 8 packed bits per byte. Used for binary embeddings and semantic hashing. Supports Hamming and Jaccard scalar and matrix kernels.

Every floating-point wrapper provides init(bitPattern:), init(float:), and var float: Float32. All are @frozen, Equatable, Hashable, Sendable. U1x8, I4x2, and U4x2 provide init(bitPattern:) and expose their underlying UInt8 value. These wrappers are exact-storage types first. They are there to preserve bits and make the native kernels callable from Swift. They are not pretending to be standard-library numeric types.

API

StructBFloat16Brain floating-point: 16-bit storage with 8-bit exponent, used in ML inference.12
Numerics.swift:142 ↗
@frozen struct BFloat16

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongMaxSimElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, ExpressibleByFloatLiteral, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:771 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:826 ↗
typealias SpatialOutput = Float32
MaxSimOutput
Numerics.swift:135 ↗
typealias MaxSimOutput = Float32
bitPattern
Numerics.swift:143 ↗
var bitPattern: UInt16
init
Numerics.swift:146 ↗
init(bitPattern: UInt16)
init(float: Float32)
init(floatLiteral value: Double)
float
Numerics.swift:156 ↗
var float: Float32 { get }
description
Numerics.swift:248 ↗
var description: String { get }
DotOutput
Numerics.swift:190 ↗
typealias DotOutput = Float32
*× 51
Numerics.swift:193 ↗
static func dot<A, B>(
    _ a: A,
    _ b: B
) -> Float32? where A : Sequence, B : Sequence, A.Element == BFloat16, B.Element == BFloat16
Symbol
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
hamming
jaccard
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
dot
angular
euclidean
sqeuclidean
AngularOutput
Numerics.swift:204 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:218 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:232 ↗
typealias SqEuclideanOutput = Float32
StructE5M2FP8 E5M2 format with 5-bit exponent and 2-bit mantissa, used in gradient storage.10
Numerics.swift:161 ↗
@frozen struct E5M2

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, ExpressibleByFloatLiteral, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:991 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:1046 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:162 ↗
var bitPattern: UInt8
init
Numerics.swift:165 ↗
init(bitPattern: UInt8)
init(float: Float32)
init(floatLiteral value: Double)
float
Numerics.swift:175 ↗
var float: Float32 { get }
description
Numerics.swift:252 ↗
var description: String { get }
DotOutput
Numerics.swift:315 ↗
typealias DotOutput = Float32
AngularOutput
Numerics.swift:329 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:343 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:357 ↗
typealias SqEuclideanOutput = Float32
StructE4M3FP8 E4M3 format with 4-bit exponent and 3-bit mantissa, used in transformer inference.10
Numerics.swift:180 ↗
@frozen struct E4M3

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, ExpressibleByFloatLiteral, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:1100 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:1155 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:181 ↗
var bitPattern: UInt8
init
Numerics.swift:184 ↗
init(bitPattern: UInt8)
init(float: Float32)
init(floatLiteral value: Double)
float
Numerics.swift:194 ↗
var float: Float32 { get }
description
Numerics.swift:256 ↗
var description: String { get }
DotOutput
Numerics.swift:371 ↗
typealias DotOutput = Float32
AngularOutput
Numerics.swift:385 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:399 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:413 ↗
typealias SqEuclideanOutput = Float32
StructE3M2MX E3M2 format with 3-bit exponent and 2-bit mantissa.10
Numerics.swift:199 ↗
@frozen struct E3M2

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, ExpressibleByFloatLiteral, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:1209 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:1264 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:200 ↗
var bitPattern: UInt8
init
Numerics.swift:203 ↗
init(bitPattern: UInt8)
init(float: Float32)
init(floatLiteral value: Double)
float
Numerics.swift:213 ↗
var float: Float32 { get }
description
Numerics.swift:260 ↗
var description: String { get }
DotOutput
Numerics.swift:427 ↗
typealias DotOutput = Float32
AngularOutput
Numerics.swift:441 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:455 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:469 ↗
typealias SqEuclideanOutput = Float32
StructE2M3MX E2M3 format with 2-bit exponent and 3-bit mantissa.10
Numerics.swift:218 ↗
@frozen struct E2M3

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, ExpressibleByFloatLiteral, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:1318 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:1373 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:219 ↗
var bitPattern: UInt8
init
Numerics.swift:222 ↗
init(bitPattern: UInt8)
init(float: Float32)
init(floatLiteral value: Double)
float
Numerics.swift:232 ↗
var float: Float32 { get }
description
Numerics.swift:264 ↗
var description: String { get }
DotOutput
Numerics.swift:483 ↗
typealias DotOutput = Float32
AngularOutput
Numerics.swift:497 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:511 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:525 ↗
typealias SqEuclideanOutput = Float32
StructI4x2Packed pair of 4-bit signed integers, [high nibble : low nibble], each in -8...7.11
Numerics.swift:303 ↗
@frozen struct I4x2

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:1650 ↗
typealias DotsOutput = Int32
SpatialOutput
Numerics.swift:1709 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:304 ↗
var bitPattern: UInt8
initPacks two lanes, first into the high nibble, keeping the low four bits of each.
Numerics.swift:305 ↗
init(_ bits: UInt8)
init(bitPattern: UInt8)
init(first: Int8, second: Int8)

Packs two lanes, first into the high nibble, keeping the low four bits of each.

firstThe first lane, in the high nibble, sign-extended.
Numerics.swift:312 ↗
var first: Int8 { get }
secondThe second lane, in the low nibble, sign-extended.
Numerics.swift:314 ↗
var second: Int8 { get }
description
Numerics.swift:347 ↗
var description: String { get }
DotOutput
Numerics.swift:597 ↗
typealias DotOutput = Int32
AngularOutput
Numerics.swift:614 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:631 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:648 ↗
typealias SqEuclideanOutput = UInt32
StructU4x2Packed pair of 4-bit unsigned integers, [high nibble : low nibble], each in 0...15.11
Numerics.swift:319 ↗
@frozen struct U4x2

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Numerics.swift:1869 ↗
typealias DotsOutput = UInt32
SpatialOutput
Numerics.swift:1928 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:320 ↗
var bitPattern: UInt8
initPacks two lanes, first into the high nibble, keeping the low four bits of each.
Numerics.swift:321 ↗
init(_ bits: UInt8)
init(bitPattern: UInt8)
init(first: UInt8, second: UInt8)

Packs two lanes, first into the high nibble, keeping the low four bits of each.

firstThe first lane, in the high nibble.
Numerics.swift:326 ↗
var first: UInt8 { get }
secondThe second lane, in the low nibble.
Numerics.swift:328 ↗
var second: UInt8 { get }
description
Numerics.swift:351 ↗
var description: String { get }
DotOutput
Numerics.swift:723 ↗
typealias DotOutput = UInt32
AngularOutput
Numerics.swift:740 ↗
typealias AngularOutput = Float32
EuclideanOutput
Numerics.swift:757 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Numerics.swift:774 ↗
typealias SqEuclideanOutput = UInt32
StructE2M1x2Packed pair of 4-bit E2M1 floats, [high nibble : low nibble], each of 1 sign, 2 exponent and 1 mantissa bits.7
Numerics.swift:334 ↗
@frozen struct E2M1x2

Conforms to

NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible
DotsOutput
Numerics.swift:1427 ↗
typealias DotsOutput = Float32
SpatialOutput
Numerics.swift:1488 ↗
typealias SpatialOutput = Float32
bitPattern
Numerics.swift:335 ↗
var bitPattern: UInt8
initPacks two lane codes, first into the high nibble, keeping the low four bits of each.
Numerics.swift:336 ↗
init(_ bits: UInt8)
init(bitPattern: UInt8)
init(first: UInt8, second: UInt8)

Packs two lane codes, first into the high nibble, keeping the low four bits of each.

firstThe first lane code, in the high nibble.
Numerics.swift:341 ↗
var first: UInt8 { get }
secondThe second lane code, in the low nibble.
Numerics.swift:343 ↗
var second: UInt8 { get }
description
Numerics.swift:355 ↗
var description: String { get }
StructU1x8Packed 8-bit binary vector element for Hamming and Jaccard distance computations.8
Numerics.swift:362 ↗
@frozen struct U1x8

Conforms to

NumKongDotsMatrixElement, NumKongSetsMatrixElement, Equatable, Hashable, Sendable, BitwiseCopyable, CustomStringConvertible, NumKongDot, NumKongHamming, NumKongJaccard
DotsOutput
Numerics.swift:1986 ↗
typealias DotsOutput = UInt32
HammingOutput
Numerics.swift:2047 ↗
typealias HammingOutput = UInt32
JaccardOutput
Numerics.swift:2048 ↗
typealias JaccardOutput = Float32
bitPattern
Numerics.swift:363 ↗
var bitPattern: UInt8
init
Numerics.swift:364 ↗
init(_ bits: UInt8)
init(bitPattern: UInt8)
popcount
Numerics.swift:366 ↗
var popcount: Int { get }
description
Numerics.swift:370 ↗
var description: String { get }
DotOutput
Numerics.swift:791 ↗
typealias DotOutput = UInt32
StructDouble8
extension Double : NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongHaversines, NumKongVincentys, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
haversines
NumKong.swift:77 ↗
static func haversines(
    aLatitudes: UnsafeBufferPointer<Float64>,
    aLongitudes: UnsafeBufferPointer<Float64>,
    bLatitudes: UnsafeBufferPointer<Float64>,
    bLongitudes: UnsafeBufferPointer<Float64>,
    results: UnsafeMutableBufferPointer<Float64>
) -> Bool
static func haversines<A, B, C, D>(
    aLatitudes: A,
    aLongitudes: B,
    bLatitudes: C,
    bLongitudes: D
) -> [Float64]? where A : Sequence, B : Sequence, C : Sequence, D : Sequence, A.Element == Double, B.Element == Double, C.Element == Double, D.Element == Double
vincentys
NumKong.swift:95 ↗
static func vincentys(
    aLatitudes: UnsafeBufferPointer<Float64>,
    aLongitudes: UnsafeBufferPointer<Float64>,
    bLatitudes: UnsafeBufferPointer<Float64>,
    bLongitudes: UnsafeBufferPointer<Float64>,
    results: UnsafeMutableBufferPointer<Float64>
) -> Bool
static func vincentys<A, B, C, D>(
    aLatitudes: A,
    aLongitudes: B,
    bLatitudes: C,
    bLongitudes: D
) -> [Float64]? where A : Sequence, B : Sequence, C : Sequence, D : Sequence, A.Element == Double, B.Element == Double, C.Element == Double, D.Element == Double
DotsOutput
NumKong.swift:567 ↗
typealias DotsOutput = Float64
SpatialOutput
NumKong.swift:619 ↗
typealias SpatialOutput = Float64
DotOutput
NumKong.swift:78 ↗
typealias DotOutput = Float64
AngularOutput
NumKong.swift:92 ↗
typealias AngularOutput = Float64
EuclideanOutput
NumKong.swift:106 ↗
typealias EuclideanOutput = Float64
SqEuclideanOutput
NumKong.swift:120 ↗
typealias SqEuclideanOutput = Float64
StructFloat7
extension Float : NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongMaxSimElement, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Matrix.swift:669 ↗
typealias DotsOutput = Float64
SpatialOutput
Matrix.swift:721 ↗
typealias SpatialOutput = Float64
MaxSimOutput
Matrix.swift:92 ↗
typealias MaxSimOutput = Float64
DotOutput
Matrix.swift:134 ↗
typealias DotOutput = Float64
AngularOutput
Matrix.swift:148 ↗
typealias AngularOutput = Float64
EuclideanOutput
Matrix.swift:162 ↗
typealias EuclideanOutput = Float64
SqEuclideanOutput
Matrix.swift:176 ↗
typealias SqEuclideanOutput = Float64
StructFloat167
extension Float16 : NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongMaxSimElement, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Matrix.swift:881 ↗
typealias DotsOutput = Float32
SpatialOutput
Matrix.swift:936 ↗
typealias SpatialOutput = Float32
MaxSimOutput
Matrix.swift:180 ↗
typealias MaxSimOutput = Float32
DotOutput
Matrix.swift:250 ↗
typealias DotOutput = Float32
AngularOutput
Matrix.swift:266 ↗
typealias AngularOutput = Float32
EuclideanOutput
Matrix.swift:282 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Matrix.swift:298 ↗
typealias SqEuclideanOutput = Float32
StructInt86
extension Int8 : NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Matrix.swift:1548 ↗
typealias DotsOutput = Int32
SpatialOutput
Matrix.swift:1600 ↗
typealias SpatialOutput = Float32
DotOutput
Matrix.swift:541 ↗
typealias DotOutput = Int32
AngularOutput
Matrix.swift:555 ↗
typealias AngularOutput = Float32
EuclideanOutput
Matrix.swift:569 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Matrix.swift:583 ↗
typealias SqEuclideanOutput = UInt32
StructUInt86
extension UInt8 : NumKongDotsMatrixElement, NumKongSpatialsMatrixElement, NumKongDot, NumKongAngular, NumKongEuclidean, NumKongSqEuclidean
DotsOutput
Matrix.swift:1767 ↗
typealias DotsOutput = UInt32
SpatialOutput
Matrix.swift:1819 ↗
typealias SpatialOutput = Float32
DotOutput
Matrix.swift:667 ↗
typealias DotOutput = UInt32
AngularOutput
Matrix.swift:681 ↗
typealias AngularOutput = Float32
EuclideanOutput
Matrix.swift:695 ↗
typealias EuclideanOutput = Float32
SqEuclideanOutput
Matrix.swift:709 ↗
typealias SqEuclideanOutput = UInt32