diff --git a/.github/workflows/amalgamate.yml b/.github/workflows/amalgamate.yml index 0a40ff4..d18c08e 100644 --- a/.github/workflows/amalgamate.yml +++ b/.github/workflows/amalgamate.yml @@ -33,8 +33,14 @@ jobs: steps: - uses: actions/checkout@v6 + # Boost.Int128 is inlined into the single header since no Boost release before 1.93 ships it. + # Its develop branch is used because that is what safe_numbers develop is tested against. + # It is cloned outside deps/ so the smoke test proves the header does not need it on the include path. + - name: Fetch Boost.Int128 + run: git clone --depth 1 --branch develop --quiet https://github.com/boostorg/int128.git "${RUNNER_TEMP}/int128" + - name: Regenerate the single header - run: python3 extra/amalgamate.py + run: python3 extra/amalgamate.py --int128-include "${RUNNER_TEMP}/int128/include" # The single header still needs these Boost libraries, as on Compiler Explorer. # Released (master) versions are used so the smoke test matches what users get. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 83cf511..4366912 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,7 @@ jobs: name: Run Boost.CI uses: boostorg/boost-ci/.github/workflows/reusable.yml@master with: + timeout: 180 # Minutes per job (Boost.CI default is 120) exclude_compiler: 'clang-3.5,clang-3.6,clang-3.7,clang-3.8,clang-3.9,clang-4.0,clang-5.0,clang-6.0,clang-7, clang-8,clang-9,clang-10,clang-11,clang-12, gcc-4.7,gcc-4.8,gcc-4.9,gcc-5,gcc-6,gcc-7,gcc-8,gcc-9,gcc-10' diff --git a/doc/modules/ROOT/pages/api_reference.adoc b/doc/modules/ROOT/pages/api_reference.adoc index 05d2a4e..59f30d7 100644 --- a/doc/modules/ROOT/pages/api_reference.adoc +++ b/doc/modules/ROOT/pages/api_reference.adoc @@ -456,16 +456,16 @@ These wrap the standard `` functions for the floating-point types `f32` a | Function | Description | xref:simd.adoc[`saturating_*`, `overflowing_*`, `checked_*`, `strict_*`] -| The scalar free function families (`add`, `sub`, `mul`, `div`, `mod`, and for unsigned elements `shl`, `shr`) applied lane by lane +| The scalar free function families (`add`, `sub`, `mul`, `div`, `mod`, and for unsigned elements `shl`, `shr`) applied lane by lane; floating point elements have only `overflowing_add`, `overflowing_sub`, `overflowing_mul`, and `overflowing_div`, as for the scalar types | xref:simd.adoc[`widening_add`, `widening_mul`] -| Exact results in lanes twice as wide +| Exact results in lanes twice as wide (integer elements) | xref:simd.adoc[`add`, `sub`, `mul`, `div`, `mod`, `shl`, `shr`] -| Vector operations with the policy chosen at the call site (`shl` and `shr` for unsigned elements only) +| Vector operations with the policy chosen at the call site (integer elements; `shl` and `shr` for unsigned elements only) | xref:simd.adoc[`select`, `min`, `max`, `clamp`] -| Lane by lane selection and ordering +| Lane by lane selection and ordering, following `std::min`, `std::max`, and `std::clamp` for every lane | xref:simd.adoc[`reduce`, `reduce_min`, `reduce_max`] | Reductions; `reduce` folds left in lane order with the element's operation diff --git a/doc/modules/ROOT/pages/simd.adoc b/doc/modules/ROOT/pages/simd.adoc index 46855cb..046ae58 100644 --- a/doc/modules/ROOT/pages/simd.adoc +++ b/doc/modules/ROOT/pages/simd.adoc @@ -24,7 +24,7 @@ The header is opt-in and is not part of the convenience header ` ---- -This release supports the unsigned element types `u8`, `u16`, `u32`, `u64`, the signed element types `i8`, `i16`, `i32`, `i64`, and their policy forms (`sat_u32`, `strict_i64`, `basic_i32`, and so on). +This release supports the unsigned element types `u8`, `u16`, `u32`, `u64`, the signed element types `i8`, `i16`, `i32`, `i64`, the floating point element types `f32` and `f64`, and their policy forms (`sat_u32`, `strict_i64`, `basic_i32`, `sat_f32`, `basic_f64`, and so on). == The Semantic Rule @@ -32,7 +32,7 @@ For every policy, lane `i` of a vector operation equals the scalar operation on When the scalar loop over `i = 0 ... N - 1` would throw or exit, the vector operation throws the same exception type with the same message as the *lowest* failing lane, and produces no result. * `throw_exception` elements throw exactly what the scalar type throws. -* `saturate` elements clamp every failing lane, branch free. Division and modulo by zero still throw `std::domain_error`, as in the scalar types. +* `saturate` integer elements clamp every failing lane, branch free. Division and modulo by zero still throw `std::domain_error`, as in the scalar types. `saturate` floating point elements (`sat_f32`, `sat_f64`) are raw IEEE 754 arithmetic with no check at all, as in the scalar types. * `strict` elements call `std::exit(EXIT_FAILURE)`. * User defined handlers are called once for every failing lane, in lane order, with exactly the arguments the scalar type passes, and their return values become those lanes. @@ -40,6 +40,7 @@ When the error mask has any lane set, the vector replays the scalar operation la The exception classification, the messages, and the handler arguments therefore come from the scalar types themselves. Compound assignment and `++`/`--` compute the result before storing it, so a vector that throws is left unchanged. +Neither exists for floating point elements, because the scalar `f32` and `f64` deliberately provide neither. == Backends @@ -70,6 +71,10 @@ On the `std::simd` backend (GCC 16, x86-64, `-O2`), checked addition, subtractio With AVX-512, GCC 16's `` pads a vector whose lane count is not a power of two with zero lanes, and its `/` and `%` may divide those lanes too, which raises `SIGFPE`. `simd::vec` divides lane by lane in exactly that configuration, and uses the vector division everywhere else. +GCC 16's `` also skips the blend in `select` when one arm is a compile time constant equal to zero, but it tests that with `==`, so an arm of `-0.0` lanes comes back as `+0.0`. +The same happens in `min`, `max`, `clamp`, `reduce_min`, and `reduce_max`, which select internally. +`simd::vec` therefore selects floating point lanes by their bit patterns, in unsigned lanes of the same size, which takes the same number of blend instructions. + NOTE: GCC 16's `` returns 32-byte vectors from internal helpers when AVX is not enabled, which `-Wpsabi` reports at the end of the translation unit even for plain `std::simd` code. Under `-Werror`, add `-Wno-psabi` or `#pragma GCC diagnostic ignored "-Wpsabi"` to translation units that use vectors of 32 bytes or more (including the 64-bit lanes the 32-bit multiplication check widens into). @@ -119,10 +124,12 @@ public: // Read only lane access. Precondition: 0 <= i < N constexpr T operator[](int i) const noexcept; - // Compound assignment for + - * / %, and for unsigned elements & | ^ << >>, with a vec or a T + // Compound assignment for + - * / %, and for unsigned elements & | ^ << >>, with a vec or a T. + // Integer elements only. template constexpr vec& operator+=(const U& rhs); // ... + // Integer elements only constexpr vec& operator++(); constexpr vec operator++(int); constexpr vec& operator--(); @@ -130,7 +137,7 @@ public: constexpr vec operator+() const noexcept; }; -// Arithmetic: + - * / % with the element's error policy +// Arithmetic: + - * / with the element's error policy, and % for integer elements // Unsigned elements only: << >> and the bitwise & | ^ ~ // Signed elements only: unary - // Comparisons: == != < <= > >= return mask_type @@ -141,23 +148,25 @@ template constexpr vec saturating_add(const vec& template constexpr std::pair, typename vec::mask_type> overflowing_add(const vec&, const vec&) noexcept; template constexpr std::optional> checked_add(const vec&, const vec&) noexcept; template constexpr vec strict_add(const vec&, const vec&) noexcept; -// ... and likewise for sub, mul, div, mod, and for unsigned elements shl, shr +// ... and likewise for sub, mul, div, mod, and for unsigned elements shl, shr. +// Floating point elements have only overflowing_add, overflowing_sub, overflowing_mul, and overflowing_div. -// Exact results in lanes twice as wide (8, 16, and 32-bit lanes) +// Exact results in lanes twice as wide (8, 16, and 32-bit integer lanes) template constexpr auto widening_add(const vec&, const vec&) noexcept; template constexpr auto widening_mul(const vec&, const vec&) noexcept; -// Policy chosen at the call site +// Policy chosen at the call site (integer elements) template constexpr auto add(const vec&, const vec&); // ... and likewise sub, mul, div, mod, and for unsigned elements shl, shr -// Lane algorithms +// Lane algorithms: lane i of min, max, and clamp is std::min, std::max, and std::clamp of lane i template constexpr vec select(const typename vec::mask_type&, const vec&, const vec&) noexcept; template constexpr vec min(const vec&, const vec&) noexcept; template constexpr vec max(const vec&, const vec&) noexcept; template constexpr vec clamp(const vec& v, const vec& lo, const vec& hi) noexcept; -// Reductions: reduce folds left in lane order with the element's operation +// Reductions: reduce folds left in lane order with the element's operation. +// For floating point elements, reduce_min and reduce_max require that no lane is NaN. template > constexpr T reduce(const vec&, BinaryOperation = {}); template constexpr T reduce_min(const vec&) noexcept; template constexpr T reduce_max(const vec&) noexcept; @@ -188,10 +197,10 @@ template constexpr void partial_store(const vec The element type and its error policy are part of the vector type, and mixing follows the scalar rules exactly: * Operations take two vectors of the same element type and lane count, or a vector and one value of its element type, which is broadcast. -* Mixed widths (`vec + vec`), mixed signedness (`vec + vec`), mixed policies (`vec + vec`), mixed lane counts (`vec + vec`), and built-in operands (`v + 1U`) are compile time errors with a `static_assert` message. +* Mixed widths (`vec + vec`, `vec + vec`), mixed signedness (`vec + vec`), mixed policies (`vec + vec`), mixed lane counts (`vec + vec`), and built-in operands (`v + 1U`) are compile time errors with a `static_assert` message. * `u128` and `i128` elements are rejected, because `std::simd` has no 128-bit lanes, and so are the bounded types. -* Unary minus is rejected for unsigned elements, and the bitwise and shift operators for signed elements, as for the scalar types. -* The converting constructor converts between widths and policies of one signedness only. The scalar types never convert between signed and unsigned, so neither do the vectors. +* Unary minus is rejected for unsigned elements, and the bitwise and shift operators for signed elements, as for the scalar types. Floating point elements reject every operation the scalar `f32` and `f64` do not provide, listed under <>. +* The converting constructor converts between widths and policies of one signedness only, and between the policies of one floating point width. The scalar types never convert between signed and unsigned, between `f32` and `f64`, or between integers and floating point, so neither do the vectors. A `mask_type` is keyed on the lane size, so a mask from a vector of `u64` can not select between vectors of `u32` on either backend. @@ -287,6 +296,51 @@ Signed lane arithmetic would be undefined behavior on overflow, so every lane co The `saturating_*`, `overflowing_*`, `checked_*`, and `strict_*` families cover `add`, `sub`, `mul`, `div`, and `mod`, as for the scalar signed types; there are no shift functions. `overflowing_div` and `overflowing_mod` flag the `min / -1` lanes, and `widening_add` and `widening_mul` return lanes of `i16`, `i32`, or `i64`. +== Floating Point Operations + +[cols="1,2,2", options="header"] +|=== +| Operation | Error lanes | Error under `throw_exception` + +| `+`, `-`, `*`, `/` +| The result is `+inf`, other than from division by zero +| `std::overflow_error` + +| `+`, `-`, `*`, `/` +| The result is `-inf`, other than from division by zero +| `std::underflow_error` + +| `+`, `-`, `*`, `/` +| The result is NaN: a NaN operand, or an invalid operation such as `inf - inf`, `0 * inf`, `0 / 0`, or a signaling NaN operand +| `std::domain_error` + +| `/` +| A finite nonzero dividend divided by zero +| `std::domain_error` +|=== + +The scalar `f32` and `f64` report an error exactly when the IEEE 754 result is not finite, and classify the cause only to choose the exception and message. +The vector computes every lane with the IEEE 754 operation, which is never undefined behavior, and its error mask is the lanes outside `[lowest, max]`: one pair of comparisons that is false for both infinities and for NaN. +When any lane is set, the scalar operator replays the lanes in order, so the lowest failing lane classifies the error exactly as the scalar type does, and a user defined handler receives the scalar arguments (`error_kind::overflow`, `underflow`, `nan_operation`, `invalid_operation`, or `divide_by_zero`, with the raw IEEE 754 result) for every failing lane. + +`sat_f32` and `sat_f64` lanes are raw IEEE 754 arithmetic: they never compute a mask, never throw, and their operators are `noexcept`. +`overflowing_add`, `overflowing_sub`, `overflowing_mul`, and `overflowing_div` return the raw lanes and the mask of the lanes the operator would have reported, for every policy, and are the only free function family, as for the scalar types. + +The surface mirrors the scalar types exactly. +There is no `%`, compound assignment, `++`, `--`, unary `+` or `-`, bitwise or shift operator, and no `saturating_*`, `checked_*`, `strict_*`, `widening_*`, or generic `add` family, and each of the operators is a `static_assert` with a message. +There is also no `strict_f32`, because `strict` is an integer policy. + +Comparisons are IEEE 754 comparisons: a NaN lane compares unequal to everything, and `-0.0` equals `+0.0`. +Lane `i` of `min`, `max`, and `clamp` is `std::min`, `std::max`, and `std::clamp` of lane `i`, so ties and NaN lanes resolve as they do for the scalar types: `min(a, b)` and `max(a, b)` return `a` when the lanes are unordered or equal (for example `-0.0` and `+0.0`), and `clamp` leaves a NaN lane NaN. +GCC 16's `std::simd::min` returns `b` in those lanes, so these are not forwarded to it. +`reduce_min` and `reduce_max` require that no lane is NaN, and when both zeros are the extreme value, which of them is returned is unspecified. + +Conversions between the policies of one width (`vec` to `vec` and back) copy every lane unchanged, including infinities and NaNs, as `sat_f32{static_cast(x)}` does for a scalar. + +NOTE: On x87 targets (`FLT_EVAL_METHOD` 2, such as 32-bit x86 without SSE2 math), values pass through 80-bit registers. +Copying a signaling NaN through them quiets it, so a failing lane may report `Operation with NAN` where the scalar operation on the original value reports `Invalid operation` (both are `std::domain_error`). +With GCC's `-fexcess-precision=fast`, the scalar types classify the unrounded 80-bit result, so a result just beyond `max` that rounds to `max` may throw for the scalar types but not for the vector lanes, which are rounded before they are checked. + == Deferred Checking The `overflowing_*` functions never branch on the error, so a loop can accumulate the masks and test them once at the end: @@ -349,11 +403,31 @@ lanes where min / -1 overflowed: 4 (wrapped quotient -2147483648) ---- ==== +.This https://github.com/boostorg/safe_numbers/blob/develop/examples/simd_float.cpp[example] demonstrates floating point vector arithmetic, where a lane that overflows to infinity or divides by zero is reported as the scalar `f32` reports it, together with the overflowing form, raw IEEE 754 `sat_f32` lanes, and the lane algorithms. +==== +[source, c++] +---- +include::example$simd_float.cpp[tags=**;!exclude] +---- + +Output: +---- +Error: Overflow detected in f32 multiplication +lane 0: 3 +lane 1: -9 +lane 2: inf (not finite) +lane 3: 4 +Error: Division by zero detected in f32 division +saturated: inf -2.25 1.5e+38 2.66667 +clamped: 1 -1 1 1 +largest reading: 3e+38 +---- +==== + == Limitations These are not supported yet: -* Floating-point element types. * `u128` and `i128` elements, since `std::simd` has no 128-bit lanes. * Bounded element types. * CUDA and SYCL device code, which have no ``. diff --git a/examples/simd_float.cpp b/examples/simd_float.cpp new file mode 100644 index 0000000..cff79fe --- /dev/null +++ b/examples/simd_float.cpp @@ -0,0 +1,84 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// tag::exclude[] +// GCC 16's returns 32-byte vectors from internal helpers when AVX is not enabled, +// which -Wpsabi reports at the end of the translation unit even for plain std::simd code +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic ignored "-Wpsabi" +#endif +// end::exclude[] + +#include +#include +#include +#include +#include + +int main() +{ + using namespace boost::safe_numbers; + using vec4 = simd::vec; + + // Sensor readings and the gains to apply to them + const std::array readings {f32{1.5f}, f32{-2.25f}, f32{3.0e38f}, f32{8.0f}}; + const std::array gains {f32{2.0f}, f32{4.0f}, f32{10.0f}, f32{0.5f}}; + + const auto r {simd::unchecked_load(readings)}; + const auto g {simd::unchecked_load(gains)}; + + // Lane 2 overflows to infinity, so the vector throws exactly what the scalar f32 throws + try + { + const auto scaled {r * g}; + static_cast(scaled); + } + catch (const std::overflow_error& e) + { + std::cout << "Error: " << e.what() << '\n'; + } + + // overflowing_mul returns the raw IEEE 754 lanes and a mask of the lanes that are not finite + const auto [raw, failed] {simd::overflowing_mul(r, g)}; + for (int i {}; i < vec4::size(); ++i) + { + std::cout << "lane " << i << ": " << raw[i] << (failed[i] ? " (not finite)" : "") << '\n'; + } + + // Division by a zero lane is reported as such, as for the scalar types + const vec4 divisors {[](const auto i) { return f32{static_cast(static_cast(i))}; }}; + try + { + const auto quotients {r / divisors}; + static_cast(quotients); + } + catch (const std::domain_error& e) + { + std::cout << "Error: " << e.what() << '\n'; + } + + // sat_f32 lanes are raw IEEE 754 arithmetic: no checks and no exceptions + using sat_vec4 = simd::vec; + const auto ieee {sat_vec4{r} / sat_vec4{divisors}}; + std::cout << "saturated:"; + for (int i {}; i < sat_vec4::size(); ++i) + { + std::cout << ' ' << ieee[i]; + } + std::cout << '\n'; + + // Lane algorithms follow std::min, std::max, and std::clamp lane by lane + const auto limited {simd::clamp(r, vec4{f32{-1.0f}}, vec4{f32{1.0f}})}; + std::cout << "clamped:"; + for (int i {}; i < vec4::size(); ++i) + { + std::cout << ' ' << limited[i]; + } + std::cout << "\nlargest reading: " << simd::reduce_max(r) << '\n'; + + // As for the scalar f32, there is no operator%, compound assignment, or unary minus: + // r % g; // static_assert: There is no operator% for floating point safe types + + return 0; +} diff --git a/extra/README.md b/extra/README.md index 3f65aa9..bd6df06 100644 --- a/extra/README.md +++ b/extra/README.md @@ -1,13 +1,15 @@ # Single header amalgamation -`extra/amalgamate.py` combines every Boost.SafeNumbers header (including the vendored copy of Boost.Int128) +`extra/amalgamate.py` combines every Boost.SafeNumbers header, plus the Boost.Int128 headers it uses, into one file, `extra/single_include/boost/safe_numbers.hpp`. +Boost.Int128 is inlined because no Boost release before 1.93.0 ships it, +so the single header keeps working with the older Boost versions available on Compiler Explorer. The single header behaves exactly like `#include `, which makes the library easy to try out on [Compiler Explorer](https://godbolt.org) and in other single-file environments. This follows the model of the Boost.Int128 [single header](https://github.com/cppalliance/int128/blob/develop/extra/single_include/boost/int128.hpp), which is what its [Compiler Explorer example](https://godbolt.org/z/3q4zce41d) includes. -Unlike Boost.Int128, Boost.SafeNumbers has external dependencies, so the single header is not fully standalone. +Unlike Boost.Int128, Boost.SafeNumbers has other external dependencies, so the single header is not fully standalone. These includes are left in place and must be found on the include path: * `` (Boost.ThrowException) @@ -16,24 +18,34 @@ These includes are left in place and must be found on the include path: ## Generating the single header -The script needs only Python 3 and its standard library. Run it from anywhere: +The script needs Python 3 and its standard library, plus a checkout of [Boost.Int128](https://github.com/boostorg/int128). +In the Boost superproject layout (`libs/safe_numbers` next to `libs/int128`) it finds Boost.Int128 on its own, +so it runs from anywhere: ```sh python3 extra/amalgamate.py ``` +Otherwise, point it at the Boost.Int128 include directory: + +```sh +python3 extra/amalgamate.py --int128-include path/to/int128/include +``` + This writes `extra/single_include/boost/safe_numbers.hpp`. The optional positional arguments `include_root top out` select a different include directory, starting header, or output file (see `python3 extra/amalgamate.py --help`). -The script exits with an error if any internal include survives in the output. +The script exits with an error if any Boost.SafeNumbers or Boost.Int128 include survives in the output. -Regenerate the header whenever anything under `include/` changes. +The `amalgamate` workflow regenerates the header with Boost.Int128 `develop` and commits it +whenever anything under `include/` changes on `develop`. +A change to Boost.Int128 alone does not trigger it; run the workflow manually to pick one up. ## Using it on Compiler Explorer 1. Choose a C++ compiler such as x86-64 GCC or x86-64 Clang, and pass `-std=c++20` or later. 2. Open **Libraries** and add **Boost** version **1.89.0 or later**. Older Boost releases fail to compile the single header - because the vendored Boost.Int128 calls a Boost.Charconv internal (`detail::to_chars_128integer_impl`) whose signature changed in 1.89.0. + because the inlined Boost.Int128 calls a Boost.Charconv internal (`detail::to_chars_128integer_impl`) whose signature changed in 1.89.0. 3. Include the single header by URL: ```cpp diff --git a/extra/amalgamate.py b/extra/amalgamate.py index e402b16..8b8b858 100644 --- a/extra/amalgamate.py +++ b/extra/amalgamate.py @@ -5,10 +5,12 @@ """Amalgamate boost.safe_numbers into a single header. Starts from the umbrella header and recursively inlines every internal - include, plus quoted includes relative to the -including file (e.g. "detail/int128/int128.hpp"), in DFS pre-order so -declarations precede use. System includes and the external Boost -dependencies (, , + include, every Boost.Int128 +include, and any quoted include relative to the including file, in DFS +pre-order so declarations precede use. Boost.Int128 is inlined because no +Boost release before 1.93 ships it, so the header keeps working with older +Boost installs (e.g. on Compiler Explorer). System includes and the other +external Boost dependencies (, , , ...) are left untouched, so the result still needs Boost on the include path. See extra/README.md for Compiler Explorer use. @@ -18,7 +20,9 @@ include guards make the repeat harmless. Run with no arguments to regenerate -extra/single_include/boost/safe_numbers.hpp from include/boost/safe_numbers.hpp. +extra/single_include/boost/safe_numbers.hpp from include/boost/safe_numbers.hpp, +taking Boost.Int128 from ../int128/include (the Boost superproject layout). +Pass --int128-include to use a different Boost.Int128 checkout. """ import argparse @@ -26,7 +30,7 @@ import sys from pathlib import Path -ANGLE_RE = re.compile(r'^\s*#\s*include\s*<(boost/safe_numbers(?:/[^>]+)?\.hpp)>\s*(?://.*)?$') +ANGLE_RE = re.compile(r'^\s*#\s*include\s*<(boost/(?:safe_numbers|int128)(?:/[^>]+)?\.hpp)>\s*(?://.*)?$') QUOTED_RE = re.compile(r'^\s*#\s*include\s*"([^"]+)"\s*(?://.*)?$') IF_RE = re.compile(r'^\s*#\s*(?:if|ifdef|ifndef)\b') ENDIF_RE = re.compile(r'^\s*#\s*endif\b') @@ -34,12 +38,12 @@ DEFINE_RE = re.compile(r'^\s*#\s*define\s+(\w+)\s*$') # Any surviving include of these forms means the output is not self-contained -LEFTOVER_RE = re.compile(r'^\s*#\s*include\s*(?:"|])') +LEFTOVER_RE = re.compile(r'^\s*#\s*include\s*(?:"|])') PREAMBLE = [ '// boost.safe_numbers: amalgamated single header build.', - '// Auto-generated: all internal boost/safe_numbers headers inlined into one file', - '// for use in Compiler Explorer and other single-file environments.', + '// Auto-generated: all boost/safe_numbers headers and the Boost.Int128 headers they', + '// use are inlined into one file for Compiler Explorer and other single-file environments.', '// External Boost dependencies (Config, Core, ThrowException, Charconv) are still', '// required on the include path.', '// Do not edit by hand; regenerate with extra/amalgamate.py.', @@ -62,11 +66,20 @@ def has_include_guard(lines): class Amalgamator: - def __init__(self, include_root): - self.include_root = include_root.resolve() + def __init__(self, include_roots): + self.include_roots = [root.resolve() for root in include_roots] self.visited = {} # rel path -> 'unconditional' or 'conditional' self.out_lines = [] + # Find the file for an include-root-relative header in the first root that has it + def locate(self, rel_path): + for root in self.include_roots: + full = root / rel_path + if full.is_file(): + return full + roots = ', '.join(str(root) for root in self.include_roots) + sys.exit(f'amalgamate: internal header {rel_path} not found under {roots}') + # Map an include directive in rel_path to an include-root-relative header, or None if external def resolve(self, line, rel_path): match = ANGLE_RE.match(line) @@ -77,20 +90,20 @@ def resolve(self, line, rel_path): if not match: return None - target = ((self.include_root / rel_path).parent / match.group(1)).resolve() + target = (self.locate(rel_path).parent / match.group(1)).resolve() if not target.is_file(): sys.exit(f'amalgamate: quoted include "{match.group(1)}" in {rel_path} not found') - try: - return target.relative_to(self.include_root).as_posix() - except ValueError: - sys.exit(f'amalgamate: quoted include "{match.group(1)}" in {rel_path} is outside {self.include_root}') + for root in self.include_roots: + try: + return target.relative_to(root).as_posix() + except ValueError: + pass + sys.exit(f'amalgamate: quoted include "{match.group(1)}" in {rel_path} is outside the include roots') # Inline one header: emit its lines, recursing into internal includes. # stack holds the headers being expanded; conditional is True inside an #if block. def inline(self, rel_path, stack, conditional): - full = self.include_root / rel_path - if not full.is_file(): - sys.exit(f'amalgamate: internal header {rel_path} not found under {self.include_root}') + full = self.locate(rel_path) if not conditional: self.visited[rel_path] = 'unconditional' @@ -129,9 +142,15 @@ def main(): parser.add_argument('out', nargs='?', type=Path, default=repo_root / 'extra' / 'single_include' / 'boost' / 'safe_numbers.hpp', help='output file (default: /extra/single_include/boost/safe_numbers.hpp)') + parser.add_argument('--int128-include', type=Path, default=repo_root.parent / 'int128' / 'include', + help='Boost.Int128 include directory (default: /../int128/include)') args = parser.parse_args() - amalgamator = Amalgamator(args.include_root) + if not (args.int128_include / 'boost' / 'int128.hpp').is_file(): + sys.exit(f'amalgamate: Boost.Int128 headers not found in {args.int128_include}; ' + 'pass --int128-include path/to/int128/include') + + amalgamator = Amalgamator([args.include_root, args.int128_include]) amalgamator.out_lines.extend(PREAMBLE) amalgamator.inline(args.top, [], False) diff --git a/include/boost/safe_numbers/detail/simd/array_backend.hpp b/include/boost/safe_numbers/detail/simd/array_backend.hpp index 6dbcf86..2a64890 100644 --- a/include/boost/safe_numbers/detail/simd/array_backend.hpp +++ b/include/boost/safe_numbers/detail/simd/array_backend.hpp @@ -35,6 +35,10 @@ struct mask_lane<8U> { using type = std::uint64_t; }; template using wrap_type_t = std::conditional_t<(sizeof(B) <= sizeof(unsigned)), unsigned, std::uint64_t>; +// Floating point lanes compute in their own type, where every operation is defined +template +using lane_arith_t = std::conditional_t, B, wrap_type_t>; + // MSVC for ARM64 at /O2 crashes (C1001) on some expanded lane forms and miscompiles // others (seen on 19.51 and 19.52), so the affected operations use a form it gets right #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_ARM64) @@ -267,9 +271,9 @@ class array_vec private: - // + - * << ~ & | ^ wrap through this unsigned type and convert back, which is - // modular (C++20), so narrow lanes never overflow a promoted int - using wrap_type = wrap_type_t; + // Integer + - * << ~ & | ^ wrap through this unsigned type and convert back, which + // is modular (C++20), so narrow lanes never overflow a promoted int + using wrap_type = lane_arith_t; using mask_lane_type = typename mask_type::lane_type; static constexpr auto lanes {static_cast(N)}; @@ -410,6 +414,15 @@ class array_vec return array_vec{make_lanes([&v](const std::size_t i) { return ~static_cast(v.lanes_[i]); })}; } + // Exact comparison is the intent for floating point lanes too, as in std::simd + #ifdef __clang__ + # pragma clang diagnostic push + # pragma clang diagnostic ignored "-Wfloat-equal" + #elif defined(__GNUC__) + # pragma GCC diagnostic push + # pragma GCC diagnostic ignored "-Wfloat-equal" + #endif + friend constexpr auto operator==(const array_vec& lhs, const array_vec& rhs) noexcept -> mask_type { return compare(lhs, rhs, [](const B x, const B y) { return x == y; }); @@ -420,6 +433,12 @@ class array_vec return compare(lhs, rhs, [](const B x, const B y) { return x != y; }); } + #ifdef __clang__ + # pragma clang diagnostic pop + #elif defined(__GNUC__) + # pragma GCC diagnostic pop + #endif + friend constexpr auto operator<(const array_vec& lhs, const array_vec& rhs) noexcept -> mask_type { return compare(lhs, rhs, [](const B x, const B y) { return x < y; }); @@ -451,17 +470,40 @@ using mask_t = array_mask; template inline constexpr int native_lanes {static_cast(16U / sizeof(B))}; -// Bitwise blend of each lane, which the optimizers turn into a vector blend +// Bitwise blend of each lane, which the optimizers turn into a vector blend. Floating +// point lanes blend their bit patterns rather than converting their values. template [[nodiscard]] constexpr auto select(const array_mask& m, const array_vec& lhs, const array_vec& rhs) noexcept -> array_vec { - using wrap_type = wrap_type_t; + if constexpr (std::is_floating_point_v) + { + using bits_type = typename array_mask::lane_type; - return array_vec{make_lanes(N)>([&](const std::size_t i) + return array_vec{make_lanes(N)>([&](const std::size_t i) + { + const auto bits {m.raw()[i]}; + const auto x {std::bit_cast(lhs.raw()[i])}; + const auto y {std::bit_cast(rhs.raw()[i])}; + return std::bit_cast(static_cast((x & bits) | (y & static_cast(~bits)))); + })}; + } + else { - const auto bits {static_cast(m.raw()[i])}; - return (static_cast(lhs.raw()[i]) & bits) | (static_cast(rhs.raw()[i]) & static_cast(~bits)); - })}; + using wrap_type = wrap_type_t; + + return array_vec{make_lanes(N)>([&](const std::size_t i) + { + const auto bits {static_cast(m.raw()[i])}; + return (static_cast(lhs.raw()[i]) & bits) | (static_cast(rhs.raw()[i]) & static_cast(~bits)); + })}; + } +} + +// The std backend needs these for a libstdc++ defect; here they are the plain operations +template +[[nodiscard]] constexpr auto select_lanes(const typename V::mask_type& m, const V& lhs, const V& rhs) noexcept -> V +{ + return select(m, lhs, rhs); } template @@ -500,6 +542,18 @@ template return res; } +template +[[nodiscard]] constexpr auto reduce_min_lanes(const V& v) noexcept -> typename V::value_type +{ + return reduce_min(v); +} + +template +[[nodiscard]] constexpr auto reduce_max_lanes(const V& v) noexcept -> typename V::value_type +{ + return reduce_max(v); +} + // Precondition: no zero lane in b, and no lane dividing min by -1 template constexpr auto divide_lanes(const array_vec& a, const array_vec& b) noexcept -> array_vec diff --git a/include/boost/safe_numbers/detail/simd/config.hpp b/include/boost/safe_numbers/detail/simd/config.hpp index ad4ff74..810af48 100644 --- a/include/boost/safe_numbers/detail/simd/config.hpp +++ b/include/boost/safe_numbers/detail/simd/config.hpp @@ -29,6 +29,7 @@ #endif #include +#include #include #include #include diff --git a/include/boost/safe_numbers/detail/simd/float_ops.hpp b/include/boost/safe_numbers/detail/simd/float_ops.hpp new file mode 100644 index 0000000..2228e1c --- /dev/null +++ b/include/boost/safe_numbers/detail/simd/float_ops.hpp @@ -0,0 +1,269 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_SAFE_NUMBERS_DETAIL_SIMD_FLOAT_OPS_HPP +#define BOOST_SAFE_NUMBERS_DETAIL_SIMD_FLOAT_OPS_HPP + +#include +#include +#include + +// IEEE 754 lane arithmetic overflows to infinity by design, and the kernels test for it +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable:4756) +#endif + +namespace boost::safe_numbers::detail::simd_impl { + +// ------------------------------ +// Lane kernels +// ------------------------------ + +// Floating point lane arithmetic is never undefined behavior: it rounds, overflows to +// infinity, and produces NaN as IEEE 754 specifies. The checked scalar operators report an +// error exactly when the result is not finite (their classifiers only choose which one), +// so the error lanes are those outside [lowest, max]. Both comparisons are false for NaN. + +enum class float_op_kind +{ + add, + sub, + mul, + div +}; + +template +constexpr auto float_lanes(const storage_t& lhs, const storage_t& rhs) noexcept -> storage_t +{ + if constexpr (Op == float_op_kind::add) + { + return lhs + rhs; + } + else if constexpr (Op == float_op_kind::sub) + { + return lhs - rhs; + } + else if constexpr (Op == float_op_kind::mul) + { + return lhs * rhs; + } + else + { + return lhs / rhs; + } +} + +template +constexpr auto finite_lanes(const storage_t& res) noexcept -> mask_t +{ + using storage_type = storage_t; + + return (res <= storage_type{std::numeric_limits::max()}) && (res >= storage_type{std::numeric_limits::lowest()}); +} + +// The element's own scalar operator, which classifies and reports a failing lane +template +constexpr auto float_scalar_op(const T lhs, const T rhs) -> T +{ + if constexpr (Op == float_op_kind::add) + { + return lhs + rhs; + } + else if constexpr (Op == float_op_kind::sub) + { + return lhs - rhs; + } + else if constexpr (Op == float_op_kind::mul) + { + return lhs * rhs; + } + else + { + return lhs / rhs; + } +} + +// Saturate is raw IEEE 754 arithmetic with no check at all. Every other policy tests the +// mask once and replays the scalar operator lane by lane only when some lane failed. +// Testing that every lane is finite saves negating the mask on the hot path. +template +constexpr auto float_op(const simd::vec& lhs, const simd::vec& rhs) -> simd::vec +{ + using V = simd::vec; + using basis_type = typename T::basis_type; + + const auto res {float_lanes(access::data(lhs), access::data(rhs))}; + + if constexpr (policy_equals(overflow_policy::saturate)) + { + return access::make(res); + } + else + { + if (!all_of(finite_lanes(res))) [[unlikely]] + { + return replay_binary(lhs, rhs, [](const T x, const T y) { return float_scalar_op(x, y); }); + } + + return access::make(res); + } +} + +template +constexpr auto float_overflowing_op(const simd::vec& lhs, const simd::vec& rhs) noexcept + -> std::pair, typename simd::vec::mask_type> +{ + using V = simd::vec; + using basis_type = typename T::basis_type; + + const auto res {float_lanes(access::data(lhs), access::data(rhs))}; + return std::pair{access::make(res), !finite_lanes(res)}; +} + +// ------------------------------ +// Increment, decrement, conversions +// ------------------------------ + +template + requires is_float_element_v +constexpr auto increment(adl_tag, const simd::vec& v) noexcept -> simd::vec +{ + static_assert(dependent_false, "Increment is deliberately not provided for floating point safe types"); + return v; // LCOV_EXCL_LINE : deliberately unreachable +} + +template + requires is_float_element_v +constexpr auto decrement(adl_tag, const simd::vec& v) noexcept -> simd::vec +{ + static_assert(dependent_false, "Decrement is deliberately not provided for floating point safe types"); + return v; // LCOV_EXCL_LINE : deliberately unreachable +} + +// Between policies of one width every lane is copied unchanged, as +// To{static_cast(x)} does. The scalar f32 and f64 do not convert to each other. +template + requires (is_float_element_v && is_float_element_v) +constexpr auto convert_elements(adl_tag, std::type_identity, const simd::vec& v) noexcept -> simd::vec +{ + static_assert(std::is_same_v, + "simd::vec can not convert between f32 and f64 elements, because the scalar types can not: " + "copy the raw lanes out with operator std::array() and convert each value explicitly"); + + if constexpr (std::is_same_v) + { + return access::make>(access::data(v)); + } + else + { + return simd::vec{}; // LCOV_EXCL_LINE : rejected above + } +} + +template + requires (is_float_element_v != is_float_element_v) +constexpr auto convert_elements(adl_tag, std::type_identity, const simd::vec&) -> simd::vec +{ + static_assert(dependent_false, + "simd::vec can not convert between floating point and integer elements, because the scalar " + "safe types can not: copy the raw lanes out with operator std::array() and " + "convert each value explicitly"); + + return simd::vec{}; // LCOV_EXCL_LINE : deliberately unreachable +} + +} // namespace boost::safe_numbers::detail::simd_impl + +namespace boost::safe_numbers::simd { + +// ------------------------------ +// Operators +// ------------------------------ + +// As for the scalar types, sat_f32 and sat_f64 never throw, and division uses the same +// noexcept condition as the other operators: IEEE 754 division by zero is an error lane +#define BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR(OP_SYMBOL, OP_KIND) \ +template \ + requires detail::simd_impl::is_float_element_v \ +[[nodiscard]] constexpr auto OP_SYMBOL(const vec& lhs, const vec& rhs) \ + noexcept(detail::policy_is_nothrow_arith()) -> vec \ +{ \ + return detail::simd_impl::float_op(lhs, rhs); \ +} + +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR(operator+, add) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR(operator-, sub) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR(operator*, mul) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR(operator/, div) + +#undef BOOST_SAFE_NUMBERS_SIMD_FLOAT_OPERATOR + +// The scalar f32 and f64 have no remainder, bitwise, or shift operators + +#define BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(OP_SYMBOL, MESSAGE) \ +template \ + requires detail::simd_impl::is_float_element_v \ +constexpr auto OP_SYMBOL(const vec& lhs, const vec&) noexcept -> vec \ +{ \ + static_assert(detail::dependent_false, MESSAGE); \ + return lhs; /* LCOV_EXCL_LINE : deliberately unreachable */ \ +} + +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator%, "There is no operator% for floating point safe types: C++ has no built-in floating point remainder operator") +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator&, "Bitwise AND is deliberately disabled for floating point safe types") +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator|, "Bitwise OR is deliberately disabled for floating point safe types") +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator^, "Bitwise XOR is deliberately disabled for floating point safe types") +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator<<, "Left shift is deliberately disabled for floating point safe types") +BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED(operator>>, "Right shift is deliberately disabled for floating point safe types") + +#undef BOOST_SAFE_NUMBERS_SIMD_FLOAT_DISABLED + +template + requires detail::simd_impl::is_float_element_v +constexpr auto operator~(const vec& v) noexcept -> vec +{ + static_assert(detail::dependent_false, "Bitwise NOT is deliberately disabled for floating point safe types"); + return v; // LCOV_EXCL_LINE : deliberately unreachable +} + +template + requires detail::simd_impl::is_float_element_v +constexpr auto operator-(const vec& v) noexcept -> vec +{ + static_assert(detail::dependent_false, "Unary minus is deliberately not provided for floating point safe types"); + return v; // LCOV_EXCL_LINE : deliberately unreachable +} + +// ------------------------------ +// Overflowing family +// ------------------------------ + +// The raw IEEE 754 lanes and the mask of the lanes where the operator would have reported +// an error, which is every lane that is not finite. As for the scalar types, this is the +// only free function family for floating point elements. + +#define BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING(OP, OP_KIND) \ +template \ + requires detail::simd_impl::is_float_element_v \ +[[nodiscard]] constexpr auto overflowing_##OP(const vec& lhs, const vec& rhs) noexcept \ + -> std::pair, typename vec::mask_type> \ +{ \ + return detail::simd_impl::float_overflowing_op(lhs, rhs); \ +} + +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING(add, add) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING(sub, sub) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING(mul, mul) +BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING(div, div) + +#undef BOOST_SAFE_NUMBERS_SIMD_FLOAT_OVERFLOWING + +} // namespace boost::safe_numbers::simd + +#ifdef _MSC_VER +# pragma warning(pop) +#endif + +#endif // BOOST_SAFE_NUMBERS_DETAIL_SIMD_FLOAT_OPS_HPP diff --git a/include/boost/safe_numbers/detail/simd/std_backend.hpp b/include/boost/safe_numbers/detail/simd/std_backend.hpp index a43828c..cf13825 100644 --- a/include/boost/safe_numbers/detail/simd/std_backend.hpp +++ b/include/boost/safe_numbers/detail/simd/std_backend.hpp @@ -35,6 +35,79 @@ using std::simd::max; using std::simd::reduce_min; using std::simd::reduce_max; +// The unsigned lanes of the same size as the floating point lanes of V, when both vectors +// have the same layout. With AVX but not AVX2, some lane counts store them differently. +template +struct bits_vec +{ + using type = void; +}; + +template + requires std::is_floating_point_v +struct bits_vec +{ + using bits_type = std::conditional_t; + using candidate = std::simd::rebind_t; + using type = std::conditional_t; +}; + +// libstdc++ 16's select skips the blend when an arm is a compile time constant equal to +// zero, but tests that with ==, so an arm of -0.0 lanes comes back as +0.0. Floating point +// lanes therefore select their bit patterns in unsigned lanes of the same size, which is +// the same blend instruction count. Where the layouts differ (AVX without AVX2), and in +// constant evaluation, which rejects reading the padding lanes, they select lane by lane. +// min, max, clamp, and the min and max reductions all select, so they use this too. +template +constexpr auto select_lanes(const typename V::mask_type& m, const V& a, const V& b) noexcept -> V +{ + using value_type = typename V::value_type; + using bits_type = typename bits_vec::type; + + if constexpr (std::is_floating_point_v) + { + if constexpr (!std::is_void_v) + { + if (!std::is_constant_evaluated()) + { + return std::bit_cast(std::simd::select(static_cast(m), std::bit_cast(a), std::bit_cast(b))); + } + } + + return V{[&m, &a, &b](const auto i) { return m[i] ? static_cast(a[i]) : static_cast(b[i]); }}; + } + else + { + return std::simd::select(m, a, b); + } +} + +template +constexpr auto reduce_min_lanes(const V& v) noexcept -> typename V::value_type +{ + if constexpr (std::is_floating_point_v) + { + return std::simd::reduce(v, [](const auto& a, const auto& b) { return select_lanes(a < b, a, b); }); + } + else + { + return std::simd::reduce_min(v); + } +} + +template +constexpr auto reduce_max_lanes(const V& v) noexcept -> typename V::value_type +{ + if constexpr (std::is_floating_point_v) + { + return std::simd::reduce(v, [](const auto& a, const auto& b) { return select_lanes(a < b, b, a); }); + } + else + { + return std::simd::reduce_max(v); + } +} + // libstdc++ 16 with AVX-512 pads a vector whose lane count is not a power of two with zero // lanes, and / and % may divide those too: a SIGFPE at run time, and not a constant // expression. Only that configuration divides lane by lane, because elsewhere GCC divides diff --git a/include/boost/safe_numbers/detail/simd/vec.hpp b/include/boost/safe_numbers/detail/simd/vec.hpp index 68bd60b..4d304d4 100644 --- a/include/boost/safe_numbers/detail/simd/vec.hpp +++ b/include/boost/safe_numbers/detail/simd/vec.hpp @@ -26,8 +26,7 @@ class vec; namespace boost::safe_numbers::detail::simd_impl { -// u8, u16, u32, u64 and i8, i16, i32, i64 in any policy form. Floating point -// elements are rejected until their lane operations exist. +// u8, u16, u32, u64, i8, i16, i32, i64, f32, and f64 in any policy form template inline constexpr bool is_unsigned_element_v {is_unsigned_library_type_v && !is_bounded_type_v && !std::is_same_v, int128::uint128>}; @@ -37,7 +36,10 @@ inline constexpr bool is_signed_element_v {is_signed_library_type_v && !is_bo !std::is_same_v, int128::int128>}; template -inline constexpr bool is_supported_element_v {is_unsigned_element_v || is_signed_element_v}; +inline constexpr bool is_float_element_v {is_float_library_type_v && !is_bounded_type_v}; + +template +inline constexpr bool is_supported_element_v {is_unsigned_element_v || is_signed_element_v || is_float_element_v}; template inline constexpr bool is_128_bit_element_v {std::is_same_v, int128::uint128> || @@ -198,8 +200,8 @@ class vec "simd::vec does not support 128-bit elements (u128, i128): std::simd has no 128-bit lanes"); static_assert(detail::simd_impl::is_supported_element_v, - "simd::vec supports the unsigned types u8, u16, u32, and u64 and the signed types " - "i8, i16, i32, and i64 in any policy form"); + "simd::vec supports the unsigned types u8, u16, u32, and u64, the signed types " + "i8, i16, i32, and i64, and the floating point types f32 and f64 in any policy form"); static_assert(N >= 1, "simd::vec requires at least one lane"); @@ -278,10 +280,13 @@ class vec return T{static_cast(data_[i])}; } - // Compound assignment computes the result first, so the operand is unchanged on error + // Compound assignment computes the result first, so the operand is unchanged on error. + // The scalar f32 and f64 deliberately have no compound assignment or unary plus, so + // neither do vectors of them. template constexpr auto operator+=(const U& rhs) noexcept(noexcept(std::declval() + rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this + rhs; return *this; } @@ -289,6 +294,7 @@ class vec template constexpr auto operator-=(const U& rhs) noexcept(noexcept(std::declval() - rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this - rhs; return *this; } @@ -296,6 +302,7 @@ class vec template constexpr auto operator*=(const U& rhs) noexcept(noexcept(std::declval() * rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this * rhs; return *this; } @@ -303,6 +310,7 @@ class vec template constexpr auto operator/=(const U& rhs) noexcept(noexcept(std::declval() / rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this / rhs; return *this; } @@ -310,6 +318,7 @@ class vec template constexpr auto operator%=(const U& rhs) noexcept(noexcept(std::declval() % rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this % rhs; return *this; } @@ -317,6 +326,7 @@ class vec template constexpr auto operator&=(const U& rhs) noexcept(noexcept(std::declval() & rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this & rhs; return *this; } @@ -324,6 +334,7 @@ class vec template constexpr auto operator|=(const U& rhs) noexcept(noexcept(std::declval() | rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this | rhs; return *this; } @@ -331,6 +342,7 @@ class vec template constexpr auto operator^=(const U& rhs) noexcept(noexcept(std::declval() ^ rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this ^ rhs; return *this; } @@ -338,6 +350,7 @@ class vec template constexpr auto operator<<=(const U& rhs) noexcept(noexcept(std::declval() << rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this << rhs; return *this; } @@ -345,11 +358,13 @@ class vec template constexpr auto operator>>=(const U& rhs) noexcept(noexcept(std::declval() >> rhs)) -> vec& { + static_assert(!detail::simd_impl::is_float_element_v, "Compound assignment is deliberately not provided for floating point safe types"); *this = *this >> rhs; return *this; } - // Each element kind's header provides increment and decrement + // Each element kind's header provides increment and decrement (for floating point + // elements, the static_assert that rejects them) constexpr auto operator++() noexcept(detail::policy_is_nothrow_arith()) -> vec& { *this = increment(detail::simd_impl::adl_tag {}, *this); @@ -376,7 +391,11 @@ class vec return old; } - constexpr auto operator+() const noexcept -> vec { return *this; } + constexpr auto operator+() const noexcept -> vec + { + static_assert(!detail::simd_impl::is_float_element_v, "Unary plus is deliberately not provided for floating point safe types"); + return *this; + } }; // ------------------------------ @@ -459,7 +478,7 @@ constexpr auto OP_SYMBOL(const vec&, const vec&) static_assert(detail::dependent_false, \ "Can not perform " OP_NAME " between simd::vec objects with different element types " \ "(width, signedness, or overflow policy): convert explicitly first, which is only " \ - "possible between elements of the same signedness"); \ + "possible where the scalar types convert"); \ } \ \ return vec{}; \ @@ -561,28 +580,64 @@ template [[nodiscard]] constexpr auto select(const typename vec::mask_type& m, const vec& lhs, const vec& rhs) noexcept -> vec { using access = detail::simd_impl::access; - return access::make>(detail::simd_impl::select(m, access::data(lhs), access::data(rhs))); + return access::make>(detail::simd_impl::select_lanes(m, access::data(lhs), access::data(rhs))); } +// Lane i is std::min(lhs[i], rhs[i]). For floating point lanes that returns lhs when +// the lanes are unordered (NaN) or equal (-0.0 and +0.0), which std::simd::min does not. template [[nodiscard]] constexpr auto min(const vec& lhs, const vec& rhs) noexcept -> vec { using access = detail::simd_impl::access; - return access::make>(detail::simd_impl::min(access::data(lhs), access::data(rhs))); + + if constexpr (detail::simd_impl::is_float_element_v) + { + const auto& a {access::data(lhs)}; + const auto& b {access::data(rhs)}; + return access::make>(detail::simd_impl::select_lanes(b < a, b, a)); + } + else + { + return access::make>(detail::simd_impl::min(access::data(lhs), access::data(rhs))); + } } +// Lane i is std::max(lhs[i], rhs[i]) template [[nodiscard]] constexpr auto max(const vec& lhs, const vec& rhs) noexcept -> vec { using access = detail::simd_impl::access; - return access::make>(detail::simd_impl::max(access::data(lhs), access::data(rhs))); + + if constexpr (detail::simd_impl::is_float_element_v) + { + const auto& a {access::data(lhs)}; + const auto& b {access::data(rhs)}; + return access::make>(detail::simd_impl::select_lanes(a < b, b, a)); + } + else + { + return access::make>(detail::simd_impl::max(access::data(lhs), access::data(rhs))); + } } -// Precondition: no lane of lo is greater than the same lane of hi +// Lane i is std::clamp(v[i], lo[i], hi[i]), so a NaN lane of v stays NaN. +// Precondition: no lane of lo is greater than the same lane of hi. template [[nodiscard]] constexpr auto clamp(const vec& v, const vec& lo, const vec& hi) noexcept -> vec { - return max(lo, min(hi, v)); + if constexpr (detail::simd_impl::is_float_element_v) + { + using access = detail::simd_impl::access; + + const auto& x {access::data(v)}; + const auto& a {access::data(lo)}; + const auto& b {access::data(hi)}; + return access::make>(detail::simd_impl::select_lanes(x < a, a, detail::simd_impl::select_lanes(b < x, b, x))); + } + else + { + return max(lo, min(hi, v)); + } } // ------------------------------ @@ -604,16 +659,18 @@ template > return res; } +// Precondition for floating point lanes: no lane is NaN. When -0.0 and +0.0 are both +// the extreme value, which of them is returned is unspecified. template [[nodiscard]] constexpr auto reduce_min(const vec& v) noexcept -> T { - return T{static_cast::basis_type>(detail::simd_impl::reduce_min(detail::simd_impl::access::data(v)))}; + return T{static_cast::basis_type>(detail::simd_impl::reduce_min_lanes(detail::simd_impl::access::data(v)))}; } template [[nodiscard]] constexpr auto reduce_max(const vec& v) noexcept -> T { - return T{static_cast::basis_type>(detail::simd_impl::reduce_max(detail::simd_impl::access::data(v)))}; + return T{static_cast::basis_type>(detail::simd_impl::reduce_max_lanes(detail::simd_impl::access::data(v)))}; } // ------------------------------ diff --git a/include/boost/safe_numbers/simd.hpp b/include/boost/safe_numbers/simd.hpp index 0e995e2..9d3b1e5 100644 --- a/include/boost/safe_numbers/simd.hpp +++ b/include/boost/safe_numbers/simd.hpp @@ -12,8 +12,10 @@ #include #include #include +#include #include #include #include +#include #endif // BOOST_SAFE_NUMBERS_SIMD_HPP diff --git a/test/Jamfile b/test/Jamfile index 14be614..e8202fb 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -335,6 +335,23 @@ compile-fail compile_fail_simd_signed_shift.cpp ; compile-fail compile_fail_simd_mixed_sign.cpp ; compile-fail compile_fail_simd_sign_conversion.cpp ; compile-fail compile_fail_simd_i128.cpp ; +run test_simd_float.cpp + : : : msvc:off ; +run test_simd_float_array.cpp + : : : msvc:off ; +run test_simd_float_constexpr.cpp ; +run test_simd_float_constexpr_array.cpp ; +compile-fail compile_fail_simd_float_mod.cpp ; +compile-fail compile_fail_simd_float_bitwise.cpp ; +compile-fail compile_fail_simd_float_shift.cpp ; +compile-fail compile_fail_simd_float_compound.cpp ; +compile-fail compile_fail_simd_float_increment.cpp ; +compile-fail compile_fail_simd_float_unary_minus.cpp ; +compile-fail compile_fail_simd_float_unary_plus.cpp ; +compile-fail compile_fail_simd_float_mixed_width.cpp ; +compile-fail compile_fail_simd_float_conversion.cpp ; +compile-fail compile_fail_simd_float_int_conversion.cpp ; +compile-fail compile_fail_simd_bounded_float.cpp ; # Signed free function tests run test_signed_charconv.cpp ; @@ -435,6 +452,7 @@ run ../examples/std_numeric_usage.cpp ; run ../examples/floats.cpp ; run ../examples/simd_unsigned.cpp ; run ../examples/simd_signed.cpp ; +run ../examples/simd_float.cpp ; run ../examples/cmath.cpp ; run ../examples/functional.cpp ; run ../examples/random.cpp ; diff --git a/test/benchmarks/benchmark_simd_operations.cpp b/test/benchmarks/benchmark_simd_operations.cpp index 2ca5ee7..8b16171 100644 --- a/test/benchmarks/benchmark_simd_operations.cpp +++ b/test/benchmarks/benchmark_simd_operations.cpp @@ -2,12 +2,13 @@ // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt -// Streaming out[i] = a[i] op b[i] over arrays of each unsigned and signed width, comparing: +// Streaming out[i] = a[i] op b[i] over arrays of each unsigned, signed, and floating point +// width, comparing: // builtin the raw scalar loop on the basis type (no checks) // std::simd the raw C++26 vector loop (no checks; std::simd backend only) // scalar safe the scalar safe type loop (one error branch per element) // vec simd::vec with the throwing policy (one error branch per vector) -// vec sat simd::vec with saturating lanes (no error branch) +// vec sat simd::vec with saturating lanes (no error branch; raw IEEE 754 for floats) // vec ovf overflowing_* with the masks accumulated and tested once // The inputs never overflow, so every throwing loop runs its hot path. Report timings // only from native hardware. @@ -71,6 +72,16 @@ struct mul_op static constexpr auto saturating(const T& lhs, const T& rhs) { return simd::saturating_mul(lhs, rhs); } }; +// Floating point only: the integer vectors have no division row +struct div_op +{ + template + static constexpr auto apply(const T& lhs, const T& rhs) { return lhs / rhs; } + + template + static constexpr auto overflowing(const T& lhs, const T& rhs) { return simd::overflowing_div(lhs, rhs); } +}; + // Builtin wrapping arithmetic with promotion handled, for the unchecked baselines template constexpr auto raw_apply(const B lhs, const B rhs) -> B @@ -79,9 +90,9 @@ constexpr auto raw_apply(const B lhs, const B rhs) -> B return static_cast(Op::apply(static_cast(lhs), static_cast(rhs))); } -// Operands that never overflow, for unsigned and signed basis types alike: positive, -// lhs >= rhs for subtraction, both below sqrt(max) for multiplication and below max / 2 -// for addition +// Operands that never overflow, for unsigned, signed, and floating point basis types alike: +// positive, lhs >= rhs for subtraction, both below sqrt(max) for multiplication and below +// max / 2 for addition, and never zero for division template struct inputs { @@ -292,4 +303,25 @@ BOOST_SAFE_NUMBERS_SIMD_BENCH(i64, std::int64_t, add_op) BOOST_SAFE_NUMBERS_SIMD_BENCH(i64, std::int64_t, sub_op) BOOST_SAFE_NUMBERS_SIMD_BENCH(i64, std::int64_t, mul_op) +// Floating point elements have no saturating_* functions: their saturating row is the +// sat_f32 or sat_f64 element type, whose lanes are raw IEEE 754 arithmetic +#define BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(T, S, B, OP) \ +BENCHMARK(builtin_loop)->Name(#T " " #OP " builtin"); \ +BOOST_SAFE_NUMBERS_SIMD_BENCH_STD(B, OP) \ +BENCHMARK(scalar_safe_loop)->Name(#T " " #OP " scalar safe"); \ +BENCHMARK(vec_loop)->Name(#T " " #OP " vec"); \ +BENCHMARK(vec_loop)->Name(#T " " #OP " vec sat"); \ +BENCHMARK(vec_loop)->Name(#T " " #OP " vec ovf"); + +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f32, sat_f32, float, add_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f32, sat_f32, float, sub_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f32, sat_f32, float, mul_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f32, sat_f32, float, div_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f64, sat_f64, double, add_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f64, sat_f64, double, sub_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f64, sat_f64, double, mul_op) +BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT(f64, sat_f64, double, div_op) + +#undef BOOST_SAFE_NUMBERS_SIMD_BENCH_FLOAT + BENCHMARK_MAIN(); diff --git a/test/compile_fail_simd_bounded_float.cpp b/test/compile_fail_simd_bounded_float.cpp new file mode 100644 index 0000000..712367c --- /dev/null +++ b/test/compile_fail_simd_bounded_float.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// simd::vec does not accept bounded floating point element types + +#include +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec, 4> v {}; + static_cast(v); + + return 0; +} diff --git a/test/compile_fail_simd_float_bitwise.cpp b/test/compile_fail_simd_float_bitwise.cpp new file mode 100644 index 0000000..506c8ca --- /dev/null +++ b/test/compile_fail_simd_float_bitwise.cpp @@ -0,0 +1,19 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Bitwise operators are disabled for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec lhs {f64{1.0}}; + const simd::vec rhs {f64{2.0}}; + + const auto res {lhs & rhs}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/compile_fail_simd_float_compound.cpp b/test/compile_fail_simd_float_compound.cpp new file mode 100644 index 0000000..f2fab62 --- /dev/null +++ b/test/compile_fail_simd_float_compound.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Compound assignment is not provided for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + simd::vec v {f32{1.0f}}; + + v += simd::vec{f32{2.0f}}; + + return static_cast(static_cast(v[0])); +} diff --git a/test/compile_fail_simd_float_conversion.cpp b/test/compile_fail_simd_float_conversion.cpp new file mode 100644 index 0000000..1de9325 --- /dev/null +++ b/test/compile_fail_simd_float_conversion.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Vectors of f32 do not convert to vectors of f64, as the scalar types do not + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec narrow {f32{1.0f}}; + + const simd::vec wide {narrow}; + + return static_cast(static_cast(wide[0])); +} diff --git a/test/compile_fail_simd_float_increment.cpp b/test/compile_fail_simd_float_increment.cpp new file mode 100644 index 0000000..bc9b341 --- /dev/null +++ b/test/compile_fail_simd_float_increment.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Increment is not provided for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + simd::vec v {f64{1.0}}; + + ++v; + + return static_cast(static_cast(v[0])); +} diff --git a/test/compile_fail_simd_float_int_conversion.cpp b/test/compile_fail_simd_float_int_conversion.cpp new file mode 100644 index 0000000..5529c1a --- /dev/null +++ b/test/compile_fail_simd_float_int_conversion.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Vectors of integer elements do not convert to vectors of floating point elements, as the scalar types do not + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec ints {i32{1}}; + + const simd::vec floats {ints}; + + return static_cast(static_cast(floats[0])); +} diff --git a/test/compile_fail_simd_float_mixed_width.cpp b/test/compile_fail_simd_float_mixed_width.cpp new file mode 100644 index 0000000..2863a1b --- /dev/null +++ b/test/compile_fail_simd_float_mixed_width.cpp @@ -0,0 +1,19 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Vectors of f32 and f64 do not mix, as the scalar types do not + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec lhs {f32{1.0f}}; + const simd::vec rhs {f64{2.0}}; + + const auto res {lhs + rhs}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/compile_fail_simd_float_mod.cpp b/test/compile_fail_simd_float_mod.cpp new file mode 100644 index 0000000..7127d0e --- /dev/null +++ b/test/compile_fail_simd_float_mod.cpp @@ -0,0 +1,19 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// There is no operator% for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec lhs {f32{5.0f}}; + const simd::vec rhs {f32{2.0f}}; + + const auto res {lhs % rhs}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/compile_fail_simd_float_shift.cpp b/test/compile_fail_simd_float_shift.cpp new file mode 100644 index 0000000..41bdf19 --- /dev/null +++ b/test/compile_fail_simd_float_shift.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Shift operators are disabled for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec lhs {f32{1.0f}}; + + const auto res {lhs << f32{1.0f}}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/compile_fail_simd_float_unary_minus.cpp b/test/compile_fail_simd_float_unary_minus.cpp new file mode 100644 index 0000000..d530084 --- /dev/null +++ b/test/compile_fail_simd_float_unary_minus.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Unary minus is not provided for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec v {f32{1.0f}}; + + const auto res {-v}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/compile_fail_simd_float_unary_plus.cpp b/test/compile_fail_simd_float_unary_plus.cpp new file mode 100644 index 0000000..750d2da --- /dev/null +++ b/test/compile_fail_simd_float_unary_plus.cpp @@ -0,0 +1,18 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Unary plus is not provided for vectors of floating point elements, as for the scalar types + +#include + +using namespace boost::safe_numbers; + +int main() +{ + const simd::vec v {f32{1.0f}}; + + const auto res {+v}; + + return static_cast(static_cast(res[0])); +} diff --git a/test/test_simd_float.cpp b/test/test_simd_float.cpp new file mode 100644 index 0000000..69b1b47 --- /dev/null +++ b/test/test_simd_float.cpp @@ -0,0 +1,749 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Differential test of simd::vec of floating point elements against the scalar library. +// For every policy, lane i of a vector operation must equal the scalar operation on lane i, +// and an error must be reported exactly as the scalar loop over i = 0 ... N - 1 reports it: +// the same exception type (overflow, underflow, or domain) and message, or the same +// sequence of user handler calls. Lanes are compared as bit patterns, so -0.0 and +0.0 +// differ; every NaN compares alike, because IEEE 754 does not fix the payload of a result. + +// GCC 16's returns 32-byte vectors from internal helpers when AVX is not enabled, +// which -Wpsabi reports at the end of the translation unit even for plain std::simd code +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic ignored "-Wpsabi" +#endif + +// Lanes divide by zero on purpose. MSVC takes the warning state at the end of each generated +// function, so this must precede every include (and the test is built without the pch on msvc). +#ifdef _MSC_VER +# pragma warning (disable: 4723) // Potential divide by 0 +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost::safe_numbers; + +// x87 evaluates in 80-bit registers (FLT_EVAL_METHOD 2). Copying a value through them quiets a +// signaling NaN, and the scalar types may classify the unrounded 80-bit result, so a sum that +// rounds down to max can be reported as an overflow. Both depend on the scalar code generation +// rather than on the vectors, so those inputs are left out on such targets. +#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 +constexpr bool excess_precision {true}; +#else +constexpr bool excess_precision {false}; +#endif + +// ------------------------------ +// Bit patterns +// ------------------------------ + +template +using bits_t = std::conditional_t, std::uint32_t, std::uint64_t>; + +constexpr auto nan_bits {~std::uint64_t{0}}; + +// The bit pattern of x, with every NaN mapped to one value +template +auto canonical(const B x) -> std::uint64_t +{ + if (std::isnan(x)) + { + return nan_bits; + } + + return static_cast(std::bit_cast>(x)); +} + +template +auto canonical_element(const T x) -> std::uint64_t +{ + return canonical(static_cast(x)); +} + +// ------------------------------ +// Handler that records every call +// ------------------------------ + +struct handler_call +{ + error_kind kind; + std::uint64_t value; + std::string message; + + friend auto operator==(const handler_call&, const handler_call&) -> bool = default; +}; + +inline std::vector& handler_log() +{ + static std::vector log; + return log; +} + +// Logs the call and returns a finite value, so a lane patched by the handler can not be +// mistaken for the raw IEEE 754 result +struct recording_handler +{ + template + auto on_error(const error_kind kind, const T value, const char* msg) const -> T + { + handler_log().push_back(handler_call{kind, canonical(value), msg}); + return static_cast(-7.25); + } +}; + +// ------------------------------ +// Outcome capture +// ------------------------------ + +struct outcome +{ + int exception {0}; + std::string message; + std::vector lanes; + std::vector calls; + + friend auto operator==(const outcome&, const outcome&) -> bool = default; +}; + +template +auto capture(F f) -> outcome +{ + outcome res; + handler_log().clear(); + + try + { + res.lanes = f(); + } + catch (const std::overflow_error& e) + { + res.exception = 1; + res.message = e.what(); + } + catch (const std::underflow_error& e) + { + res.exception = 2; + res.message = e.what(); + } + catch (const std::domain_error& e) + { + res.exception = 3; + res.message = e.what(); + } + + res.calls = handler_log(); + return res; +} + +template +auto lanes_of(const simd::vec& v) -> std::vector +{ + std::vector res; + for (int i {}; i < N; ++i) + { + res.push_back(canonical_element(v[i])); + } + + return res; +} + +template +auto flags_of(const M& m, const int n) -> std::vector +{ + std::vector res; + for (int i {}; i < n; ++i) + { + res.push_back(m[i] ? 1U : 0U); + } + + return res; +} + +// ------------------------------ +// Inputs +// ------------------------------ + +template +auto signaling_nan() -> B +{ + return std::bit_cast(std::bit_cast>(std::numeric_limits::signaling_NaN())); +} + +// Values on both sides of every error boundary: the zeros, the subnormals, the largest +// finite values and their neighbors, the operands whose square or quotient crosses max, +// the infinities, and the NaNs +template +auto edge_values() -> std::vector +{ + constexpr auto max {std::numeric_limits::max()}; + constexpr auto inf {std::numeric_limits::infinity()}; + const auto root {std::sqrt(max)}; + + std::vector res {static_cast(0), -static_cast(0), + std::numeric_limits::denorm_min(), -std::numeric_limits::denorm_min(), + std::numeric_limits::min(), -std::numeric_limits::min(), + std::numeric_limits::epsilon(), + static_cast(0.5), static_cast(-0.5), static_cast(1), static_cast(-1), + static_cast(2), static_cast(-2), static_cast(3), static_cast(-3), + max / 2, -max / 2, max, -max, + std::nextafter(max, static_cast(0)), -std::nextafter(max, static_cast(0)), + root, -root, std::nextafter(root, inf), -std::nextafter(root, inf), + inf, -inf, + std::numeric_limits::quiet_NaN(), -std::numeric_limits::quiet_NaN()}; + + if constexpr (std::numeric_limits::has_signaling_NaN && !excess_precision) + { + res.push_back(signaling_nan()); + } + + return res; +} + +// A random bit pattern, quieted when it is a signaling NaN on an excess precision target +template +auto from_bits(const bits_t bits) -> B +{ + if constexpr (excess_precision) + { + constexpr auto quiet_bit {static_cast>(bits_t{1} << (std::numeric_limits::digits - 2))}; + if (std::isnan(std::bit_cast(bits))) + { + return std::bit_cast(static_cast>(bits | quiet_bit)); + } + } + + return std::bit_cast(bits); +} + +// True on an excess precision target when the 80-bit result of some operation is beyond max +// but rounds to a finite value +template +auto in_rounding_band(const B x, const B y) -> bool +{ + if constexpr (excess_precision) + { + const auto lx {static_cast(x)}; + const auto ly {static_cast(y)}; + const auto max {static_cast(std::numeric_limits::max())}; + + for (const auto r : {lx + ly, lx - ly, lx * ly, lx / ly}) + { + const volatile B stored {static_cast(r)}; + if (std::isfinite(r) && std::fabs(r) > max && std::isfinite(static_cast(stored))) + { + return true; + } + } + } + else + { + static_cast(x); + static_cast(y); + } + + return false; +} + +// The edges crossed with themselves, random bit patterns (mostly huge or tiny magnitudes, +// so errors in random lanes), and moderate values that keep whole vectors error free +template +auto make_pairs() -> std::vector> +{ + std::vector> res; + const auto add {[&res](const B x, const B y) + { + if (!in_rounding_band(x, y)) + { + res.emplace_back(x, y); + } + }}; + + const auto edges {edge_values()}; + for (const auto x : edges) + { + for (const auto y : edges) + { + add(x, y); + } + } + + std::mt19937_64 gen {42U}; + std::uniform_int_distribution> bits {}; + std::uniform_real_distribution moderate {static_cast(-1000), static_cast(1000)}; + for (int i {}; i < 400; ++i) + { + const auto x {from_bits(bits(gen))}; + const auto y {from_bits(bits(gen))}; + add(x, y); + + const auto z {from_bits(bits(gen))}; + add(z, moderate(gen)); + } + + for (int i {}; i < 800; ++i) + { + const auto x {moderate(gen)}; + add(x, moderate(gen)); + } + + return res; +} + +// Chunks the pairs into vectors of N lanes, wrapping around for the last chunk +template +auto chunk(const std::vector>& pairs, const std::size_t first) + -> std::pair(N)>, std::array(N)>> +{ + std::array(N)> lhs {}; + std::array(N)> rhs {}; + + for (std::size_t i {}; i < static_cast(N); ++i) + { + const auto& p {pairs[(first + i) % pairs.size()]}; + lhs[i] = T{p.first}; + rhs[i] = T{p.second}; + } + + return {lhs, rhs}; +} + +// ------------------------------ +// Checks +// ------------------------------ + +template +void check_op(const char* name, const Op op) +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + + const auto pairs {make_pairs()}; + int failures {}; + + for (std::size_t first {}; first < pairs.size(); first += static_cast(N)) + { + // Named references rather than structured bindings, which lambdas can capture + // only from Clang 16 + const auto chunked {chunk(pairs, first)}; + const auto& lhs {chunked.first}; + const auto& rhs {chunked.second}; + + const auto scalar {capture([&] + { + std::vector res; + for (std::size_t i {}; i < static_cast(N); ++i) + { + res.push_back(canonical_element(op(lhs[i], rhs[i]))); + } + + return res; + })}; + + const auto vector {capture([&] + { + return lanes_of(op(simd::unchecked_load(lhs), simd::unchecked_load(rhs))); + })}; + + if (!BOOST_TEST(scalar == vector) && ++failures > 5) + { + // LCOV_EXCL_START + BOOST_ERROR(name); + return; + // LCOV_EXCL_STOP + } + } +} + +template +void check_overflowing(const char* name, const Op op) +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + + const auto pairs {make_pairs()}; + int failures {}; + + for (std::size_t first {}; first < pairs.size(); first += static_cast(N)) + { + const auto chunked {chunk(pairs, first)}; + const auto& lhs {chunked.first}; + const auto& rhs {chunked.second}; + + std::vector scalar; + std::vector scalar_flags; + for (std::size_t i {}; i < static_cast(N); ++i) + { + const auto r {op(lhs[i], rhs[i])}; + scalar.push_back(canonical_element(r.first)); + scalar_flags.push_back(r.second ? 1U : 0U); + } + + const auto r {op(simd::unchecked_load(lhs), simd::unchecked_load(rhs))}; + const auto ok {lanes_of(r.first) == scalar && flags_of(r.second, N) == scalar_flags}; + + if (!BOOST_TEST(ok) && ++failures > 5) + { + // LCOV_EXCL_START + BOOST_ERROR(name); + return; + // LCOV_EXCL_STOP + } + } +} + +// ------------------------------ +// Drivers +// ------------------------------ + +template +void test_operators() +{ + check_op("add", [](const auto& x, const auto& y) { return x + y; }); + check_op("sub", [](const auto& x, const auto& y) { return x - y; }); + check_op("mul", [](const auto& x, const auto& y) { return x * y; }); + check_op("div", [](const auto& x, const auto& y) { return x / y; }); +} + +template +void test_overflowing() +{ + check_overflowing("overflowing_add", [](const auto& x, const auto& y) { return overflowing_add(x, y); }); + check_overflowing("overflowing_sub", [](const auto& x, const auto& y) { return overflowing_sub(x, y); }); + check_overflowing("overflowing_mul", [](const auto& x, const auto& y) { return overflowing_mul(x, y); }); + check_overflowing("overflowing_div", [](const auto& x, const auto& y) { return overflowing_div(x, y); }); +} + +// Comparison masks, min, max, and clamp lane by lane against the scalar comparisons, +// std::min, std::max, and std::clamp, with NaN and both zeros in every operand position +template +void test_ordering() +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + + const auto edges {edge_values()}; + const auto pairs {make_pairs()}; + + for (std::size_t first {}; first < pairs.size(); first += static_cast(N)) + { + const auto chunked {chunk(pairs, first)}; + const auto& lhs {chunked.first}; + const auto& rhs {chunked.second}; + + const auto a {simd::unchecked_load(lhs)}; + const auto b {simd::unchecked_load(rhs)}; + + const auto eq {a == b}; + const auto ne {a != b}; + const auto lt {a < b}; + const auto le {a <= b}; + const auto gt {a > b}; + const auto ge {a >= b}; + const auto lo {simd::min(a, b)}; + const auto hi {simd::max(a, b)}; + + for (int i {}; i < N; ++i) + { + const auto x {lhs[static_cast(i)]}; + const auto y {rhs[static_cast(i)]}; + + BOOST_TEST_EQ(eq[i], x == y); + BOOST_TEST_EQ(ne[i], x != y); + BOOST_TEST_EQ(lt[i], x < y); + BOOST_TEST_EQ(le[i], x <= y); + BOOST_TEST_EQ(gt[i], x > y); + BOOST_TEST_EQ(ge[i], x >= y); + BOOST_TEST_EQ(canonical_element(lo[i]), canonical_element(std::min(x, y))); + BOOST_TEST_EQ(canonical_element(hi[i]), canonical_element(std::max(x, y))); + } + + // std::clamp requires ordered bounds: the edges in order, around every lane of a + for (std::size_t j {}; j + 1U < edges.size(); j += 3U) + { + const auto e1 {T{edges[j]}}; + const auto e2 {T{edges[j + 1U]}}; + if (!(e1 <= e2) && !(e2 <= e1)) + { + continue; + } + + const auto low {e2 < e1 ? e2 : e1}; + const auto high {e2 < e1 ? e1 : e2}; + const auto clamped {simd::clamp(a, V{low}, V{high})}; + + for (int i {}; i < N; ++i) + { + BOOST_TEST_EQ(canonical_element(clamped[i]), canonical_element(std::clamp(lhs[static_cast(i)], low, high))); + } + } + } + + // -0.0 and +0.0 compare equal, so min and max return the first operand. Constant + // vectors of zeros also pin the libstdc++ 16 select defect, which turned -0.0 into +0.0. + const V pos_zero {T{static_cast(0)}}; + const V neg_zero {T{-static_cast(0)}}; + const auto neg_zero_bits {canonical(-static_cast(0))}; + BOOST_TEST(all_of(pos_zero == neg_zero)); + BOOST_TEST_EQ(canonical_element(simd::min(neg_zero, pos_zero)[0]), neg_zero_bits); + BOOST_TEST_EQ(canonical_element(simd::min(pos_zero, neg_zero)[0]), canonical(static_cast(0))); + BOOST_TEST_EQ(canonical_element(simd::max(neg_zero, pos_zero)[0]), neg_zero_bits); + BOOST_TEST_EQ(canonical_element(simd::select(pos_zero < neg_zero, pos_zero, neg_zero)[N - 1]), neg_zero_bits); + BOOST_TEST_EQ(canonical_element(simd::clamp(neg_zero, neg_zero, pos_zero)[0]), neg_zero_bits); + BOOST_TEST_EQ(canonical_element(simd::reduce_min(neg_zero)), neg_zero_bits); + BOOST_TEST_EQ(canonical_element(simd::reduce_max(neg_zero)), neg_zero_bits); + + // A NaN lane of v stays NaN under clamp, as with std::clamp + const V nan {T{std::numeric_limits::quiet_NaN()}}; + BOOST_TEST(all_of(simd::clamp(nan, pos_zero, V{T{static_cast(1)}}) != nan)); + BOOST_TEST_EQ(canonical_element(simd::clamp(nan, pos_zero, V{T{static_cast(1)}})[0]), nan_bits); +} + +// reduce folds in lane order with the element's operator+, so it reports an error exactly +// where the scalar loop does. reduce_min and reduce_max require lanes without NaN. +template +void test_reductions() +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + + const V ramp {[](const auto i) { return T{static_cast(static_cast(i) % 2 == 0 ? static_cast(i) + 1 : -static_cast(i))}; }}; + + auto expected {ramp[0]}; + auto lowest {ramp[0]}; + auto highest {ramp[0]}; + for (int i {1}; i < N; ++i) + { + expected = expected + ramp[i]; + lowest = std::min(lowest, ramp[i]); + highest = std::max(highest, ramp[i]); + } + + BOOST_TEST_EQ(canonical_element(simd::reduce(ramp)), canonical_element(expected)); + BOOST_TEST_EQ(canonical_element(simd::reduce_min(ramp)), canonical_element(lowest)); + BOOST_TEST_EQ(canonical_element(simd::reduce_max(ramp)), canonical_element(highest)); + BOOST_TEST_EQ(canonical_element(simd::reduce(ramp, [](const T x, const T y) { return x * y; })), + canonical_element([&] + { + auto p {ramp[0]}; + for (int i {1}; i < N; ++i) + { + p = p * ramp[i]; + } + return p; + }())); + + if constexpr (N > 1) + { + // The running sum overflows at lane 1 under throw_exception + const V big {T{std::numeric_limits::max()}}; + if constexpr (std::is_same_v>) + { + BOOST_TEST_THROWS(static_cast(simd::reduce(big)), std::overflow_error); + } + } +} + +// select, broadcasting, default construction, raw lanes, and conversions between policies +template +void test_construction() +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + using S = simd::vec, N>; + + const V zero {}; + for (int i {}; i < N; ++i) + { + BOOST_TEST_EQ(canonical_element(zero[i]), canonical(static_cast(0))); + } + + const V v {[](const auto i) { return T{static_cast(static_cast(i)) + static_cast(0.25)}; }}; + const V w {T{static_cast(-1)}}; + const auto picked {simd::select(v > V{T{static_cast(1)}}, v, w)}; + for (int i {}; i < N; ++i) + { + BOOST_TEST_EQ(canonical_element(picked[i]), canonical(i > 0 ? static_cast(i) + static_cast(0.25) : static_cast(-1))); + } + + // select copies the lane bits unchanged, including a signaling NaN (which x87 quiets + // whenever it copies a value through its registers). The NaN is a run time value, since + // GCC may quiet a constant one while folding. + if constexpr (std::numeric_limits::has_signaling_NaN && !excess_precision) + { + const volatile auto snan_bits {std::bit_cast>(signaling_nan())}; + const auto expected {static_cast>(snan_bits)}; + + std::array(N)> raw {}; + raw.fill(std::bit_cast(expected)); + const V snan {raw}; + const auto kept {static_cast(N)>>(simd::select(zero == zero, snan, v))}; + for (const auto lane : kept) + { + BOOST_TEST_EQ(std::bit_cast>(lane), expected); + } + } + + // Broadcasting a T on either side + BOOST_TEST(all_of((v + T{static_cast(1)}) == (T{static_cast(1)} + v))); + BOOST_TEST(all_of((v * T{static_cast(2)}) == (v + v))); + BOOST_TEST(all_of((T{static_cast(0)} - v) < (v - v) || v == zero)); + BOOST_TEST(all_of((v / T{static_cast(2)}) * T{static_cast(2)} == v)); + + // Policy conversions copy every lane, even inf and NaN, whatever the policy + std::array(N)> specials {}; + for (std::size_t i {}; i < specials.size(); ++i) + { + const std::array values {std::numeric_limits::infinity(), std::numeric_limits::quiet_NaN(), + -std::numeric_limits::max(), static_cast(1.5)}; + specials[i] = values[i % values.size()]; + } + + const V from {specials}; + const S to {from}; + const V back {to}; + BOOST_TEST(lanes_of(to) == lanes_of(from)); + BOOST_TEST(lanes_of(back) == lanes_of(from)); + + // Raw lane round trip + const auto lanes {static_cast(N)>>(from)}; + for (std::size_t i {}; i < lanes.size(); ++i) + { + BOOST_TEST_EQ(canonical(lanes[i]), canonical(specials[i])); + } +} + +// Loads and stores of exactly the element type +template +void test_load_store() +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + + std::vector data; + for (int i {}; i < N + 3; ++i) + { + data.push_back(T{static_cast(i) * static_cast(-1.5)}); + } + + const auto v {simd::unchecked_load(data)}; + const auto p {simd::partial_load(data.data(), 2)}; + for (int i {}; i < N; ++i) + { + BOOST_TEST_EQ(canonical_element(v[i]), canonical_element(data[static_cast(i)])); + BOOST_TEST_EQ(canonical_element(p[i]), i < 2 ? canonical_element(data[static_cast(i)]) : canonical(static_cast(0))); + } + + std::vector out(data.size(), T{static_cast(9)}); + simd::unchecked_store(v + v, out); + simd::partial_store(v, out.data() + N, 1); + for (int i {}; i < N; ++i) + { + BOOST_TEST_EQ(canonical_element(out[static_cast(i)]), canonical_element(data[static_cast(i)] + data[static_cast(i)])); + } + + BOOST_TEST_EQ(canonical_element(out[static_cast(N)]), canonical_element(data[0])); + BOOST_TEST_EQ(canonical_element(out[static_cast(N) + 1U]), canonical(static_cast(9))); +} + +// Throwing, deferred, and saturating handling of the same overflow +template +void test_policies() +{ + using T = detail::float_basis; + using S = detail::float_basis; + using V = simd::vec; + using SV = simd::vec; + + // Only the last lane overflows + const V a {[](const auto i) { return T{static_cast(i) == N - 1 ? std::numeric_limits::max() : static_cast(1)}; }}; + + BOOST_TEST_THROWS(static_cast(a * T{static_cast(4)}), std::overflow_error); + BOOST_TEST_THROWS(static_cast((V{} - a) * T{static_cast(4)}), std::underflow_error); + BOOST_TEST_THROWS(static_cast(a / V{}), std::domain_error); + BOOST_TEST_THROWS(static_cast((a - a) / (a - a)), std::domain_error); + + const auto [product, overflowed] {simd::overflowing_mul(a, V{T{static_cast(4)}})}; + BOOST_TEST_EQ(reduce_count(overflowed), 1); + BOOST_TEST_EQ(reduce_min_index(overflowed), N - 1); + BOOST_TEST_EQ(canonical_element(product[N - 1]), canonical(std::numeric_limits::infinity())); + + // The deferred idiom: accumulate the masks and test once + auto errors {typename V::mask_type{false}}; + auto sum {a}; + for (int step {}; step < 3; ++step) + { + const auto next {simd::overflowing_add(sum, a)}; + sum = next.first; + errors = errors || next.second; + } + + BOOST_TEST_EQ(reduce_count(errors), 1); + + // sat_f32 and sat_f64 lanes are raw IEEE 754 and never throw + const SV s {static_cast(N)>>(a)}; + const auto saturated {s * S{static_cast(4)}}; + BOOST_TEST_EQ(canonical_element(saturated[N - 1]), canonical(std::numeric_limits::infinity())); + BOOST_TEST_EQ(canonical_element((s / SV{})[0]), canonical(std::numeric_limits::infinity())); + + static_assert(noexcept(s + s) && noexcept(s - s) && noexcept(s * s) && noexcept(s / s)); + static_assert(!noexcept(a + a) && !noexcept(a - a) && !noexcept(a * a) && !noexcept(a / a)); + static_assert(noexcept(simd::overflowing_div(a, a))); +} + +template +void test_width() +{ + using T = detail::float_basis; + using S = detail::float_basis; + using H = detail::float_basis; + + test_operators(); + test_operators(); + test_operators(); + + test_overflowing(); + test_overflowing(); + + test_ordering(); + test_reductions(); + test_reductions(); + test_construction(); + test_load_store(); + test_policies(); +} + +template +void test_type() +{ + test_width(); + test_width(); + test_width(); + test_width>::size()>(); + test_width(); +} + +int main() +{ + test_type(); + test_type(); + + return boost::report_errors(); +} diff --git a/test/test_simd_float_array.cpp b/test/test_simd_float_array.cpp new file mode 100644 index 0000000..658eae5 --- /dev/null +++ b/test/test_simd_float_array.cpp @@ -0,0 +1,9 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Runs test_simd_float.cpp on the portable std::array backend, which is otherwise +// not exercised wherever is available + +#define BOOST_SAFE_NUMBERS_SIMD_FORCE_ARRAY_BACKEND +#include "test_simd_float.cpp" diff --git a/test/test_simd_float_constexpr.cpp b/test/test_simd_float_constexpr.cpp new file mode 100644 index 0000000..b327e1b --- /dev/null +++ b/test/test_simd_float_constexpr.cpp @@ -0,0 +1,115 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// simd::vec of floating point elements is usable in constant expressions on both backends. +// The constant evaluators reject a result that overflows to infinity, so only finite lanes +// appear here; the lane counts 3, 5, and 7 cover the padded registers of AVX-512. + +// GCC 16's returns 32-byte vectors from internal helpers when AVX is not enabled, +// which -Wpsabi reports at the end of the translation unit even for plain std::simd code +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic ignored "-Wpsabi" +#endif + +#include + +#include +#include +#include +#include +#include + +using namespace boost::safe_numbers; + +template +using bits_t = std::conditional_t, std::uint32_t, std::uint64_t>; + +template +constexpr auto bits_of(const T x) -> bits_t +{ + return std::bit_cast>(static_cast(x)); +} + +// 0.5, 1.5, ..., 7.5, then repeating: every sum, product, and quotient below is exact +template +constexpr auto ramp() -> simd::vec +{ + using basis_type = typename T::basis_type; + return simd::vec{[](const auto i) { return T{static_cast(static_cast(i) % 8) + static_cast(0.5)}; }}; +} + +template +constexpr auto check() -> bool +{ + using basis_type = typename T::basis_type; + using V = simd::vec; + using sat_type = detail::float_basis; + using S = simd::vec; + + const auto a {ramp()}; + const V two {T{static_cast(2)}}; + const V half {T{static_cast(0.5)}}; + + const auto sum {a + two}; + const auto difference {sum - two}; + const auto product {a * two}; + const auto quotient {product / two}; + + const auto [checked_sum, sum_flags] {simd::overflowing_add(a, half)}; + const auto [checked_difference, difference_flags] {simd::overflowing_sub(a, half)}; + const auto [checked_product, product_flags] {simd::overflowing_mul(a, half)}; + const auto [checked_quotient, quotient_flags] {simd::overflowing_div(a, half)}; + + const S sat_a {a}; + const auto saturated {(sat_a * S{sat_type{static_cast(4)}}) / S{sat_type{static_cast(4)}}}; + const V back {saturated}; + + // Signed zeros select by bit pattern, so -0.0 survives min, max, and the reductions + const V pos_zero {T{static_cast(0)}}; + const V neg_zero {T{-static_cast(0)}}; + const auto neg_zero_bits {std::bit_cast>(-static_cast(0))}; + + // Lane i of a is i % 8 + 0.5, so the extremes are 0.5 and min(N, 8) - 0.5 + const auto top {T{static_cast(N < 8 ? N : 8) - static_cast(0.5)}}; + + return all_of(difference == a) && + all_of(quotient == a) && + all_of(product == a + a) && + all_of(checked_sum == a + half) && none_of(sum_flags) && + all_of(checked_difference == a - half) && none_of(difference_flags) && + all_of(checked_product * two == a) && none_of(product_flags) && + all_of(checked_quotient == product) && none_of(quotient_flags) && + all_of(back == a) && + none_of(a < a) && all_of(a <= a) && all_of(a >= half) && none_of(a != a) && + all_of(simd::min(a, two) <= two) && + all_of(simd::max(a, two) >= two) && + all_of(simd::clamp(a, half, two) <= two) && + all_of(simd::select(a < two, a, two) == simd::min(a, two)) && + simd::reduce_min(a) == T{static_cast(0.5)} && + simd::reduce_max(a) == top && + (N > 8 || simd::reduce(a) == T{static_cast(N * N) / static_cast(2)}) && + bits_of(simd::min(neg_zero, pos_zero)[0]) == neg_zero_bits && + bits_of(simd::max(neg_zero, pos_zero)[0]) == neg_zero_bits && + bits_of(simd::reduce_min(neg_zero)) == neg_zero_bits && + bits_of(simd::reduce_max(neg_zero)) == neg_zero_bits && + std::bit_cast>(static_cast(N)>>(a)[0]) == + std::bit_cast>(static_cast(0.5)); +} + +template +constexpr auto check_widths() -> bool +{ + return check() && check() && check() && check() && check() && + check() && check::size()>(); +} + +static_assert(check_widths()); +static_assert(check_widths()); +static_assert(check_widths()); +static_assert(check_widths()); + +int main() +{ + return 0; +} diff --git a/test/test_simd_float_constexpr_array.cpp b/test/test_simd_float_constexpr_array.cpp new file mode 100644 index 0000000..b0c5347 --- /dev/null +++ b/test/test_simd_float_constexpr_array.cpp @@ -0,0 +1,9 @@ +// Copyright 2026 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Runs test_simd_float_constexpr.cpp on the portable std::array backend, which is +// otherwise not exercised wherever is available + +#define BOOST_SAFE_NUMBERS_SIMD_FORCE_ARRAY_BACKEND +#include "test_simd_float_constexpr.cpp"