Skip to content

Commit 8bd5a9f

Browse files
authored
Merge pull request #295 from bluescarni/pr/real_iter
Real additions
2 parents d04b453 + 7c89489 commit 8bd5a9f

22 files changed

Lines changed: 351 additions & 61 deletions

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ workflows:
7777
version: 2.1
7878
all_builds:
7979
jobs:
80-
- focal_clang9_msan_00
81-
- focal_clang9_msan_01
80+
# - focal_clang9_msan_00
81+
# - focal_clang9_msan_01
8282
- bionic_gcc7_conda_coverage
8383
- bionic_gcc7_conda_release
8484
- focal_gcc9_conda_coverage

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Checks: 'clang-diagnostic-*,clang-analyzer-*,*,-hicpp-uppercase-literal-suffix,-readability-uppercase-literal-suffix,-modernize-use-trailing-return-type,-readability-named-parameter,-hicpp-named-parameter,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-header-guard,-cppcoreguidelines-macro-usage,-google-runtime-references,-readability-isolate-declaration,-fuchsia-default-arguments-calls,-fuchsia-overloaded-operator,-fuchsia-default-arguments-declarations,-readability-else-after-return,-google-runtime-int,-hicpp-signed-bitwise,-cert-dcl21-cpp,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-modernize-use-transparent-functors,-cert-dcl16-c,-cppcoreguidelines-pro-type-union-access,-bugprone-branch-clone,-fuchsia-statically-constructed-objects,-cppcoreguidelines-pro-bounds-constant-array-index,-readability-static-accessed-through-instance,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-llvmlibc-restrict-system-libc-headers,-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,-llvm-else-after-return,-altera-struct-pack-align,-readability-function-cognitive-complexity'
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,*,-hicpp-uppercase-literal-suffix,-readability-uppercase-literal-suffix,-modernize-use-trailing-return-type,-readability-named-parameter,-hicpp-named-parameter,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-llvm-header-guard,-cppcoreguidelines-macro-usage,-google-runtime-references,-readability-isolate-declaration,-fuchsia-default-arguments-calls,-fuchsia-overloaded-operator,-fuchsia-default-arguments-declarations,-readability-else-after-return,-google-runtime-int,-hicpp-signed-bitwise,-cert-dcl21-cpp,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-modernize-use-transparent-functors,-cert-dcl16-c,-cppcoreguidelines-pro-type-union-access,-bugprone-branch-clone,-fuchsia-statically-constructed-objects,-cppcoreguidelines-pro-bounds-constant-array-index,-readability-static-accessed-through-instance,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-llvmlibc-restrict-system-libc-headers,-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,-llvm-else-after-return,-altera-struct-pack-align,-readability-function-cognitive-complexity,-readability-identifier-length,-altera-unroll-loops,-altera-id-dependent-backward-branch'
33
WarningsAsErrors: '*'
44
AnalyzeTemporaryDtors: false
55
FormatStyle: none

cmake/yacma/YACMACompilerLinkerSettings.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ if(NOT _YACMACompilerLinkerSettingsRun)
112112
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wsizeof-array-div)
113113
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wxor-used-as-pow)
114114
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wfinal-dtor-non-final-class)
115+
# New warnings in clang 11.
116+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wpointer-to-int-cast)
117+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wuninitialized-const-reference)
118+
# New warnings in clang 13.
119+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wunused-but-set-parameter)
120+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wunused-but-set-variable)
121+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wnull-pointer-subtraction)
115122
# NOTE: this is a new flag in Clang 13 which seems to give
116123
# incorrect warnings for UDLs.
117124
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wno-reserved-identifier)
@@ -171,6 +178,12 @@ if(NOT _YACMACompilerLinkerSettingsRun)
171178
message(STATUS "Activating the '-Wno-maybe-uninitialized' workaround for GCC.")
172179
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wno-maybe-uninitialized)
173180
endif()
181+
# From GCC 10.
182+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wmismatched-tags)
183+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wredundant-tags)
184+
# From GCC 12.
185+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Warray-compare)
186+
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wmissing-requires)
174187
endif()
175188

176189
# MSVC setup.

doc/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ Changelog
44
0.27 (unreleased)
55
-----------------
66

7+
New
8+
~~~
9+
10+
- Add several ``std`` math overloads for :cpp:class:`~mppp::real`
11+
(`#295 <https://github.com/bluescarni/mppp/pull/295>`__).
12+
- Add hashing capabilities to :cpp:class:`~mppp::real`
13+
(`#295 <https://github.com/bluescarni/mppp/pull/295>`__).
14+
- Add a ``std::hash`` specialisation for :cpp:class:`~mppp::real128`
15+
(`#295 <https://github.com/bluescarni/mppp/pull/295>`__).
16+
717
Changes
818
~~~~~~~
919

doc/conf.py.in

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ release = '@mp++_VERSION@'
7272
#
7373
# This is also used if you do content translation via gettext catalogs.
7474
# Usually you set "language" from the command line for these cases.
75-
language = None
75+
language = 'en'
7676

