RayZaler 0.1
The free opto-mechanical simulation framework
Helpers.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _LIBRZ_HELPERS_H
20#define _LIBRZ_HELPERS_H
21
22#include <cstring>
23#include <string>
24#include <list>
25#include <cstdarg>
26#include <stdexcept>
27#include <type_traits>
28#include <vector>
29#include <algorithm>
30#include <cctype>
31#include <cstdint>
32
33#define _JOIN(a, b) a ## b
34#define JOIN(a, b) _JOIN(a, b)
35
36#define _STRINGFY(a) #a
37#define STRINGFY(a) _STRINGFY(a)
38
39std::string string_vprintf(const char* fmt, va_list ap);
40std::string string_printf(const char* fmt, ...);
41
42std::vector<std::string> operator / (std::string const &, std::string const &);
43std::vector<std::string> operator / (std::string const &, char sep);
44
45static inline bool
46iequals(const std::string &a, const std::string &b)
47{
48 return a.size() == b.size() &&
49 std::equal(
50 a.begin(),
51 a.end(),
52 b.begin(),
53 [](char c_a, char c_b) {
54 return std::tolower(static_cast<unsigned char>(c_a)) ==
55 std::tolower(static_cast<unsigned char>(c_b));
56 });
57}
58
59#if defined(__GNUC__)
60# pragma GCC push_options
61# pragma GCC optimize ("O1")
62#endif
63
64namespace RZ {
65 template<typename T>
66 struct is_real {
67 static constexpr bool value = false;
68 };
69
70 template<>
71 struct is_real<float> {
72 static constexpr bool value = true;
73 };
74
75 template<>
76 struct is_real<double> {
77 static constexpr bool value = true;
78 };
79};
80
81template<typename T> T
82sumPrecise (const T *data, size_t N)
83{
84 T sum = 0;
85
86 if constexpr (RZ::is_real<T>::value) {
87 T c = 0;
88 T y, t;
89
90 while (N-- > 0) {
91 y = data[N] - c;
92 t = sum + y;
93 c = (t - sum) - y;
94 sum = t;
95 }
96 } else {
97 while (N-- > 0)
98 sum += data[N];
99 }
100
101 return sum;
102}
103
104template<typename T> T
105sumPrecise (std::list<T> const &data)
106{
107 T sum = 0;
108 auto it = data.begin();
109
110 if constexpr (RZ::is_real<T>::value) {
111 T c = 0;
112 T y, t;
113
114 while (it != data.end()) {
115 y = *it++ - c;
116 t = sum + y;
117 c = (t - sum) - y;
118 sum = t;
119 }
120 } else {
121 while (it != data.end())
122 sum += *it++;
123 }
124
125 return sum;
126}
127
128
129#if defined(__GNUC__)
130# pragma GCC pop_options
131#endif
132
133#endif // _LIBRZ_HELPERS_H
Definition: Helpers.h:66