Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vector_sum.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Fast summation of arrays (vectors, spans) of numbers.
15//
16// Speed: up to 2x faster than Eigen for float arrays with ~100 elements or more
17// (as of 2023-05).
18// Precision: Better or comparable precision to std::accumulate<> on the same
19// value type. That said, the precision is inferior to precise sum
20// algorithm such as ::AccurateSum.
21
22#ifndef OR_TOOLS_UTIL_VECTOR_SUM_H_
23#define OR_TOOLS_UTIL_VECTOR_SUM_H_
24
25#include "absl/types/span.h"
27
28namespace operations_research {
29
30// Computes the sum of `values`, assuming that the first element of `values` is
31// aligned to 16 bytes.
32inline float AlignedVectorSum(absl::Span<const float> values) {
33 return internal::VectorSum<4, 4, /*assume_aligned_at_start=*/true>(values);
34}
35
36// Computes the sum of `values` without assuming anything.
37inline float VectorSum(absl::Span<const float> values) {
38 return internal::VectorSum<4, 4, /*assume_aligned_at_start=*/false>(values);
39}
40
41} // namespace operations_research
42
43#endif // OR_TOOLS_UTIL_VECTOR_SUM_H_
Value VectorSum(absl::Span< const Value > values)
In SWIG mode, we don't want anything besides these top-level includes.
float VectorSum(absl::Span< const float > values)
Computes the sum of values without assuming anything.
Definition vector_sum.h:37
float AlignedVectorSum(absl::Span< const float > values)
Definition vector_sum.h:32