numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
basic_operations.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21#pragma once
22
23#include <tuple>
24
25#ifdef __AVX2__
26#include <emmintrin.h>
27#include <immintrin.h>
28#endif
29
30#ifdef __FAST_MATH__
31#warning "Use of -ffast-math is unsafe for multi_double module."
32#endif
33
35
44inline auto quick_two_sum(double a, double b) -> std::tuple<double, double> {
45 const double s = a + b;
46 const double e = b - (s - a);
47 return {s, e};
48}
49
57inline auto two_sum(double a, double b) -> std::tuple<double, double> {
58 const double s = a + b;
59 const double v = s - a;
60 const double e = (a - (s - v)) + (b - v);
61 return {s, e};
62}
63
70inline auto split(double a) -> std::tuple<double, double> {
71 constexpr double coeff = 0x1.0p+27 + 1.0;
72 const double t = coeff * a;
73 const double a_h = t - (t - a);
74 const double a_l = a - a_h;
75 return {a_h, a_l};
76}
77
86inline auto two_prod_no_fma(double a, double b) -> std::tuple<double, double> {
87 const double p = a * b;
88 const auto [a_h, a_l] = split(a);
89 const auto [b_h, b_l] = split(b);
90 const double e = ((a_h * b_h - p) + a_h * b_l + a_l * b_h) + a_l * b_l;
91 return {p, e};
92}
93
94#ifdef __AVX2__
95
104inline auto two_prod_fma(double a, double b) -> std::tuple<double, double> {
105 const double p = a * b;
106 const __m128d a_mm = _mm_set_sd(a);
107 const __m128d b_mm = _mm_set_sd(b);
108 const __m128d p_mm = _mm_set_sd(p);
109 const __m128d e_mm = _mm_fmsub_sd(a_mm, b_mm, p_mm);
110 double e = 0.0;
111 _mm_store_sd(&e, e_mm);
112 return {p, e};
113}
114
115#endif
116
124inline auto two_prod(double a, double b) -> std::tuple<double, double> {
125#ifdef __AVX2__
126 return two_prod_fma(a, b);
127#else
128 return two_prod_no_fma(a, b);
129#endif
130}
131
132} // namespace num_collect::multi_double::impl
Namespace of internal implementations.
auto split(double a) -> std::tuple< double, double >
split a number to higher bits and lower bits
auto two_sum(double a, double b) -> std::tuple< double, double >
calculate sum of a and b, and error of the sum
auto two_prod_no_fma(double a, double b) -> std::tuple< double, double >
calculate product of a and b, and error of the product without FMA instructions
auto two_prod(double a, double b) -> std::tuple< double, double >
calculate product of a and b, and error of the product
auto quick_two_sum(double a, double b) -> std::tuple< double, double >
calculate sum of a and b, and error of the sum on the condition that absolute value of a is larger th...