From 30c01a9fc5a92cca09686e7621704e26b7ef9842 Mon Sep 17 00:00:00 2001 From: "Steve (Numerics) Canon" Date: Fri, 26 Nov 2021 22:04:55 -0500 Subject: [PATCH] Possible spelling: Relaxed.sum/product. --- .../Complex+AlgebraicField.swift | 13 +++ .../Complex+ElementaryFunctions.swift | 4 +- Sources/RealModule/AlgebraicField.swift | 31 +++++-- Sources/RealModule/CMakeLists.txt | 3 +- Sources/RealModule/Real.swift | 10 --- Sources/RealModule/RealFunctions.swift | 48 ----------- Sources/RealModule/RelaxedArithmetic.swift | 83 +++++++++++++++++++ 7 files changed, 126 insertions(+), 66 deletions(-) create mode 100644 Sources/RealModule/RelaxedArithmetic.swift diff --git a/Sources/ComplexModule/Complex+AlgebraicField.swift b/Sources/ComplexModule/Complex+AlgebraicField.swift index be8a2aa..c1ff5e4 100644 --- a/Sources/ComplexModule/Complex+AlgebraicField.swift +++ b/Sources/ComplexModule/Complex+AlgebraicField.swift @@ -123,4 +123,17 @@ extension Complex: AlgebraicField { } return nil } + + @inlinable + public static func _relaxedAdd(_ a: Self, _ b: Self) -> Self { + Complex(Relaxed.sum(a.x, b.x), Relaxed.sum(a.y, b.y)) + } + + @inlinable + public static func _relaxedMul(_ a: Self, _ b: Self) -> Self { + Complex( + Relaxed.sum(Relaxed.product(a.x, b.x), -Relaxed.product(a.y, b.y)), + Relaxed.sum(Relaxed.product(a.x, b.y), Relaxed.product(a.y, b.x)) + ) + } } diff --git a/Sources/ComplexModule/Complex+ElementaryFunctions.swift b/Sources/ComplexModule/Complex+ElementaryFunctions.swift index f6ef2f4..6c75e4b 100644 --- a/Sources/ComplexModule/Complex+ElementaryFunctions.swift +++ b/Sources/ComplexModule/Complex+ElementaryFunctions.swift @@ -119,7 +119,7 @@ extension Complex: ElementaryFunctions { } // Special cases out of the way, evaluate as discussed above. return Complex( - RealType._mulAdd(.cos(z.y), .expMinusOne(z.x), .cosMinusOne(z.y)), + Relaxed.multiplyAdd(.cos(z.y), .expMinusOne(z.x), .cosMinusOne(z.y)), .exp(z.x) * .sin(z.y) ) } @@ -300,7 +300,7 @@ extension Complex: ElementaryFunctions { // We are not trying for sub-ulp accuracy, just a good relative error // bound, so for our purposes it suffices to have log u dominate the // result: - if u >= 1 || u >= RealType._mulAdd(u,u,v*v) { + if u >= 1 || u >= Relaxed.multiplyAdd(u, u, v*v) { let r = v / u return Complex(.log(u) + .log(onePlus: r*r)/2, θ) } diff --git a/Sources/RealModule/AlgebraicField.swift b/Sources/RealModule/AlgebraicField.swift index 7a231e8..4c8070d 100644 --- a/Sources/RealModule/AlgebraicField.swift +++ b/Sources/RealModule/AlgebraicField.swift @@ -89,6 +89,12 @@ public protocol AlgebraicField: SignedNumeric { /// } /// ``` var reciprocal: Self? { get } + + /// a + b, with the optimizer licensed to reassociate and form FMAs. + static func _relaxedAdd(_ a: Self, _ b: Self) -> Self + + /// a * b, with the optimizer licensed to reassociate and form FMAs. + static func _relaxedMul(_ a: Self, _ b: Self) -> Self } extension AlgebraicField { @@ -99,12 +105,27 @@ extension AlgebraicField { return result } - /// Implementations should be *conservative* with the reciprocal property; - /// it is OK to return `nil` even in cases where a reciprocal could be - /// represented. For this reason, a default implementation that simply - /// always returns `nil` is correct, but conforming types should provide - /// a better implementation if possible. + // Implementations should be *conservative* with the reciprocal property; + // it is OK to return `nil` even in cases where a reciprocal could be + // represented. For this reason, a default implementation that simply + // always returns `nil` is correct, but conforming types should provide + // a better implementation if possible. + @_transparent public var reciprocal: Self? { return nil } + + // It's always OK to simply fall back on normal arithmetic, and for any + // field with exact arithmetic, this is the correct definition. + @_transparent + public static func _relaxedAdd(_ a: Self, _ b: Self) -> Self { + a + b + } + + // It's always OK to simply fall back on normal arithmetic, and for any + // field with exact arithmetic, this is the correct definition. + @_transparent + public static func _relaxedMul(_ a: Self, _ b: Self) -> Self { + a * b + } } diff --git a/Sources/RealModule/CMakeLists.txt b/Sources/RealModule/CMakeLists.txt index c074dcf..acc78da 100644 --- a/Sources/RealModule/CMakeLists.txt +++ b/Sources/RealModule/CMakeLists.txt @@ -17,7 +17,8 @@ add_library(RealModule Float16+Real.swift Float80+Real.swift Real.swift - RealFunctions.swift) + RealFunctions.swift + RelaxedArithmetic.swift) set_target_properties(RealModule PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) target_link_libraries(RealModule PUBLIC diff --git a/Sources/RealModule/Real.swift b/Sources/RealModule/Real.swift index e38590c..ec363df 100644 --- a/Sources/RealModule/Real.swift +++ b/Sources/RealModule/Real.swift @@ -88,16 +88,6 @@ extension Real { a*b + c } - @_transparent - public static func _relaxedAdd(_ a: Self, _ b: Self) -> Self { - a + b - } - - @_transparent - public static func _relaxedMul(_ a: Self, _ b: Self) -> Self { - a * b - } - @_transparent public static func sqrt(_ x: Self) -> Self { return x.squareRoot() diff --git a/Sources/RealModule/RealFunctions.swift b/Sources/RealModule/RealFunctions.swift index 0a8ab12..4e77543 100644 --- a/Sources/RealModule/RealFunctions.swift +++ b/Sources/RealModule/RealFunctions.swift @@ -80,52 +80,4 @@ public protocol RealFunctions: ElementaryFunctions { /// a*b + c, computed _either_ with an FMA or with separate multiply and add, /// whichever is fastest on the compilation target. static func _mulAdd(_ a: Self, _ b: Self, _ c: Self) -> Self - - /// a + b, with the optimizer licensed to reassociate and form FMAs. - /// - /// Floating-point addition is not an associative operation, so the Swift - /// compiler does not have any flexibility in how it evaluates an expression - /// like: - /// ``` - /// func sum(array: [Float]) -> Float { - /// array.reduce(0, +) - /// } - /// ``` - /// Using `_relaxedAdd` instead of `+` permits the compiler to reorder the - /// terms in the summation, which unlocks loop unrolling and vectorization. - /// In a benchmark, simply using `_relaxedAdd` provides about an 8x speedup - /// for release builds, without any unsafe flags or other optimizations. - /// Further improvement should be possible by improving LLVM optimizations - /// or adding attributes to license more aggressive unrolling and taking - /// advantage of vector ISA extensions for swift. - /// - /// If a type or toolchain does not support reassociation for optimization - /// purposes, this operation decays to a normal addition; it is a license - /// for the compiler to optimize, not a guarantee that any change occurs. - static func _relaxedAdd(_ a: Self, _ b: Self) -> Self - - /// a * b, with the optimizer licensed to reassociate and form FMAs. - /// - /// Floating-point addition and multiplication are not associative operations, - /// so the Swift compiler does not have any flexibility in how it evaluates - /// an expression - /// like: - /// ``` - /// func sumOfSquares(array: [Float]) -> Float { - /// array.reduce(0) { $0 + $1*$1 } - /// } - /// ``` - /// Using `_relaxedAdd` and `_relaxedMul` instead of `+` and `*` permits the - /// compiler to reorder the terms in the summation, which unlocks loop - /// unrolling and vectorization, and form fused multiply-adds, which allows - /// us to achieve twice the throughput on some hardware. - /// - /// If you want to license FMA formation, but _not_ reassociation (desirable - /// for some numerics tasks), use `_mulAdd(a, b, c)` instead. - /// - /// If a type or toolchain does not support reassociation for optimization - /// purposes, this operation decays to a normal multiplication; it is a - /// license for the compiler to optimize, not a guarantee that any change - /// occurs. - static func _relaxedMul(_ a: Self, _ b: Self) -> Self } diff --git a/Sources/RealModule/RelaxedArithmetic.swift b/Sources/RealModule/RelaxedArithmetic.swift new file mode 100644 index 0000000..58890d1 --- /dev/null +++ b/Sources/RealModule/RelaxedArithmetic.swift @@ -0,0 +1,83 @@ +//===--- RelaxedArithmetic.swift ------------------------------*- swift -*-===// +// +// This source file is part of the Swift Numerics open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import _NumericsShims + +/// A namespace for "relaxed arithmetic" operations for types conforming to +/// `AlgebraicField`. +public enum Relaxed { } + +extension Relaxed { + /// a+b with the optimizer licensed to reassociate expressions and form FMAs. + /// + /// Floating-point addition is not an associative operation, so the Swift + /// compiler does not have any flexibility in how it evaluates an expression + /// like: + /// ``` + /// func sum(array: [Float]) -> Float { + /// array.reduce(0, +) + /// } + /// ``` + /// Using `Relaxed.sum` instead of `+` permits the compiler to reorder the + /// terms in the summation, which unlocks loop unrolling and vectorization. + /// In a benchmark, simply using `Relaxed.sum` provides about an 8x speedup + /// for release builds, without any unsafe flags or other optimizations. + /// Further improvement should be possible by improving LLVM optimizations + /// or adding attributes to license more aggressive unrolling and taking + /// advantage of vector ISA extensions for swift. + /// + /// If you want to compute `a-b` with relaxed semantics, use + /// `Relaxed.sum(a, -b)`. + /// + /// If a type or toolchain does not support reassociation for optimization + /// purposes, this operation decays to a normal addition; it is a license + /// for the compiler to optimize, not a guarantee that any change occurs. + @_transparent + public static func sum(_ a: T, _ b: T) -> T { + T._relaxedAdd(a, b) + } + + /// a*b with the optimizer licensed to reassociate expressions and form FMAs. + /// + /// Floating-point addition and multiplication are not associative operations, + /// so the Swift compiler does not have any flexibility in how it evaluates + /// an expression + /// like: + /// ``` + /// func sumOfSquares(array: [Float]) -> Float { + /// array.reduce(0) { $0 + $1*$1 } + /// } + /// ``` + /// Using `Relaxed.sum` and `Relaxed.product` instead of `+` and `*` permits + /// the compiler to reorder the terms in the summation, which unlocks loop + /// unrolling and vectorization, and form fused multiply-adds, which allows + /// us to achieve twice the throughput on some hardware. + /// + /// If a type or toolchain does not support reassociation for optimization + /// purposes, this operation decays to a normal multiplication; it is a + /// license for the compiler to optimize, not a guarantee that any change + /// occurs. + @_transparent + public static func product(_ a: T, _ b: T) -> T { + T._relaxedMul(a, b) + } +} + +extension Relaxed { + /// a*b + c, computed _either_ with an FMA or with separate multiply and add, + /// whichever is fastest according to the optimizer's heuristics. + @_transparent + public static func multiplyAdd( + _ a: T, _ b: T, _ c: T + ) -> T { + T._mulAdd(a, b, c) + } +}