Skip to content

Commit 63097c7

Browse files
committed
quadmath_cpp version 1
0 parents  commit 63097c7

File tree

7 files changed

+266
-0
lines changed

7 files changed

+266
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
desktop.ini
2+
*/build/

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 ZERICO2005
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
quadmath_cpp provides C++ overloads for __float128 and quadmath.h. This allows you to use quadmath.h in templated functions. Instead of calling sinq(x), you can just call sin(x).
2+
3+
quadmath_cpp also includes C++11 <cmath> functions not present in quadmath.h, such as islessgreater(x, y) and fpclassify(x)
4+
5+
Include the quadmath_cpp.h header in your code to use it. The header should compile fine on GCC from C++98 to C++23

quadmath_cpp.h

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
** Author: zerico2005 (2024)
3+
** Project: quadmath_cpp
4+
** License: MIT License
5+
** A copy of the MIT License should be included with
6+
** this project. If not, see https://opensource.org/license/MIT
7+
*/
8+
9+
#ifndef QUADMATH_CPP_H
10+
#define QUADMATH_CPP_H
11+
12+
//------------------------------------------------------------------------------
13+
// <quadmath.h> overloads
14+
//------------------------------------------------------------------------------
15+
16+
#include <cmath>
17+
#include <quadmath.h>
18+
19+
/* Classification */
20+
21+
inline bool signbit(__float128 x) { return (signbitq(x) != 0); }
22+
inline bool isfinite(__float128 x) { return (finiteq(x) != 0); }
23+
inline bool isinf(__float128 x) { return (isinfq(x) != 0); }
24+
inline bool isnan(__float128 x) { return (isnanq(x) != 0); }
25+
inline bool issignaling(__float128 x) { return (issignalingq(x) != 0); }
26+
27+
/* Manipulation */
28+
29+
inline __float128 fabs(__float128 x) { return fabsq(x); }
30+
inline __float128 copysign(__float128 x, __float128 y) { return copysignq(x, y); }
31+
inline __float128 nextafter(__float128 x, __float128 y) { return nextafterq(x, y); }
32+
33+
/* Float Exponents */
34+
35+
inline int ilogb(__float128 x) { return ilogbq(x); }
36+
inline __float128 frexp (__float128 x, int* exp) { return frexpq (x, exp); }
37+
inline __float128 ldexp (__float128 x, int exp) { return ldexpq (x, exp); }
38+
inline __float128 scalbn (__float128 x, int exp) { return scalbnq (x, exp); }
39+
inline __float128 scalbln(__float128 x, long exp) { return scalblnq(x, exp); }
40+
41+
/* Arithmetic */
42+
43+
inline __float128 fmax(__float128 x, __float128 y) { return fmaxq(x, y); }
44+
inline __float128 fmin(__float128 x, __float128 y) { return fminq(x, y); }
45+
inline __float128 fdim(__float128 x, __float128 y) { return fdimq(x, y); }
46+
inline __float128 fma(__float128 x, __float128 y, __float128 z) { return fmaq(x, y, z); }
47+
inline __float128 sqrt(__float128 x) { return sqrtq(x); }
48+
inline __float128 cbrt(__float128 x) { return cbrtq(x); }
49+
inline __float128 hypot(__float128 x, __float128 y) { return hypotq(x, y); }
50+
51+
/* Remainder and Modulus */
52+
53+
inline __float128 fmod(__float128 x, __float128 y) { return fmodq(x, y); }
54+
inline __float128 modf(__float128 x, __float128* int_part) { return modfq(x, int_part); }
55+
inline __float128 remainder(__float128 x, __float128 y) { return remainderq(x, y); }
56+
inline __float128 remquo(__float128 x, __float128 y, int* quo) { return remquoq(x, y, quo); }
57+
58+
/* Rounding */
59+
60+
inline __float128 trunc(__float128 x) { return truncq(x); }
61+
inline __float128 floor(__float128 x) { return floorq(x); }
62+
inline __float128 ceil (__float128 x) { return ceilq (x); }
63+
inline __float128 rint (__float128 x) { return rintq (x); }
64+
inline __float128 round(__float128 x) { return roundq(x); }
65+
inline long lrint (__float128 x) { return lrintq (x); }
66+
inline long lround(__float128 x) { return lroundq(x); }
67+
inline long long llrint (__float128 x) { return llrintq (x); }
68+
inline long long llround(__float128 x) { return llroundq(x); }
69+
inline __float128 nearbyint(__float128 x) { return nearbyintq(x); }
70+
71+
/* Logarithms and Exponents */
72+
73+
inline __float128 log (__float128 x) { return logq (x); }
74+
inline __float128 log1p(__float128 x) { return log1pq(x); }
75+
inline __float128 logb (__float128 x) { return logbq (x); }
76+
inline __float128 log2 (__float128 x) { return log2q (x); }
77+
inline __float128 log10(__float128 x) { return log10q(x); }
78+
inline __float128 exp (__float128 x) { return expq (x); }
79+
inline __float128 expm1(__float128 x) { return expm1q(x); }
80+
inline __float128 exp2 (__float128 x) { return exp2q (x); }
81+
inline __float128 pow(__float128 x, __float128 y) { return powq(x, y); }
82+
83+
/* Trigonometry */
84+
85+
inline __float128 sin (__float128 x) { return sinq (x); }
86+
inline __float128 cos (__float128 x) { return cosq (x); }
87+
inline __float128 tan (__float128 x) { return tanq (x); }
88+
inline __float128 asin (__float128 x) { return asinq (x); }
89+
inline __float128 acos (__float128 x) { return acosq (x); }
90+
inline __float128 atan (__float128 x) { return atanq (x); }
91+
inline __float128 sinh(__float128 x) { return sinhq(x); }
92+
inline __float128 cosh(__float128 x) { return coshq(x); }
93+
inline __float128 tanh(__float128 x) { return tanhq(x); }
94+
inline __float128 asinh(__float128 x) { return asinhq(x); }
95+
inline __float128 acosh(__float128 x) { return acoshq(x); }
96+
inline __float128 atanh(__float128 x) { return atanhq(x); }
97+
inline __float128 atan2(__float128 y, __float128 x) { return atan2q(y, x); }
98+
inline void sincos(__float128 x, __float128* p_sin, __float128* p_cos) { sincosq(x, p_sin, p_cos); }
99+
100+
/* Transcendental Functions */
101+
102+
inline __float128 erf (__float128 x) { return erfq (x); }
103+
inline __float128 erfc(__float128 x) { return erfcq(x); }
104+
inline __float128 lgamma(__float128 x) { return lgammaq(x); }
105+
inline __float128 tgamma(__float128 x) { return tgammaq(x); }
106+
107+
//------------------------------------------------------------------------------
108+
// C++11 <cmath> overloads
109+
//------------------------------------------------------------------------------
110+
111+
/* Manipulation */
112+
113+
inline __float128 nexttoward(__float128 x, long double y) {
114+
return nextafterq(x, static_cast<__float128>(y));
115+
}
116+
117+
/* Compairison */
118+
119+
inline bool isgreater(__float128 x, __float128 y) {
120+
return (x > y);
121+
}
122+
inline bool isgreaterequal(__float128 x, __float128 y) {
123+
return (x >= y);
124+
}
125+
inline bool isless(__float128 x, __float128 y) {
126+
return (x < y);
127+
}
128+
inline bool islessequal(__float128 x, __float128 y) {
129+
return (x <= y);
130+
}
131+
inline bool islessgreater(__float128 x, __float128 y) {
132+
return (x < y) || (x > y);
133+
}
134+
135+
/* Classification */
136+
137+
inline bool isunordered(__float128 x, __float128 y) {
138+
return (isnanq(x) != 0) || (isnanq(y) != 0);
139+
}
140+
141+
inline bool isnormal(__float128 x) {
142+
return (finiteq(x) != 0 && fabsq(x) >= FLT128_MIN);
143+
}
144+
145+
inline int fpclassify(__float128 x) {
146+
return
147+
isinfq(x) ? FP_INFINITE :
148+
isnanq(x) ? FP_NAN :
149+
x == static_cast<__float128>(0.0) ? FP_ZERO :
150+
isnormal(x) ? FP_NORMAL :
151+
FP_SUBNORMAL;
152+
}
153+
154+
//------------------------------------------------------------------------------
155+
// Additional overloads
156+
//------------------------------------------------------------------------------
157+
158+
/** @brief calls `exp(x * M_LN10q)` */
159+
inline __float128 exp10(__float128 x) {
160+
return expq(x * M_LN10q);
161+
}
162+
163+
/** @brief calls `*p_sinh = sinhq(x); *p_cosh = coshq(x);` */
164+
inline void sinhcosh(__float128 x, __float128* p_sinh, __float128* p_cosh) {
165+
*p_sinh = sinhq(x);
166+
*p_cosh = coshq(x);
167+
}
168+
169+
#endif /* QUADMATH_CPP_H */

