Skip to content

Commit f9cee58

Browse files
committed
bid128 conditional handling on linux only
1 parent db97fba commit f9cee58

3 files changed

Lines changed: 98 additions & 48 deletions

File tree

include/decimal.hpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright © 2017–2025 VargaLABS, Toronto, ON, Canada 🇨🇦 */
33

44
#pragma once
5-
#include <stdfloat>
65
#include <format>
76
#include <iostream>
87
#include <algorithm>
@@ -16,17 +15,24 @@
1615
#include <bid_functions.h>
1716
#include <dfp754.h>
1817

18+
#if defined(__linux__)
19+
#define sigma_has_bid128 1
20+
#else
21+
#define sigma_has_bid128 0
22+
#endif
23+
1924
#ifndef BID32_MAX_PRINTED_DIGITS
2025
#define BID32_MAX_PRINTED_DIGITS 20
2126
#endif
2227

2328
#ifndef BID64_MAX_PRINTED_DIGITS
2429
#define BID64_MAX_PRINTED_DIGITS 40
2530
#endif
26-
27-
#ifndef BID128_MAX_PRINTED_DIGITS
28-
#define BID128_MAX_PRINTED_DIGITS 80
29-
#endif
31+
#if sigma_has_bid128
32+
#ifndef BID128_MAX_PRINTED_DIGITS
33+
#define BID128_MAX_PRINTED_DIGITS 80
34+
#endif
35+
#endif
3036

