Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
dot.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::dot"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_DOT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_DOT_HPP_
7
11#include "arene/base/linalg/detail/constraints_for.hpp"
12#include "arene/base/linalg/detail/mandates.hpp"
13#include "arene/base/linalg/detail/preconditions.hpp"
16#include "arene/base/stdlib_choice/common_type.hpp"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/is_assignable.hpp"
20#include "arene/base/stdlib_choice/move.hpp"
23
24// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
25// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
26
27namespace arene {
28namespace base {
29namespace linalg {
30namespace detail {
31
32/// @brief The type resulting from multiplying the two parameter types together; alias for brevity
33/// @tparam LeftValue The left type to multiply
34/// @tparam RightValue The right type to multiply
35template <typename LeftValue, typename RightValue>
36using mul_result_t = decltype(std::declval<LeftValue>() * std::declval<RightValue>());
37
38/// @brief Whether or not the given reference types support the necessary operations to use them in a dot product
39/// @tparam LeftRef The left of the two reference types used in the dot product
40/// @tparam RightRef The right of the two reference types used in the dot product
41/// @tparam Result The type used to accumulate the entries of the dot product; defaults to @c LeftRef*RightRef
42template <
43 typename LeftRef,
44 typename RightRef,
45 typename Result = mul_result_t<LeftRef, RightRef>,
46 typename = constraints<>>
47constexpr bool dot_ops_supported_v{false};
48
49/// @brief Whether or not the given reference types support the necessary operations to use them in a dot product
50/// @tparam LeftRef The left of the two reference types used in the dot product
51/// @tparam RightRef The right of the two reference types used in the dot product
52/// @tparam Result The type used to accumulate the entries of the dot product; defaults to @c LeftRef*RightRef
53template <typename LeftRef, typename RightRef, typename Result>
54constexpr bool dot_ops_supported_v<
55 LeftRef,
56 RightRef,
57 Result,
59 Result&,
61
62/// @brief The default result type for a dot product when no scalar accumulator is provided
63/// @tparam LeftVec The left of the two vector types used in the dot product
64/// @tparam RightVec The right of the two vector types used in the dot product
65/// @note This follows the C++ standard in [linalg.algs.blas1.dot] in using @c value_type instead of <c>reference</c>,
66/// even though the actual type multiplied is <c>reference</c>, because the latter may be a proxy type unsuitable for
67/// data.
68template <typename LeftVec, typename RightVec>
69using default_dot_result_t = mul_result_t<typename LeftVec::value_type, typename RightVec::value_type>;
70
71/// @brief constraints specialization for @c dot
72template <>
73class algorithm_constraints<class dot_fn> {
74 public:
75 /// @brief constraints for @c dot
76 /// @tparam InVec input vector type (a rank-1 specialization of @c mdspan)
77 /// @tparam Scalar scalar/accumulator type
78 /// @tparam Norm elementwise norm function object
79 template <typename LeftVec, typename RightVec, typename Scalar = default_dot_result_t<LeftVec, RightVec>>
80 using type = arene::base::constraints<
85};
86
87/// @brief Class to implement @c dot
88class dot_fn {
89 public:
90 /// @brief Compute the dot product of two vectors with a given initial value
91 /// @tparam LeftVec The type of the first vector (a rank-1 specialization of <c>mdspan</c>)
92 /// @tparam RightVec The type of the second vector (a rank-1 specialization of <c>mdspan</c>)
93 /// @tparam Scalar The scalar type of the initial value
94 /// @param left_vec The first vector
95 /// @param right_vec The second vector
96 /// @param init The initial value to which the dot product result is added
97 /// @pre @c left_vec and @c right_vec are both instantiations of @c mdspan with matching 1-dimensional extents,
98 /// otherwise @c ARENE_PRECONDITION violation
99 /// @pre The dot product of @c left_vec and @c right_vec would not overflow <c>init</c>, otherwise the behaviour is
100 /// undefined
101 /// @return The sum of the product of each entry of @c left_vec with each matching entry of @c right_vec
102 /// @note This version with the initial value corresponds to the SDSDOT function from BLAS 1.
103 template <
104 typename LeftVec,
105 typename RightVec,
106 typename Scalar,
107 constraints_for<dot_fn, LeftVec, RightVec, Scalar> = nullptr>
108 constexpr auto operator()(LeftVec const left_vec, RightVec const right_vec, Scalar init) const
109 noexcept(noexcept(init = std::move(init) + (left_vec(0U) * right_vec(0U)))) -> Scalar {
110 static_assert(possibly_comparable_v<LeftVec, RightVec>, "Operands to dot must have the same extents");
111
112 ARENE_PRECONDITION(actually_comparable(left_vec, right_vec));
113
115
116 // parasoft-begin-suppress AUTOSAR-A6_5_2-a "False positive: the loop does contain a loop counter"
117 for (index_type idx{}; cmp_less(idx, left_vec.extent(0U)); ++idx) {
118 // Writing like this instead of *= so that it works for types with @c operator* defined but no <c>operator*=</c>.
119 init = static_cast<Scalar>(std::move(init) + (left_vec(idx) * right_vec(idx)));
120 }
121 // parasoft-end-suppress AUTOSAR-A6_5_2-a
122
123 return std::move(init);
124 }
125
126 /// @brief Compute the dot product of two vectors
127 /// @tparam LeftVec The type of the first vector (a rank-1 specialization of <c>mdspan</c>)
128 /// @tparam RightVec The type of the second vector (a rank-1 specialization of <c>mdspan</c>)
129 /// @param left_vec The first vector
130 /// @param right_vec The second vector
131 /// @pre @c left_vec and @c right_vec are both instantiations of @c mdspan with matching 1-dimensional extents,
132 /// otherwise @c ARENE_PRECONDITION violation
133 /// @pre The dot product of @c left_vec and @c right_vec would not overflow
134 /// <c>default_dot_result_t<LeftVec, RightVec></c>, otherwise the behaviour is undefined
135 /// @return The sum of the product of each entry of @c left_vec with each matching entry of @c right_vec
136 /// @note This version without the initial value corresponds to the SDOT and DDOT functions from BLAS 1.
137 template <typename LeftVec, typename RightVec, constraints_for<dot_fn, LeftVec, RightVec> = nullptr>
138 constexpr auto operator()(LeftVec const left_vec, RightVec const right_vec) const noexcept(noexcept(
140 )) -> default_dot_result_t<LeftVec, RightVec> {
141 return (*this)(left_vec, right_vec, numbers::zero_v<default_dot_result_t<LeftVec, RightVec>>);
142 }
143};
144
145} // namespace detail
146
147// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
148// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
149/// @brief Compute the dot product of two vectors.
150ARENE_CPP14_INLINE_VARIABLE(detail::dot_fn, dot);
151// parasoft-end-suppress AUTOSAR-M7_3_3-a
152// parasoft-end-suppress CERT_CPP-DCL59-a
153
154} // namespace linalg
155} // namespace base
156} // namespace arene
157
158// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
159// parasoft-end-suppress AUTOSAR-A7_1_5-a
160
161#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_DOT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
constexpr const & zero_v
constexpr reference to the additive identity value zero<T>::value .
Definition arithmetic_identities.hpp:58
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename common_type< Ts... >::type common_type_t
Helper alias template for common_type.
Definition common_type.hpp:99
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
Provides backports of the safe integer comparison functions from C++20.