Skip to content

Commit f36af5a

Browse files
committed
Remove include dependency from integer and floats on Array
1 parent 9a7e8ef commit f36af5a

15 files changed

+108
-66
lines changed

num/__private/float_macros.h

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <concepts>
2020

21-
#include "containers/array.h"
2221
#include "marker/unsafe.h"
2322
#include "num/__private/float_ordering.h"
2423
#include "num/__private/intrinsics.h"
@@ -27,6 +26,12 @@
2726
#include "num/signed_integer.h"
2827
#include "num/unsigned_integer.h"
2928

29+
namespace sus::containers {
30+
template <class T, size_t N>
31+
requires(N <= PTRDIFF_MAX)
32+
class Array;
33+
}
34+
3035
#define _sus__float_storage(PrimitiveT) \
3136
/** The inner primitive value, in case it needs to be unwrapped from the \
3237
* type. Avoid using this member except to convert when a consumer requires \
@@ -696,19 +701,21 @@
696701
return r; \
697702
}
698703

699-
#define _sus__float_endian(T, PrimitiveT, UnsignedIntT) \
704+
#define _sus__float_endian(T, Bytes, UnsignedIntT) \
700705
/** Return the memory representation of this floating point number as a byte \
701706
* array in big-endian (network) byte order. \
702707
*/ \
703-
constexpr ::sus::Array<u8, sizeof(PrimitiveT)> to_be_bytes() \
704-
const& noexcept { \
708+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
709+
::sus::containers::Array<u8, Bytes>> \
710+
constexpr Array to_be_bytes() const& noexcept { \
705711
return to_bits().to_be_bytes(); \
706712
} \
707713
/** Return the memory representation of this floating point number as a byte \
708714
* array in little-endian byte order. \
709715
*/ \
710-
constexpr ::sus::Array<u8, sizeof(PrimitiveT)> to_le_bytes() \
711-
const& noexcept { \
716+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
717+
::sus::containers::Array<u8, Bytes>> \
718+
constexpr Array to_le_bytes() const& noexcept { \
712719
return to_bits().to_le_bytes(); \
713720
} \
714721
/** Return the memory representation of this floating point number as a byte \
@@ -717,26 +724,29 @@
717724
* As the target platform's native endianness is used, portable code should \
718725
* use `to_be_bytes()` or `to_le_bytes()`, as appropriate, instead. \
719726
*/ \
720-
constexpr ::sus::Array<u8, sizeof(PrimitiveT)> to_ne_bytes() \
721-
const& noexcept { \
727+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
728+
::sus::containers::Array<u8, Bytes>> \
729+
constexpr Array to_ne_bytes() const& noexcept { \
722730
return to_bits().to_ne_bytes(); \
723731
} \
724732
/** Create a floating point value from its representation as a byte array in \
725733
* big endian. \
726734
* \
727735
* See `##T##::from_bits()` for why this function is not constexpr. \
728736
*/ \
729-
static T from_be_bytes( \
730-
const ::sus::Array<u8, sizeof(PrimitiveT)>& bytes) noexcept { \
737+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
738+
::sus::containers::Array<u8, Bytes>> \
739+
static T from_be_bytes(const Array& bytes) noexcept { \
731740
return T::from_bits(UnsignedIntT::from_be_bytes(bytes)); \
732741
} \
733742
/** Create a floating point value from its representation as a byte array in \
734743
* big endian. \
735744
* \
736745
* See `##T##::from_bits()` for why this function is not constexpr. \
737746
*/ \
738-
static T from_le_bytes( \
739-
const ::sus::Array<u8, sizeof(PrimitiveT)>& bytes) noexcept { \
747+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
748+
::sus::containers::Array<u8, Bytes>> \
749+
static T from_le_bytes(const Array& bytes) noexcept { \
740750
return T::from_bits(UnsignedIntT::from_le_bytes(bytes)); \
741751
} \
742752
/** Create a floating point value from its representation as a byte array in \
@@ -748,28 +758,29 @@
748758
* \
749759
* See `##T##::from_bits()` for why this function is not constexpr. \
750760
*/ \
751-
static T from_ne_bytes( \
752-
const ::sus::Array<u8, sizeof(PrimitiveT)>& bytes) noexcept { \
761+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
762+
::sus::containers::Array<u8, Bytes>> \
763+
static T from_ne_bytes(const Array& bytes) noexcept { \
753764
return T::from_bits(UnsignedIntT::from_ne_bytes(bytes)); \
754765
}
755766

756767
// to_be_bytes, to_le_bytes, to_ne_bytes
757768

758-
#define _sus__float(T, PrimitiveT, UnsignedIntT) \
759-
_sus__float_storage(PrimitiveT); \
760-
_sus__float_constants(T, PrimitiveT); \
761-
_sus__float_construct(T, PrimitiveT); \
762-
_sus__float_comparison(T); \
763-
_sus__float_unary_ops(T); \
764-
_sus__float_binary_ops(T); \
765-
_sus__float_mutable_ops(T); \
766-
_sus__float_abs(T, PrimitiveT); \
767-
_sus__float_math(T, PrimitiveT); \
768-
_sus__float_fract_trunc(T); \
769-
_sus__float_convert_to(T, PrimitiveT); \
770-
_sus__float_bytes(T, UnsignedIntT); \
771-
_sus__float_category(T); \
772-
_sus__float_clamp(T); \
773-
_sus__float_euclid(T, PrimitiveT); \
774-
_sus__float_endian(T, PrimitiveT, UnsignedIntT); \
769+
#define _sus__float(T, PrimitiveT, UnsignedIntT) \
770+
_sus__float_storage(PrimitiveT); \
771+
_sus__float_constants(T, PrimitiveT); \
772+
_sus__float_construct(T, PrimitiveT); \
773+
_sus__float_comparison(T); \
774+
_sus__float_unary_ops(T); \
775+
_sus__float_binary_ops(T); \
776+
_sus__float_mutable_ops(T); \
777+
_sus__float_abs(T, PrimitiveT); \
778+
_sus__float_math(T, PrimitiveT); \
779+
_sus__float_fract_trunc(T); \
780+
_sus__float_convert_to(T, PrimitiveT); \
781+
_sus__float_bytes(T, UnsignedIntT); \
782+
_sus__float_category(T); \
783+
_sus__float_clamp(T); \
784+
_sus__float_euclid(T, PrimitiveT); \
785+
_sus__float_endian(T, sizeof(PrimitiveT), UnsignedIntT); \
775786
static_assert(true)

num/__private/signed_integer_macros.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "assertions/check.h"
2323
#include "assertions/endian.h"
24-
#include "containers/array.h"
2524
#include "marker/unsafe.h"
2625
#include "num/__private/int_log10.h"
2726
#include "num/__private/intrinsics.h"
@@ -31,6 +30,12 @@
3130
#include "option/option.h"
3231
#include "tuple/tuple.h"
3332

33+
namespace sus::containers {
34+
template <class T, size_t N>
35+
requires(N <= PTRDIFF_MAX)
36+
class Array;
37+
}
38+
3439
#define _sus__signed_impl(T, PrimitiveT, UnsignedT) \
3540
_sus__signed_storage(PrimitiveT); \
3641
_sus__signed_constants(T, PrimitiveT); \
@@ -1225,9 +1230,9 @@
12251230
* whether an overflow happened. \
12261231
*/ \
12271232
constexpr Tuple<T, bool> overflowing_pow(const u32& exp) const& noexcept { \
1228-
const auto out = \
1233+
const auto out = \
12291234
__private::pow_with_overflow(primitive_value, exp.primitive_value); \
1230-
return Tuple<T, bool>::with(out.value, out.overflow); \
1235+
return Tuple<T, bool>::with(out.value, out.overflow); \
12311236
} \
12321237
\
12331238
/** Wrapping (modular) exponentiation. Computes self.pow(exp), wrapping \
@@ -1373,14 +1378,18 @@
13731378
/** Return the memory representation of this integer as a byte array in \
13741379
* big-endian (network) byte order. \
13751380
*/ \
1376-
constexpr ::sus::Array<u8, Bytes> to_be_bytes() const& noexcept { \
1381+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1382+
::sus::containers::Array<u8, Bytes>> \
1383+
constexpr Array to_be_bytes() const& noexcept { \
13771384
return to_be().to_ne_bytes(); \
13781385
} \
13791386
\
13801387
/** Return the memory representation of this integer as a byte array in \
13811388
* little-endian byte order. \
13821389
*/ \
1383-
constexpr ::sus::Array<u8, Bytes> to_le_bytes() const& noexcept { \
1390+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1391+
::sus::containers::Array<u8, Bytes>> \
1392+
constexpr Array to_le_bytes() const& noexcept { \
13841393
return to_le().to_ne_bytes(); \
13851394
} \
13861395
\
@@ -1390,8 +1399,10 @@
13901399
* As the target platform's native endianness is used, portable code should \
13911400
* use `to_be_bytes()` or `to_le_bytes()`, as appropriate, instead. \
13921401
*/ \
1393-
constexpr ::sus::Array<u8, Bytes> to_ne_bytes() const& noexcept { \
1394-
auto bytes = ::sus::Array<u8, sizeof(T)>::with_uninitialized(unsafe_fn); \
1402+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1403+
::sus::containers::Array<u8, Bytes>> \
1404+
constexpr Array to_ne_bytes() const& noexcept { \
1405+
auto bytes = Array::with_uninitialized(unsafe_fn); \
13951406
if (std::is_constant_evaluated()) { \
13961407
auto uval = __private::into_unsigned(primitive_value); \
13971408
for (auto i = size_t{0}; i < sizeof(T); ++i) { \
@@ -1411,16 +1422,18 @@
14111422
/** Create an integer value from its representation as a byte array in big \
14121423
* endian. \
14131424
*/ \
1414-
static constexpr T from_be_bytes( \
1415-
const ::sus::Array<u8, Bytes>& bytes) noexcept { \
1425+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1426+
::sus::containers::Array<u8, Bytes>> \
1427+
static constexpr T from_be_bytes(const Array& bytes) noexcept { \
14161428
return from_be(from_ne_bytes(bytes)); \
14171429
} \
14181430
\
14191431
/** Create an integer value from its representation as a byte array in \
14201432
* little endian. \
14211433
*/ \
1422-
static constexpr T from_le_bytes( \
1423-
const ::sus::Array<u8, Bytes>& bytes) noexcept { \
1434+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1435+
::sus::containers::Array<u8, Bytes>> \
1436+
static constexpr T from_le_bytes(const Array& bytes) noexcept { \
14241437
return from_le(from_ne_bytes(bytes)); \
14251438
} \
14261439
\
@@ -1431,8 +1444,9 @@
14311444
* wants to use `from_be_bytes()` or `from_le_bytes()`, as appropriate \
14321445
* instead. \
14331446
*/ \
1434-
static constexpr T from_ne_bytes( \
1435-
const ::sus::Array<u8, Bytes>& bytes) noexcept { \
1447+
template <std::same_as<::sus::containers::Array<u8, Bytes>> Array = \
1448+
::sus::containers::Array<u8, Bytes>> \
1449+
static constexpr T from_ne_bytes(const Array& bytes) noexcept { \
14361450
using U = decltype(__private::into_unsigned(primitive_value)); \
14371451
U val; \
14381452
if (std::is_constant_evaluated()) { \

0 commit comments

Comments
 (0)