3137
namespace math::bid {
3238
enum struct category : unsigned char {
@@ -133,6 +139,7 @@ namespace math::bid {
133139
default: return print(kind != category::positive, mantissa, exponent, buffer.data(), buffer.size());
134140
}
135141
}
142+
#if sigma_has_bid128
136143
inline std::string display_bid128(BID_UINT128 bid) {
137144
char buf[200]{};
138145
unsigned int flags = 0;
@@ -142,12 +149,14 @@ namespace math::bid {
142149
inline BID_UINT128 uint128_to_bid128(BID_UINT128 significand, int exponent) {
143150
BID_UINT128 value;
144151
return value;
145-
}
152+
}
153+
#endif
146154
}
147155

148156
namespace math {
149-
using uint128_t = BID_UINT128;
150-
157+
#if sigma_has_bid128
158+
using uint128_t = BID_UINT128;
159+
#endif
151160
template <class T> struct bid_t {
152161
bid_t(T value) : value(value){}
153162
operator T(){ return value; }
@@ -196,7 +205,7 @@ namespace math {
196205
template<> uint64_t str2bid(std::string_view str){ unsigned int flags; std::string tmp{str}; return __bid64_from_string(const_cast<char*>(tmp.data()), 0, &flags); }
197206
template<> uint64_t str2bid(const std::string& str){ unsigned int flags; return __bid64_from_string(const_cast<char*>(str.data()), 0, &flags); }
198207
template<> std::string bid2str(uint64_t bid){ return bid::display_bid64(bid); };
199-
208+
#if sigma_has_bid128
200209
template<> uint128_t bid2dpd(uint128_t bid){ return __bid_to_dpd128(bid); }
201210
template<> uint128_t dpd2bid(uint128_t dpd){ return __bid_dpd_to_bid128(dpd); }
202211
template<> uint128_t uint2bid(uint128_t significand, int exponent){ return bid::uint128_to_bid128(significand, exponent); }
@@ -208,6 +217,7 @@ namespace math {
208217
template<> uint128_t str2bid(std::string_view str){ unsigned int flags; std::string tmp{str}; return __bid128_from_string(const_cast<char*>(tmp.data()), 0, &flags); }
209218
template<> uint128_t str2bid(const std::string& str){ unsigned int flags; return __bid128_from_string(const_cast<char*>(str.data()), 0, &flags); }
210219
template<> std::string bid2str(uint128_t bid){ return bid::display_bid128(bid); };
220+
#endif
211221
}
212222

213223
// operator defintions
@@ -243,7 +253,9 @@ namespace math {
243253

244254
sigma_decimal_class(32, uint32_t, double);
245255
sigma_decimal_class(64, uint64_t, long double);
246-
sigma_decimal_class(128, uint128_t, long double);
256+
#if sigma_has_bid128
257+
sigma_decimal_class(128, uint128_t, long double);
258+
#endif
247259

248260
// operator combinations
249261
#define sigma_external_operators(_bit_size_) \
@@ -254,7 +266,10 @@ namespace math {
254266
sigma_comparison_operator(uint##_bit_size_##_t, <=, __bid##_bit_size_##_quiet_less_equal); sigma_comparison_operator(uint##_bit_size_##_t, >=, __bid##_bit_size_##_quiet_greater_equal);
255267

256268
// operator instatiations for various bit sizes
257-
sigma_external_operators(32); sigma_external_operators(64); sigma_external_operators(128);
269+
sigma_external_operators(32); sigma_external_operators(64);
270+
#if sigma_has_bid128
271+
sigma_external_operators(128);
272+
#endif
258273
#undef sigma_external_operators
259274
#undef sigma_decimal_class
260275
#undef sigma_comparison_operator
@@ -269,8 +284,10 @@ namespace math {
269284
namespace literals {
270285
sigma_decimal_literal(uint32_t, _dec32);
271286
sigma_decimal_literal(uint64_t, _dec64);
272-
sigma_decimal_literal(uint128_t, _dec128);
273287
sigma_decimal_literal(uint64_t, _dec);
288+
#if sigma_has_bid128
289+
sigma_decimal_literal(uint128_t, _dec128);
290+
#endif
274291
}
275292
#undef sigma_decimal_literal
276293
// begin utilities:
@@ -304,7 +321,9 @@ namespace math {
304321
namespace math {
305322
sigma_stream_op(uint32_t);
306323
sigma_stream_op(uint64_t);
307-
sigma_stream_op(BID_UINT128);
324+
#if sigma_has_bid128
325+
sigma_stream_op(BID_UINT128);
326+
#endif
308327
}
309328
#undef sigma_stream_op
310329

@@ -349,7 +368,10 @@ namespace math {
349368
sigma_unary_op(N, cbrt) sigma_unary_op(N, atanh) sigma_unary_op(N, asinh) sigma_unary_op(N, asin) \
350369
sigma_unary_op(N, acosh) sigma_unary_op(N, acos) sigma_unary_op(N, sqrt)
351370

352-
sigma_op(32); sigma_op(64); sigma_op(128);
371+
sigma_op(32); sigma_op(64);
372+
#if sigma_has_bid128
373+
sigma_op(128);
374+
#endif
353375

354376
#undef sigma__op
355377
#undef sigma_unary_op
@@ -417,6 +439,7 @@ namespace math {
417439
inline static const decimal_t cube_root_2 = static_cast<decimal_t>("1.259921049894873");
418440
inline static const decimal_t cube_root_3 = static_cast<decimal_t>("1.442249570307408");
419441
};
442+
#if sigma_has_bid128
420443
template <> struct constants<BID_UINT128> { // 34 digit precision
421444
using decimal_t = typename math::decimal_t<BID_UINT128>;
422445
inline static const decimal_t nan = static_cast<decimal_t>("nan");
@@ -445,5 +468,6 @@ namespace math {
445468
inline static const decimal_t quarter_pi = static_cast<decimal_t>("0.7853981633974483096156608458198757");
446469
inline static const decimal_t cube_root_2 = static_cast<decimal_t>("1.2599210498948731647672106072782284");
447470
inline static const decimal_t cube_root_3 = static_cast<decimal_t>("1.4422495703074083823216383107801096");
448-
};
471+
};
472+
#endif
449473
}

test/decimal.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,22 @@
77
#include <doctest/all>
88
#include <decimal.hpp>
99

10-
#if __has_include(<stdfloat>)
11-
#include <stdfloat>
12-
#define LIBDECIMAL_HAS_STDFLOAT 1
13-
#else
14-
#define LIBDECIMAL_HAS_STDFLOAT 0
15-
#endif
16-
1710
#define TRACE std::cerr
18-
#ifndef _MSC_VER
11+
#if sigma_has_bid128
1912
using uint128_t = BID_UINT128;
2013
#endif
2114

2215
struct value_t{ std::string a,b,c; };
2316
using decimal_t = typename math::decimal_t<uint64_t>;
2417

2518
TEST_CASE("pi") {
26-
using uint128_t = BID_UINT128;
2719
TRACE << math::sin(math::constants<uint32_t>::pi) << " " << math::constants<uint32_t>::pi << std::endl;
2820
TRACE << math::sin(math::constants<uint64_t>::pi) << " " << math::constants<uint64_t>::pi << std::endl;
29-
TRACE << math::sin(math::constants<uint128_t>::pi) << " " << math::constants<uint128_t>::pi << std::endl;
3021
TRACE << math::exp(math::constants<uint64_t>::zero) << std::endl;
3122
TRACE << math::exp(math::constants<uint32_t>::zero) << std::endl;
23+
#if sigma_has_bid128
24+
TRACE << math::sin(math::constants<uint128_t>::pi) << " " << math::constants<uint128_t>::pi << std::endl;
25+
#endif
3226
}
3327

3428
TEST_CASE("sin-32") {
@@ -108,7 +102,11 @@ TEST_CASE("zeroes") {
108102

109103
using namespace math::literals;
110104
TRACE << "ZERO: " << std::hex
111-
<< (0.0_dec32).value << " " << (0.0_dec64).value << "(0.0_dec128).value " << std::endl;
105+
<< (0.0_dec32).value << " " << (0.0_dec64).value
106+
#if sigma_has_bid128
107+
<< "(0.0_dec128).value "
108+
#endif
109+
<< std::endl;
112110
}
113111

114112
TEST_CASE("from-integer") {

thirdparty/intel-decimal-float/CMakeLists.txt

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,56 @@ file(GLOB bid_core_sources CONFIGURE_DEPENDS
1616
"${src_dir}/*.c"
1717
)
1818

19-
set(bid_f128_sources
20-
"${f128_dir}/dpml_ux_bid.c"
21-
"${f128_dir}/dpml_ux_bessel.c"
22-
"${f128_dir}/dpml_ux_cbrt.c"
23-
"${f128_dir}/dpml_ux_erf.c"
24-
"${f128_dir}/dpml_ux_exp.c"
25-
"${f128_dir}/dpml_ux_int.c"
26-
"${f128_dir}/dpml_ux_inv_hyper.c"
27-
"${f128_dir}/dpml_ux_inv_trig.c"
28-
"${f128_dir}/dpml_ux_lgamma.c"
29-
"${f128_dir}/dpml_ux_log.c"
30-
"${f128_dir}/dpml_ux_mod.c"
31-
"${f128_dir}/dpml_ux_powi.c"
32-
"${f128_dir}/dpml_ux_pow.c"
33-
"${f128_dir}/dpml_ux_sqrt.c"
34-
"${f128_dir}/dpml_ux_trig.c"
35-
"${f128_dir}/dpml_ux_ops.c"
36-
"${f128_dir}/dpml_ux_ops_64.c"
37-
"${f128_dir}/dpml_four_over_pi.c"
38-
"${f128_dir}/dpml_exception.c"
39-
"${f128_dir}/sqrt_tab_t.c"
40-
)
19+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
20+
set(bid_f128_sources
21+
"${f128_dir}/dpml_ux_bid.c"
22+
"${f128_dir}/dpml_ux_bessel.c"
23+
"${f128_dir}/dpml_ux_cbrt.c"
24+
"${f128_dir}/dpml_ux_erf.c"
25+
"${f128_dir}/dpml_ux_exp.c"
26+
"${f128_dir}/dpml_ux_int.c"
27+
"${f128_dir}/dpml_ux_inv_hyper.c"
28+
"${f128_dir}/dpml_ux_inv_trig.c"
29+
"${f128_dir}/dpml_ux_lgamma.c"
30+
"${f128_dir}/dpml_ux_log.c"
31+
"${f128_dir}/dpml_ux_mod.c"
32+
"${f128_dir}/dpml_ux_powi.c"
33+
"${f128_dir}/dpml_ux_pow.c"
34+
"${f128_dir}/dpml_ux_sqrt.c"
35+
"${f128_dir}/dpml_ux_trig.c"
36+
"${f128_dir}/dpml_ux_ops.c"
37+
"${f128_dir}/dpml_ux_ops_64.c"
38+
"${f128_dir}/dpml_four_over_pi.c"
39+
"${f128_dir}/dpml_exception.c"
40+
"${f128_dir}/sqrt_tab_t.c"
41+
)
42+
else()
43+
set(bid_f128_sources)
44+
list(REMOVE_ITEM bid_core_sources
45+
"${src_dir}/bid128_acos.c"
46+
"${src_dir}/bid128_asin.c"
47+
"${src_dir}/bid128_cbrt.c"
48+
"${src_dir}/bid128_cosh.c"
49+
"${src_dir}/bid128_erf.c"
50+
"${src_dir}/bid128_erfc.c"
51+
"${src_dir}/bid128_exp.c"
52+
"${src_dir}/bid128_expm1.c"
53+
"${src_dir}/bid128_lgamma.c"
54+
"${src_dir}/bid128_log.c"
55+
"${src_dir}/bid128_log10.c"
56+
"${src_dir}/bid128_log2.c"
57+
"${src_dir}/bid128_sinh.c"
58+
"${src_dir}/bid128_tgamma.c"
59+
"${src_dir}/bid64_acos.c"
60+
"${src_dir}/bid64_asin.c"
61+
"${src_dir}/bid64_cbrt.c"
62+
"${src_dir}/bid64_exp.c"
63+
"${src_dir}/bid64_exp10.c"
64+
"${src_dir}/bid64_lgamma.c"
65+
"${src_dir}/bid64_log2.c"
66+
"${src_dir}/bid64_tgamma.c"
67+
)
68+
endif()
4169

4270
add_library(libbid_core_obj OBJECT ${bid_core_sources})
4371
target_include_directories(libbid_core_obj PRIVATE

0 commit comments

Comments
 (0)