7777
# There are two options for replacing |today|: either, you set today to some
7878
# non-false value, then it is used:
@@ -136,7 +136,15 @@ html_theme_options = {
136136
},
137137
}
138138

139-
jupyter_execute_notebooks = "force"
139+
nb_execution_mode = "off"
140+
141+
myst_enable_extensions = [
142+
"amsmath",
143+
"colon_fence",
144+
"deflist",
145+
"dollarmath",
146+
"html_image",
147+
]
140148

141149
# Theme options are theme-specific and customize the look and feel of a theme
142150
# further. For a list of options available for each theme, see the

doc/real.rst

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,24 +1555,34 @@ Arithmetic
15551555
:return: :math:`-x`.
15561556

15571557
.. cpp:function:: template <mppp::cvr_real T> mppp::real &mppp::abs(mppp::real &rop, T &&x)
1558+
.. cpp:function:: template <mppp::cvr_real T> mppp::real &mppp::fabs(mppp::real &rop, T &&x)
15581559

15591560
Binary :cpp:class:`~mppp::real` absolute value.
15601561

1561-
This function will set *rop* to :math:`\left| x \right|`. The precision of the result will be
1562+
These functions will set *rop* to :math:`\left| x \right|`. The precision of the result will be
15621563
equal to the precision of *x*.
15631564

1565+
.. versionadded:: 0.27
1566+
1567+
The ``fabs()`` overload.
1568+
15641569
:param rop: the return value.
15651570
:param x: the operand.
15661571

15671572
:return: a reference to *rop*.
15681573

15691574
.. cpp:function:: template <mppp::cvr_real T> mppp::real mppp::abs(T &&x)
1575+
.. cpp:function:: template <mppp::cvr_real T> mppp::real mppp::fabs(T &&x)
15701576

15711577
Unary :cpp:class:`~mppp::real` absolute value.
15721578

1573-
This function will return :math:`\left| x \right|`. The precision of the result will be
1579+
These functions will return :math:`\left| x \right|`. The precision of the result will be
15741580
equal to the precision of *x*.
15751581

1582+
.. versionadded:: 0.27
1583+
1584+
The ``fabs()`` overload.
1585+
15761586
:param x: the operand.
15771587

15781588
:return: :math:`\left| x \right|`.
@@ -1685,8 +1695,11 @@ Comparison
16851695
~~~~~~~~~~
16861696

16871697
.. cpp:function:: bool mppp::nan_p(const mppp::real &r)
1698+
.. cpp:function:: bool mppp::isnan(const mppp::real &r)
16881699
.. cpp:function:: bool mppp::inf_p(const mppp::real &r)
1700+
.. cpp:function:: bool mppp::isinf(const mppp::real &r)
16891701
.. cpp:function:: bool mppp::number_p(const mppp::real &r)
1702+
.. cpp:function:: bool mppp::isfinite(const mppp::real &r)
16901703
.. cpp:function:: bool mppp::zero_p(const mppp::real &r)
16911704
.. cpp:function:: bool mppp::regular_p(const mppp::real &r)
16921705
.. cpp:function:: bool mppp::integer_p(const mppp::real &r)
@@ -1696,16 +1709,20 @@ Comparison
16961709

16971710
These functions will return ``true`` if *r* is, respectively:
16981711

1699-
* NaN,
1700-
* an infinity,
1701-
* a finite number,
1702-
* zero,
1703-
* a regular number (i.e., not NaN, infinity or zero),
1704-
* an integral value,
1705-
* one,
1712+
* NaN (``nan_p()`` and ``isnan()``),
1713+
* an infinity (``inf_p()`` and ``isinf()``),
1714+
* a finite number (``number_p()`` and ``isfinite()``),
1715+
* zero (``zero_p()``),
1716+
* a regular number (i.e., not NaN, infinity or zero) (``regular_p()``),
1717+
* an integral value (``integer_p()``),
1718+
* one (``is_one()``),
17061719

17071720
``false`` otherwise.
17081721

1722+
.. versionadded:: 0.27
1723+
1724+
The ``isnan()``, ``isinf()`` and ``isfinite()`` overloads.
1725+
17091726
:param r: the input argument.
17101727

17111728
:return: the result of the detection.
@@ -3184,6 +3201,21 @@ Serialisation
31843201

31853202
:exception unspecified: any exception thrown by the invoked :cpp:func:`mppp::real::binary_load()` overload.
31863203

3204+
Other
3205+
~~~~~
3206+
3207+
.. cpp:function:: std::size_t mppp::hash(const mppp::real &x)
3208+
3209+
.. versionadded:: 0.27
3210+
3211+
Hash function for :cpp:class:`~mppp::real`.
3212+
3213+
This function guarantees that ``x == y`` implies ``hash(x) == hash(y)``.
3214+
3215+
:param x: the argument.
3216+
3217+
:return: a hash value for *x*.
3218+
31873219
.. _real_operators:
31883220

31893221
Mathematical operators
@@ -3348,6 +3380,30 @@ Constants
33483380

33493381
:return: a reference to *rop*.
33503382

