Kùzu C++ API
Loading...
Searching...
No Matches
numeric_utils.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include "int128_t.h"
6#include "types.h"
7#include <bit>
8#include <concepts>
9
10namespace kuzu {
11namespace common {
12namespace numeric_utils {
13
14template<typename T>
15concept IsIntegral = std::integral<T> || std::same_as<std::remove_cvref_t<T>, int128_t>;
16
17template<typename T>
18concept IsSigned = std::same_as<T, int128_t> || std::numeric_limits<T>::is_signed;
19
20template<typename T>
21concept IsUnSigned = std::numeric_limits<T>::is_unsigned;
22
23template<typename T>
24struct MakeSigned {
25 using type = std::make_signed_t<T>;
26};
27
28template<>
30 using type = int128_t;
31};
32
33template<typename T>
35
36template<typename T>
38 using type = std::make_unsigned_t<T>;
39};
40
41template<>
43 // currently evaluates to int128_t as we don't have an uint128_t type
44 using type = int128_t;
45};
46
47template<typename T>
49
50template<typename T>
51decltype(auto) makeValueSigned(T value) {
52 return static_cast<MakeSignedT<T>>(value);
53}
54
55template<typename T>
56decltype(auto) makeValueUnSigned(T value) {
57 return static_cast<MakeUnSignedT<T>>(value);
58}
59
60template<typename T>
61constexpr int bitWidth(T x) {
62 return std::bit_width(x);
63}
64
65template<>
66constexpr int bitWidth<int128_t>(int128_t x) {
67 if (x.high != 0) {
68 constexpr size_t BITS_PER_BYTE = 8;
69 return sizeof(x.low) * BITS_PER_BYTE + std::bit_width(makeValueUnSigned(x.high));
70 }
71 return std::bit_width(x.low);
72}
73} // namespace numeric_utils
74} // namespace common
75} // namespace kuzu
Definition numeric_utils.h:15
Definition numeric_utils.h:18
Definition numeric_utils.h:21
decltype(auto) makeValueSigned(T value)
Definition numeric_utils.h:51
constexpr int bitWidth(T x)
Definition numeric_utils.h:61
constexpr int bitWidth< int128_t >(int128_t x)
Definition numeric_utils.h:66
typename MakeUnSigned< T >::type MakeUnSignedT
Definition numeric_utils.h:48
decltype(auto) makeValueUnSigned(T value)
Definition numeric_utils.h:56
typename MakeSigned< T >::type MakeSignedT
Definition numeric_utils.h:34
struct KUZU_API int128_t
Definition int128_t.h:17
Definition alter_type.h:5
Definition int128_t.h:20
uint64_t low
Definition int128_t.h:21
int64_t high
Definition int128_t.h:22
Definition numeric_utils.h:24
std::make_signed_t< T > type
Definition numeric_utils.h:25
Definition numeric_utils.h:37
std::make_unsigned_t< T > type
Definition numeric_utils.h:38