test/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
set(SRC_DIR "./src")
4+
5+
set(PROJECT_NAME "quadmath-cpp-test")
6+
project(${PROJECT_NAME})
7+
8+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "./bin")
9+
10+
# Compiling
11+
set(CMAKE_C_COMPILER "gcc")
12+
set(CMAKE_CXX_COMPILER "g++")
13+
14+
# Set C and C++ standards
15+
set(CMAKE_C_STANDARD 99)
16+
set(CMAKE_CXX_STANDARD 11)
17+
18+
# Compiler Flags
19+
set(OPT_FLAG -O2 -g)
20+
21+
# Source Files
22+
file(GLOB_RECURSE SRC_FILES "${SRC_DIR}/*.c" "${SRC_DIR}/*.cpp")
23+
24+
add_executable(${PROJECT_NAME} ${SRC_FILES})
25+
26+
# Compile Options
27+
28+
target_compile_options(${PROJECT_NAME} PRIVATE
29+
${OPT_FLAG}
30+
-Wall -Wextra -Wpedantic -Wshadow -Wfloat-conversion -Wconversion -Wformat=2
31+
)
32+
33+
target_link_libraries(${PROJECT_NAME} PRIVATE "quadmath")

test/README.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Updated: 2024/09/15
2+
3+
Build Instructions for Windows 10 MSYS2 MinGW64:
4+
From this directory, run `mkdir build`, `cd build`, `cmake -G Ninja ..`. Then, run `ninja` to compile, and `bin/quadmath-cpp-test` to run the program.
5+
6+
Build Instructions for Linux:
7+
From this directory, run `mkdir build`, `cd build`, `cmake -G Ninja ..`. Then, run `ninja` to compile, and `bin/quadmath-cpp-test` to run the program.

test/src/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
** Author: zerico2005 (2024)
3+
** Project: quadmath_cpp
4+
** License: MIT License
5+
** A copy of the MIT License should be included with
6+
** this project. If not, see https://opensource.org/license/MIT
7+
*/
8+
9+
#include "../../quadmath_cpp.h"
10+
11+
#include <cstdio>
12+
13+
int main(void) {
14+
__float128 val_0 = 1.234q;
15+
__float128 val_1 = 5.678q;
16+
__float128 result = pow(val_0, val_1);
17+
18+
char buf_0[256];
19+
char buf_1[256];
20+
char buf_result[256];
21+
22+
quadmath_snprintf(buf_0, sizeof(buf_0), "%.3Qf", val_0);
23+
quadmath_snprintf(buf_1, sizeof(buf_1), "%.3Qf", val_1);
24+
quadmath_snprintf(buf_result, sizeof(buf_result), "%.*Qf", FLT128_DIG, result);
25+
26+
printf("pow(%s, %s) = %s\n", buf_0, buf_1, buf_result);
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)