3383+
Standard library specialisations
3384+
--------------------------------
3385+
3386+
.. cpp:class:: template <> std::hash<mppp::real>
3387+
3388+
.. versionadded:: 0.27
3389+
3390+
Specialisation of ``std::hash`` for :cpp:class:`mppp::real`.
3391+
3392+
The hash is computed via :cpp:func:`std::size_t mppp::hash(const mppp::real &)`.
3393+
3394+
.. cpp:type:: public argument_type = mppp::real
3395+
.. cpp:type:: public result_type = std::size_t
3396+
3397+
.. note::
3398+
3399+
The :cpp:type:`argument_type` and :cpp:type:`result_type` type aliases are defined only until C++14.
3400+
3401+
.. cpp:function:: public std::size_t operator()(const mppp::real &x) const
3402+
3403+
:param x: the input :cpp:class:`mppp::real`.
3404+
3405+
:return: a hash value for *x*.
3406+
33513407
.. _real_literals:
33523408

33533409
User-defined literals

doc/real128.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,7 @@ Other
13791379

13801380
Hash function for :cpp:class:`~mppp::real128`.
13811381

1382-
All NaN values produce the same hash value. For non-NaN arguments, this function
1383-
guarantees that ``x == y`` implies ``hash(x) == hash(y)``.
1382+
This function guarantees that ``x == y`` implies ``hash(x) == hash(y)``.
13841383

13851384
:param x: the argument.
13861385

@@ -1647,6 +1646,27 @@ Standard library specialisations
16471646

16481647
:return: the output of :cpp:func:`mppp::real128_denorm_min()`.
16491648

1649+
.. cpp:class:: template <> std::hash<mppp::real128>
1650+
1651+
.. versionadded:: 0.27
1652+
1653+
Specialisation of ``std::hash`` for :cpp:class:`mppp::real128`.
1654+
1655+
The hash is computed via :cpp:func:`std::size_t mppp::hash(const mppp::real128 &)`.
1656+
1657+
.. cpp:type:: public argument_type = mppp::real128
1658+
.. cpp:type:: public result_type = std::size_t
1659+
1660+
.. note::
1661+
1662+
The :cpp:type:`argument_type` and :cpp:type:`result_type` type aliases are defined only until C++14.
1663+
1664+
.. cpp:function:: public std::size_t operator()(const mppp::real128 &x) const
1665+
1666+
:param x: the input :cpp:class:`mppp::real128`.
1667+
1668+
:return: a hash value for *x*.
1669+
16501670
.. _real128_literals:
16511671

16521672
User-defined literals

include/mp++/detail/utils.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define MPPP_DETAIL_UTILS_HPP
1111

1212
#include <cassert>
13+
#include <cstddef>
14+
#include <functional>
1315
#include <limits>
1416
#include <stdexcept>
1517
#include <string>
@@ -298,6 +300,14 @@ inline make_signed_t<T> make_signed(const T &n, int s)
298300
return static_cast<make_signed_t<T>>((n ^ s_ex) - s_ex);
299301
}
300302

303+
// The hash combiner. This is lifted directly from Boost. See also:
304+
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf
305+
template <typename T>
306+
inline void hash_combine(std::size_t &seed, const T &val)
307+
{
308+
seed ^= std::hash<T>{}(val) + static_cast<std::size_t>(0x9e3779b9ull) + (seed << 6) + (seed >> 2);
309+
}
310+
301311
#if defined(_MSC_VER)
302312

303313
#pragma warning(pop)

include/mp++/integer.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7037,13 +7037,12 @@ inline std::size_t hash(const integer<SSize> &n)
70377037
= size >= 0 ? static_cast<std::size_t>(size) : static_cast<std::size_t>(detail::nint_abs(size));
70387038
const ::mp_limb_t *ptr
70397039
= n._get_union().is_static() ? n._get_union().g_st().m_limbs.data() : n._get_union().g_dy()._mp_d;
7040-
// Init the retval as the signed size.
7041-
auto retval = static_cast<std::size_t>(size);
7040+
// Init the retval as the hash of the signed size.
7041+
auto retval = std::hash<detail::mpz_size_t>{}(size);
70427042
// Combine the limbs.
70437043
for (std::size_t i = 0; i < asize; ++i) {
7044-
// The hash combiner. This is lifted directly from Boost. See also:
7045-
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf
7046-
retval ^= (ptr[i] & GMP_NUMB_MASK) + std::size_t(0x9e3779b9ul) + (retval << 6) + (retval >> 2);
7044+
// Combine the hashes of the limbs.
7045+
detail::hash_combine(retval, ptr[i] & GMP_NUMB_MASK);
70477046
}
70487047
return retval;
70497048
}
@@ -8200,7 +8199,7 @@ namespace std
82008199
// Specialisation of std::hash for mppp::integer.
82018200
template <size_t SSize>
82028201
struct hash<mppp::integer<SSize>> {
8203-
// NOTE: these typedefs have been deprecated in C++17.
8202+
// NOTE: these typedefs have been deprecated in C++17.
82048203
#if MPPP_CPLUSPLUS < 201703L
82058204
// The argument type.
82068205
using argument_type = mppp::integer<SSize>;

0 commit comments

Comments
 (0)