Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* xref:unsigned_integers.adoc[]
* xref:signed_integers.adoc[]
* xref:floats.adoc[]
* xref:simd.adoc[]
* xref:bounded_uint.adoc[]
* xref:bounded_int.adoc[]
* xref:bounded_float.adoc[]
Expand Down
43 changes: 43 additions & 0 deletions doc/modules/ROOT/pages/api_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ https://www.boost.org/LICENSE_1_0.txt
| Name | Description
| `boost::charconv` | `to_chars` and `from_chars` overloads
| `boost::safe_numbers` | All other parts of the library
| `boost::safe_numbers::simd` | `vec` and its free functions, from `<boost/safe_numbers/simd.hpp>`
|===

[#api_types]
Expand Down Expand Up @@ -118,6 +119,16 @@ https://www.boost.org/LICENSE_1_0.txt
| Safe floating-point value constrained to a compile-time range `[Min, Max]`
|===

=== SIMD Vectors

[cols="1,2", options="header"]
|===
| Type | Description

| xref:simd.adoc[`simd::vec<T, N>`]
| `N` safe values of type `T` checked a whole vector at a time, on `std::simd` when available and a portable backend otherwise
|===

=== Enumerations

[cols="1,2", options="header"]
Expand Down Expand Up @@ -438,6 +449,34 @@ These wrap the standard `<cmath>` functions for the floating-point types `f32` a
| Generic policy-parameterized arithmetic (takes `overflow_policy` as template parameter)
|===

=== SIMD Vectors

[cols="1,2", options="header"]
|===
| 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

| xref:simd.adoc[`widening_add`, `widening_mul`]
| Exact results in lanes twice as wide

| xref:simd.adoc[`add<Policy>`, `sub<Policy>`, `mul<Policy>`, `div<Policy>`, `mod<Policy>`, `shl<Policy>`, `shr<Policy>`]
| Vector operations with the policy chosen at the call site (`shl` and `shr` for unsigned elements only)

| xref:simd.adoc[`select`, `min`, `max`, `clamp`]
| Lane by lane selection and ordering

| xref:simd.adoc[`reduce`, `reduce_min`, `reduce_max`]
| Reductions; `reduce` folds left in lane order with the element's operation

| xref:simd.adoc[`all_of`, `any_of`, `none_of`, `reduce_count`, `reduce_min_index`, `reduce_max_index`]
| Mask reductions

| xref:simd.adoc[`unchecked_load`, `partial_load`, `unchecked_store`, `partial_store`]
| Loads and stores of contiguous ranges of the element type
|===

== `<numeric>`

=== `gcd`
Expand Down Expand Up @@ -512,6 +551,10 @@ This header is not included in the convenience header since it requires external
| `<boost/safe_numbers/floats.hpp>`
| All safe floating-point types (`f32`, `f64`)

| `<boost/safe_numbers/simd.hpp>`
| `simd::vec<T, N>` and its free functions.
This header is not included in the convenience header

| `<boost/safe_numbers/bounded_integers.hpp>`
| Bounded unsigned and signed integer types (`bounded_uint<Min, Max>`, `bounded_int<Min, Max>`)

Expand Down
363 changes: 363 additions & 0 deletions doc/modules/ROOT/pages/simd.adoc

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions examples/simd_signed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 <simd> 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 <boost/safe_numbers/simd.hpp>
#include <boost/safe_numbers/iostream.hpp>
#include <array>
#include <cstdint>
#include <iostream>
#include <limits>
#include <stdexcept>

int main()
{
using namespace boost::safe_numbers;
using vec4 = simd::vec<i32, 4>;

// Balances and the changes to apply to them
const std::array<i32, 4> balances {i32{1'000}, i32{-2'147'483'000}, i32{2'000'000'000}, i32{-50}};
const std::array<i32, 4> changes {i32{-250}, i32{-1'000}, i32{500'000'000}, i32{75}};

const auto b {simd::unchecked_load<vec4>(balances)};
const auto c {simd::unchecked_load<vec4>(changes)};

// Lane 1 underflows and lane 2 overflows. The lowest failing lane decides what is
// thrown, exactly as a scalar loop over the lanes would.
try
{
const auto totals {b + c};
static_cast<void>(totals);
}
catch (const std::underflow_error& e)
{
std::cout << "Error: " << e.what() << '\n';
}

// overflowing_add returns the wrapped lanes and a mask of the lanes that failed
const auto [wrapped, failed] {simd::overflowing_add(b, c)};
for (int i {}; i < vec4::size(); ++i)
{
std::cout << "lane " << i << ": " << wrapped[i] << (failed[i] ? " (out of range)" : "") << '\n';
}

// sat_i32 lanes clamp toward the side the result left the range on
using sat_vec4 = simd::vec<sat_i32, 4>;
const auto clamped {sat_vec4{b} + sat_vec4{c}};
std::cout << "saturated:";
for (int i {}; i < sat_vec4::size(); ++i)
{
std::cout << ' ' << clamped[i];
}
std::cout << '\n';

// Negation and division are checked too: -min and min / -1 do not fit
const vec4 lowest {i32{std::numeric_limits<std::int32_t>::min()}};
try
{
const auto negated {-lowest};
static_cast<void>(negated);
}
catch (const std::domain_error& e)
{
std::cout << "Error: " << e.what() << '\n';
}

const auto [quotients, overflowed] {simd::overflowing_div(lowest, vec4{i32{-1}})};
std::cout << "lanes where min / -1 overflowed: " << reduce_count(overflowed)
<< " (wrapped quotient " << quotients[0] << ")\n";

// As for the scalar signed types, bitwise and shift operators do not compile:
// b & c; // static_assert: Bitwise AND is deliberately disabled for signed safe integers

return 0;
}
78 changes: 78 additions & 0 deletions examples/simd_unsigned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 <simd> 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 <boost/safe_numbers/simd.hpp>
#include <boost/safe_numbers/iostream.hpp>
#include <array>
#include <iostream>
#include <stdexcept>

int main()
{
using namespace boost::safe_numbers;
using vec4 = simd::vec<u32, 4>;

// Lanes load from a contiguous range of exactly the element type
const std::array<u32, 4> prices {u32{100U}, u32{250U}, u32{3'000'000'000U}, u32{7U}};
const std::array<u32, 4> counts {u32{3U}, u32{2U}, u32{2U}, u32{10U}};

const auto p {simd::unchecked_load<vec4>(prices)};
const auto c {simd::unchecked_load<vec4>(counts)};

// One check covers every lane: lane 2 overflows, so the whole multiplication
// throws exactly what the scalar u32 operation throws
try
{
const auto totals {p * c};
static_cast<void>(totals);
}
catch (const std::overflow_error& e)
{
std::cout << "Error: " << e.what() << '\n';
}

// overflowing_mul returns the wrapped lanes and a mask of the lanes that overflowed
const auto [wrapped, overflowed] {simd::overflowing_mul(p, c)};
for (int i {}; i < vec4::size(); ++i)
{
std::cout << "lane " << i << ": " << wrapped[i] << (overflowed[i] ? " (overflowed)" : "") << '\n';
}

// The error policy comes from the element type: sat_u32 lanes clamp instead
using sat_vec4 = simd::vec<sat_u32, 4>;
const auto clamped {sat_vec4{p} * sat_vec4{c}};
std::cout << "saturated lane 2: " << clamped[2] << '\n';

// Deferred checking: accumulate the masks and test once at the end
auto sum {vec4{}};
auto errors {vec4::mask_type{false}};
for (int step {}; step < 3; ++step)
{
const auto [next, carry] {simd::overflowing_add(sum, p)};
sum = next;
errors = errors || carry;
}

std::cout << "lanes that overflowed while summing: " << reduce_count(errors) << '\n';

// Elementwise operations without errors store straight back to safe values
std::array<u32, 4> out {};
simd::unchecked_store(simd::min(p, c * u32{50U}), out);
std::cout << "min(p, 50 * c) =";
for (const auto x : out)
{
std::cout << ' ' << x;
}
std::cout << '\n';

return 0;
}
Loading
Loading