Skip to content

Commit 4ffcb39

Browse files
vector-of-boolrcsanchez97
authored andcommitted
[MONGOCRYPT-536] A Toggle for Decimal128 Range Support (#569)
* Conditional Decimal128 range support The configure-time option MONGOCRYPT_ENABLE_DECIMAL128 can be used to enable/disable Decimal128 range support. This is enabled by default. - Wrap Decimal128 code in conditions on whether Decimal128 is available. - Only supports IntelDFP for now.
1 parent 64f93f1 commit 4ffcb39

18 files changed

+118
-8
lines changed

CMakeLists.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ include (GNUInstallDirs)
4545

4646
include (LTO)
4747
include (ImportBSON)
48-
include (ImportDFP)
48+
49+
option (MONGOCRYPT_ENABLE_DECIMAL128 "Enable extended support for Decimal128" ON)
50+
mark_as_advanced (MONGOCRYPT_ENABLE_DECIMAL128)
51+
set (maybe_dfp_library)
52+
if (MONGOCRYPT_ENABLE_DECIMAL128)
53+
include (ImportDFP)
54+
set (maybe_dfp_library mongocrypt::intel_dfp)
55+
endif ()
4956

5057
if (USE_SHARED_LIBBSON AND ENABLE_BUILD_FOR_PPA)
5158
message (FATAL_ERROR "PPA build requires static linking to libbson")
@@ -281,7 +288,7 @@ target_link_libraries (
281288
kms_message_static
282289
$<BUILD_INTERFACE:mongo::mlib>
283290
PUBLIC
284-
mongocrypt::intel_dfp
291+
${maybe_dfp_library}
285292
${CMAKE_DL_LIBS}
286293
)
287294

@@ -339,7 +346,7 @@ target_link_libraries (
339346
kms_message_static
340347
$<BUILD_INTERFACE:mongo::mlib>
341348
PUBLIC
342-
mongocrypt::intel_dfp
349+
${maybe_dfp_library}
343350
${CMAKE_THREAD_LIBS_INIT}
344351
${CMAKE_DL_LIBS}
345352
)
@@ -358,7 +365,7 @@ if (ENABLE_BUILD_FOR_PPA)
358365
endif ()
359366
set (PKG_CONFIG_STATIC_LIBS "${PKG_CONFIG_STATIC_LIBS} -pthread")
360367
endif ()
361-
if (MONGOCRYPT_DFP_DIR STREQUAL "USE-SYSTEM")
368+
if (MONGOCRYPT_DFP_DIR STREQUAL "USE-SYSTEM" AND MONGOCRYPT_ENABLE_DECIMAL128)
362369
get_property (SYSTEM_DFP_LOC TARGET intel_dfp PROPERTY IMPORTED_LOCATION)
363370
set (PKG_CONFIG_STATIC_LIBS "${PKG_CONFIG_STATIC_LIBS} ${SYSTEM_DFP_LOC}")
364371
endif ()
@@ -539,7 +546,7 @@ if ("cxx_relaxed_constexpr" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
539546
target_link_libraries ("${exe_name}" PRIVATE
540547
mongocrypt
541548
mongo::mlib
542-
mongocrypt::intel_dfp
549+
${maybe_dfp_library}
543550
Threads::Threads
544551
_mongocrypt::libbson_for_static
545552
)
@@ -634,7 +641,7 @@ if (ENABLE_BUILD_FOR_PPA)
634641
endif ()
635642
set (PKG_CONFIG_LIBS "${PKG_CONFIG_LIBS} -pthread")
636643
endif ()
637-
if (MONGOCRYPT_DFP_DIR STREQUAL "USE-SYSTEM")
644+
if (MONGOCRYPT_DFP_DIR STREQUAL "USE-SYSTEM" AND MONGOCRYPT_ENABLE_DECIMAL128)
638645
get_property (SYSTEM_DFP_LOC TARGET intel_dfp PROPERTY IMPORTED_LOCATION)
639646
set (PKG_CONFIG_LIBS "${PKG_CONFIG_LIBS} ${SYSTEM_DFP_LOC}")
640647
endif ()

cmake/ImportDFP.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,7 @@ elseif (MONGOCRYPT_DFP_DIR STREQUAL "USE-SYSTEM")
6464
# users during find_package()
6565
target_link_libraries (_mongocrypt-intel_dfp INTERFACE $<BUILD_INTERFACE:intel_dfp>)
6666

67+
# Notify in-tree consumers that IntelDFP is available:
68+
target_compile_definitions (_mongocrypt-intel_dfp INTERFACE $<BUILD_INTERFACE:MONGOCRYPT_INTELDFP>)
6769
endif ()
6870

cmake/IntelDFP.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ target_include_directories(intel_dfp_obj PUBLIC ${intel_dfp_SOURCE_DIR}/LIBRARY/
380380
add_library (_mongocrypt_intel_dfp INTERFACE)
381381
add_library (mongocrypt::intel_dfp ALIAS _mongocrypt_intel_dfp)
382382

383+
# Notify in-tree consumers that IntelDFP is available:
384+
target_compile_definitions (_mongocrypt_intel_dfp INTERFACE $<BUILD_INTERFACE:MONGOCRYPT_INTELDFP>)
385+
383386
target_sources (_mongocrypt_intel_dfp
384387
#[[
385388
For targets *within this build* that link with mongocrypt::intel_dfp,

src/mc-dec128.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
#include <mlib/int128.h>
88
#include <mlib/endian.h>
99

10+
// Conditional preprocessor definition set by the usage of an intel_dfp from
11+
// the ImportDFP.cmake script:
12+
#ifndef MONGOCRYPT_INTELDFP
13+
// Notify includers that Decimal128 is not available:
14+
#define MONGOCRYPT_HAVE_DECIMAL128_SUPPORT 0
15+
16+
#else // With IntelDFP:
17+
// Tell includers that Decimal128 is okay:
18+
#define MONGOCRYPT_HAVE_DECIMAL128_SUPPORT 1
19+
1020
// Include the header that declares the DFP functions, which may be macros that
1121
// expand to renamed symbols:
1222
#include <bid_conf.h>
@@ -728,4 +738,6 @@ mc_dec128_to_bson_decimal128 (mc_dec128 v)
728738

729739
MLIB_C_LINKAGE_END
730740

741+
#endif /// defined MONGOCRYPT_INTELDFP
742+
731743
#endif // MC_DEC128_H_INCLUDED

src/mc-dec128.test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <mc-dec128.h>
22

3+
#include <cstdio>
4+
5+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
6+
37
#include <stdlib.h>
48

59
#include <mlib/check.hpp>
@@ -70,3 +74,13 @@ main ()
7074
mc_dec128 nan = MC_DEC128_POSITIVE_NAN;
7175
CHECK (mc_dec128_is_nan (nan));
7276
}
77+
78+
#else
79+
80+
int
81+
main ()
82+
{
83+
std::puts ("@@ctest-skip@@\n Decimal128 support is not enabled\n");
84+
}
85+
86+
#endif

src/mc-optional-private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef struct {
103103
.set = true, .value = val \
104104
}
105105

106+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
106107
typedef struct {
107108
bool set;
108109
mc_dec128 value;
@@ -113,7 +114,7 @@ typedef struct {
113114
{ \
114115
.set = true, .value = __VA_ARGS__ \
115116
}
116-
117+
#endif // MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
117118

118119
#define OPT_NULLOPT \
119120
{ \

src/mc-range-edge-generation-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ typedef struct {
7777
mc_edges_t *
7878
mc_getEdgesDouble (mc_getEdgesDouble_args_t args, mongocrypt_status_t *status);
7979

80+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
8081
typedef struct {
8182
mc_dec128 value;
8283
size_t sparsity;
@@ -87,6 +88,7 @@ typedef struct {
8788
mc_edges_t *
8889
mc_getEdgesDecimal128 (mc_getEdgesDecimal128_args_t args,
8990
mongocrypt_status_t *status);
91+
#endif // MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
9092

9193
BSON_STATIC_ASSERT2 (ull_is_u64,
9294
sizeof (uint64_t) == sizeof (unsigned long long));

src/mc-range-edge-generation.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ mc_getEdgesDouble (mc_getEdgesDouble_args_t args, mongocrypt_status_t *status)
213213
return ret;
214214
}
215215

216+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
216217
mc_edges_t *
217218
mc_getEdgesDecimal128 (mc_getEdgesDecimal128_args_t args,
218219
mongocrypt_status_t *status)
@@ -238,3 +239,4 @@ mc_getEdgesDecimal128 (mc_getEdgesDecimal128_args_t args,
238239
mc_edges_t *ret = mc_edges_new (leaf, args.sparsity, status);
239240
return ret;
240241
}
242+
#endif // MONGOCRYPT_HAVE_DECIMAL128_SUPPORT

src/mc-range-encoding-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ mc_getTypeInfoDouble (mc_getTypeInfoDouble_args_t args,
9696
mongocrypt_status_t *status)
9797
MONGOCRYPT_WARN_UNUSED_RESULT;
9898

99+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
99100
/**
100101
* @brief OST-encoding of a Decimal128
101102
*/
@@ -122,5 +123,6 @@ mc_getTypeInfoDecimal128 (mc_getTypeInfoDecimal128_args_t args,
122123
mc_OSTType_Decimal128 *out,
123124
mongocrypt_status_t *status)
124125
MONGOCRYPT_WARN_UNUSED_RESULT;
126+
#endif // MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
125127

126128
#endif /* MC_RANGE_ENCODING_PRIVATE_H */

src/mc-range-encoding.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ mc_getTypeInfoDouble (mc_getTypeInfoDouble_args_t args,
314314
return true;
315315
}
316316

317+
#if MONGOCRYPT_HAVE_DECIMAL128_SUPPORT
317318
/**
318319
* @brief There is no shipped algorithm for creating a full 128-bit integer from
319320
* a Decimal128, but it's easy enough to write one of our own.
@@ -615,3 +616,5 @@ mc_getTypeInfoDecimal128 (mc_getTypeInfoDecimal128_args_t args,
615616

616617
return true;
617618
}
619+
620+
#endif // defined MONGOCRYPT_HAVE_DECIMAL128_SUPPORT

0 commit comments

Comments
 (0)