23
23
#include < cstdint>
24
24
#include < type_traits>
25
25
26
+ #if FLANG_LITTLE_ENDIAN
27
+ #define SWAP (a, i, b, j ) \
28
+ a{i}, b { j }
29
+ #elif FLANG_BIG_ENDIAN
30
+ #define SWAP (a, i, b, j ) \
31
+ b{j}, a { i }
32
+ #else
33
+ #error host endianness is not known
34
+ #endif
35
+
26
36
namespace Fortran ::common {
27
37
28
38
template <bool IS_SIGNED = false > class Int128 {
@@ -34,14 +44,14 @@ template <bool IS_SIGNED = false> class Int128 {
34
44
constexpr Int128 (unsigned long n) : low_{n} {}
35
45
constexpr Int128 (unsigned long long n) : low_{n} {}
36
46
constexpr Int128 (int n)
37
- : low_{ static_cast <std::uint64_t >(n)} , high_{- static_cast <std:: uint64_t >(
38
- n < 0 )} {}
47
+ : SWAP( low_, static_cast <std::uint64_t >(n), high_,
48
+ -static_cast<std::uint64_t>( n < 0 )) {}
39
49
constexpr Int128 (long n)
40
- : low_{ static_cast <std::uint64_t >(n)} , high_{- static_cast <std:: uint64_t >(
41
- n < 0 )} {}
50
+ : SWAP( low_, static_cast <std::uint64_t >(n), high_,
51
+ -static_cast<std::uint64_t>( n < 0 )) {}
42
52
constexpr Int128 (long long n)
43
- : low_{ static_cast <std::uint64_t >(n)} , high_{- static_cast <std:: uint64_t >(
44
- n < 0 )} {}
53
+ : SWAP( low_, static_cast <std::uint64_t >(n), high_,
54
+ -static_cast<std::uint64_t>( n < 0 )) {}
45
55
constexpr Int128 (const Int128 &) = default;
46
56
constexpr Int128 (Int128 &&) = default;
47
57
constexpr Int128 &operator =(const Int128 &) = default ;
@@ -246,7 +256,8 @@ template <bool IS_SIGNED = false> class Int128 {
246
256
}
247
257
248
258
private:
249
- constexpr Int128 (std::uint64_t hi, std::uint64_t lo) : low_{lo}, high_{hi} {}
259
+ constexpr Int128 (std::uint64_t hi, std::uint64_t lo)
260
+ : SWAP(low_, lo, high_, hi) {}
250
261
constexpr int LeadingZeroes () const {
251
262
if (high_ == 0 ) {
252
263
return 64 + LeadingZeroBitCount (low_);
@@ -255,7 +266,7 @@ template <bool IS_SIGNED = false> class Int128 {
255
266
}
256
267
}
257
268
static constexpr std::uint64_t topBit{std::uint64_t {1 } << 63 };
258
- std::uint64_t low_{ 0 } , high_{ 0 } ;
269
+ std::uint64_t SWAP ( low_, 0 , high_, 0 ) ;
259
270
};
260
271
261
272
using UnsignedInt128 = Int128<false >;
@@ -288,4 +299,7 @@ template <int BITS>
288
299
using HostSignedIntType = typename HostSignedIntTypeHelper<BITS>::type;
289
300
290
301
} // namespace Fortran::common
302
+
303
+ #undef SWAP
304
+
291
305
#endif
0 commit comments