Skip to content

Commit

Permalink
move together int128 headers and fix hugeint double casting (#2316)
Browse files Browse the repository at this point in the history
fix #2319
  • Loading branch information
Ashleyhx committed Nov 1, 2023
1 parent 94a611c commit 7b408f1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 73 deletions.
25 changes: 12 additions & 13 deletions src/common/types/int128_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
#include <limits>

#include "common/exception/overflow.h"
#include "common/int128_t.h"
#include "common/types/ku_string.h"
#include "function/cast/functions/numeric_limits.h"

namespace kuzu {
namespace common {
namespace kuzu::common {

const int128_t Int128_t::powerOf10[]{
int128_t(1),
Expand Down Expand Up @@ -350,7 +349,7 @@ template<class DST, bool SIGNED = true>
bool TryCastInt128Template(int128_t input, DST& result) {
switch (input.high) {
case 0:
if (input.low <= uint64_t(std::numeric_limits<DST>::max())) {
if (input.low <= uint64_t(function::NumericLimits<DST>::maximum())) {
result = static_cast<DST>(input.low);
return true;
}
Expand All @@ -360,9 +359,9 @@ bool TryCastInt128Template(int128_t input, DST& result) {
throw common::OverflowException(
"Cast failed. Cannot cast " + Int128_t::ToString(input) + " to unsigned type.");
}
if (input.low >=
uint64_t(std::numeric_limits<DST>::max()) - uint64_t(std::numeric_limits<DST>::max())) {
result = static_cast<DST>(std::numeric_limits<DST>::max() - input.low) - 1;
if (input.low >= uint64_t(function::NumericLimits<DST>::maximum()) -
uint64_t(function::NumericLimits<DST>::maximum())) {
result = static_cast<DST>(function::NumericLimits<DST>::maximum() - input.low) - 1;
return true;
}
break;
Expand Down Expand Up @@ -425,10 +424,11 @@ template<class REAL_T>
bool CastInt128ToFloating(int128_t input, REAL_T& result) {
switch (input.high) {
case -1:
result = -REAL_T(std::numeric_limits<REAL_T>::max() - input.low) - 1;
result = -REAL_T(function::NumericLimits<uint64_t>::maximum() - input.low) - 1;
break;
default:
result = REAL_T(input.high) * std::numeric_limits<uint64_t>::max() + REAL_T(input.low);
result =
REAL_T(input.high) * function::NumericLimits<uint64_t>::maximum() + REAL_T(input.low);
break;
}
return true;
Expand Down Expand Up @@ -522,8 +522,8 @@ bool castFloatingToInt128(REAL_T value, int128_t& result) {
if (negative) {
value = -value;
}
result.low = (uint64_t)fmod(value, REAL_T(std::numeric_limits<uint64_t>::max()));
result.high = (uint64_t)(value / REAL_T(std::numeric_limits<uint64_t>::max()));
result.low = (uint64_t)fmod(value, REAL_T(function::NumericLimits<uint64_t>::maximum()));
result.high = (uint64_t)(value / REAL_T(function::NumericLimits<uint64_t>::maximum()));
if (negative) {
Int128_t::negateInPlace(result);
}
Expand Down Expand Up @@ -619,5 +619,4 @@ int128_t::operator int64_t() const {
return NarrowCast<int64_t>(*this);
}

} // namespace common
} // namespace kuzu
} // namespace kuzu::common
55 changes: 0 additions & 55 deletions src/include/common/int128_t.h

This file was deleted.

43 changes: 41 additions & 2 deletions src/include/common/types/int128_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,51 @@

#include <stdint.h>

#include "common/int128_t.h"
#include "common/api.h"
#include "common/string_utils.h"

namespace kuzu {
namespace common {

struct int128_t;
struct KUZU_API int128_t;

// System representation for int128_t.
struct int128_t {
uint64_t low;
int64_t high;

int128_t() = default;
int128_t(int64_t value); // NOLINT: Allow implicit conversion from `int64_t`
constexpr int128_t(uint64_t low, int64_t high) : low(low), high(high) {}

constexpr int128_t(const int128_t&) = default;
constexpr int128_t(int128_t&&) = default;
int128_t& operator=(const int128_t&) = default;
int128_t& operator=(int128_t&&) = default;

// comparison operators
bool operator==(const int128_t& rhs) const;
bool operator!=(const int128_t& rhs) const;
bool operator>(const int128_t& rhs) const;
bool operator>=(const int128_t& rhs) const;
bool operator<(const int128_t& rhs) const;
bool operator<=(const int128_t& rhs) const;

// arithmetic operators
int128_t operator+(const int128_t& rhs) const;
int128_t operator-(const int128_t& rhs) const;
int128_t operator*(const int128_t& rhs) const;
int128_t operator/(const int128_t& rhs) const;
int128_t operator%(const int128_t& rhs) const;
int128_t operator-() const;

// inplace arithmetic operators
int128_t& operator+=(const int128_t& rhs);
int128_t& operator*=(const int128_t& rhs);

// cast operators
explicit operator int64_t() const;
};

class Int128_t {
public:
Expand Down
2 changes: 1 addition & 1 deletion src/include/common/types/value/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <utility>

#include "common/api.h"
#include "common/int128_t.h"
#include "common/type_utils.h"
#include "common/types/date_t.h"
#include "common/types/int128_t.h"
#include "common/types/internal_id_t.h"
#include "common/types/interval_t.h"
#include "common/types/ku_list.h"
Expand Down
2 changes: 1 addition & 1 deletion src/include/function/cast/functions/numeric_limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cstdint>
#include <limits>

#include "common/int128_t.h"
#include "common/types/int128_t.h"

namespace kuzu {
namespace function {
Expand Down
15 changes: 14 additions & 1 deletion test/test_files/tinysnb/function/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff
[43,83,67,43]
[77,64,100,54]


-LOG CastMapToString
-STATEMENT MATCH (m:movies) RETURN string(m.audience)
---- 3
Expand Down Expand Up @@ -394,6 +393,11 @@ abcd
---- 1
1|2|3|4|5|7|8|9

-LOG CastUnsignedIntToFLoatDouble
-STATEMENT Return to_float(to_uint64(1)), to_double(to_uint64(2)), to_float(to_uint32(3)), to_double(to_uint32(4)), to_float(to_uint16(5)), to_double(to_uint16(6)), to_float(to_uint8(7)), to_double(to_uint8(8))
---- 1
1.000000|2.000000|3.000000|4.000000|5.000000|6.000000|7.000000|8.000000

-LOG CastNumericalToUInt32
-STATEMENT Return to_uint32(to_int64(1)), to_uint32(to_int32(2)), to_uint32(to_int16(3)), to_uint32(to_int8(4)), to_uint32(to_uint8(5)), to_uint32(to_uint16(6)), to_uint32(to_uint64(8)), to_uint32(to_int128(9))
---- 1
Expand Down Expand Up @@ -575,3 +579,12 @@ False
---- 1
170141183460469231731687303715884105728.000000|-4324324

-LOG CastNegInt128ToFloatDouble
-STATEMENT Return to_float(to_int128(-15)), to_double(to_int128(-1)), to_float(to_int128(15)), to_double(to_int128(1))
---- 1
-15.000000|-1.000000|15.000000|1.000000

-LOG CastFloatDoubleToInt128
-STATEMENT Return to_int128(to_float(-15)), to_int128(to_double(-1)), to_int128(to_float(15)), to_int128(to_double(1))
---- 1
-15|-1|15|1

0 comments on commit 7b408f1

Please sign in to comment.