Skip to content

Commit adfd628

Browse files
committed
Minor fixes.
1. Remove dead code. 2. Fix nlz() function. 3. Make long_uint/long_int types compatible with STL library.
1 parent 3e6cff4 commit adfd628

File tree

6 files changed

+207
-68
lines changed

6 files changed

+207
-68
lines changed

include/slimcpplib/long_fixdiv.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,6 @@ constexpr long_fixed_divider<type_t>::long_fixed_divider(const type_t& multiplie
112112
////////////////////////////////////////////////////////////////////////////////////////////////////
113113
// public methods
114114

115-
template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
116-
constexpr uint_t nlz_costexpr(type_t value) noexcept
117-
{
118-
type_t mask = ~type_t(0) ^ ((~type_t(0)) / 2);
119-
uint_t count = 0;
120-
121-
for (uint_t n = 0; n < bit_count_v<type_t>; ++n) {
122-
123-
if ((value & mask) != 0)
124-
break;
125-
126-
mask /= 2;
127-
++count;
128-
}
129-
130-
return count;
131-
}
132-
133-
134-
135-
////////////////////////////////////////////////////////////////////////////////////////////////////
136115
template<typename type_t>
137116
constexpr long_fixed_divider<type_t> long_fixed_divider<type_t>::create(const type_t& divider) noexcept
138117
{

include/slimcpplib/long_int.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,6 @@ class long_int_t : public long_uint_t<native_t, size>
111111

112112

113113

114-
////////////////////////////////////////////////////////////////////////////////////////////////////
115-
// is_signed_v
116-
////////////////////////////////////////////////////////////////////////////////////////////////////
117-
template<typename native_t, uint_t size>
118-
inline constexpr bool is_signed_v<long_int_t<native_t, size>> = true;
119-
120-
121-
122114
////////////////////////////////////////////////////////////////////////////////////////////////////
123115
// standalone methods
124116
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -466,7 +458,7 @@ constexpr long_int_t<native_t, size> long_int_t<native_t, size>::operator%(const
466458
// standalone methods
467459
////////////////////////////////////////////////////////////////////////////////////////////////////
468460

469-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
461+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
470462
constexpr bool operator==(type_t value1, const long_int_t<native_t, size>& value2) noexcept
471463
{
472464
return long_int_t<native_t, size>(value1) == value2;
@@ -475,7 +467,7 @@ constexpr bool operator==(type_t value1, const long_int_t<native_t, size>& value
475467

476468

477469
////////////////////////////////////////////////////////////////////////////////////////////////////
478-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
470+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
479471
constexpr bool operator!=(type_t value1, const long_int_t<native_t, size>& value2) noexcept
480472
{
481473
return long_int_t<native_t, size>(value1) != value2;
@@ -484,7 +476,7 @@ constexpr bool operator!=(type_t value1, const long_int_t<native_t, size>& value
484476

485477

486478
////////////////////////////////////////////////////////////////////////////////////////////////////
487-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
479+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
488480
constexpr bool operator<(type_t value1, const long_int_t<native_t, size>& value2) noexcept
489481
{
490482
return long_int_t<native_t, size>(value1) < value2;
@@ -493,7 +485,7 @@ constexpr bool operator<(type_t value1, const long_int_t<native_t, size>& value2
493485

494486

495487
////////////////////////////////////////////////////////////////////////////////////////////////////
496-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
488+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
497489
constexpr bool operator<=(type_t value1, const long_int_t<native_t, size>& value2) noexcept
498490
{
499491
return long_int_t<native_t, size>(value1) <= value2;
@@ -502,7 +494,7 @@ constexpr bool operator<=(type_t value1, const long_int_t<native_t, size>& value
502494

503495

504496
////////////////////////////////////////////////////////////////////////////////////////////////////
505-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
497+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
506498
constexpr bool operator>(type_t value1, const long_int_t<native_t, size>& value2) noexcept
507499
{
508500
return long_int_t<native_t, size>(value1) > value2;
@@ -511,7 +503,7 @@ constexpr bool operator>(type_t value1, const long_int_t<native_t, size>& value2
511503

512504

513505
////////////////////////////////////////////////////////////////////////////////////////////////////
514-
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<std::is_unsigned_v<type_t> || std::is_signed_v<type_t>, int> = 0>
506+
template<typename type_t, typename native_t, uint_t size, std::enable_if_t<is_unsigned_v<type_t> || is_signed_v<type_t>, int> = 0>
515507
constexpr bool operator>=(type_t value1, const long_int_t<native_t, size>& value2) noexcept
516508
{
517509
return long_int_t<native_t, size>(value1) >= value2;

include/slimcpplib/long_math.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ namespace slim
5050
// type definitions
5151
////////////////////////////////////////////////////////////////////////////////////////////////////
5252

53-
using uint_t = uintptr_t;
54-
using int_t = intptr_t;
53+
using uint_t = std::uintptr_t;
54+
using int_t = std::intptr_t;
5555

5656

5757

@@ -71,9 +71,10 @@ constexpr uint_t bit_count_v = byte_count_v<type_t>* CHAR_BIT;
7171
////////////////////////////////////////////////////////////////////////////////////////////////////
7272

7373
template<typename type_t>
74-
inline constexpr bool is_unsigned_v = std::is_unsigned_v<type_t>;
74+
inline constexpr bool is_unsigned_v = std::numeric_limits<type_t>::is_integer && !std::numeric_limits<type_t>::is_signed;
7575
template<typename type_t>
76-
inline constexpr bool is_signed_v = std::is_signed_v<type_t>;
76+
inline constexpr bool is_signed_v = std::numeric_limits<type_t>::is_integer && std::numeric_limits<type_t>::is_signed;
77+
7778

7879

7980
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -83,9 +84,9 @@ inline constexpr bool is_signed_v = std::is_signed_v<type_t>;
8384
template<typename type_t>
8485
inline constexpr bool is_unsigned_array_v = false;
8586
template<typename type_t, uint_t size>
86-
inline constexpr bool is_unsigned_array_v<std::array<type_t, size>> = is_unsigned_v<type_t> && true;
87+
inline constexpr bool is_unsigned_array_v<std::array<type_t, size>> = is_unsigned_v<type_t>;
8788
template<typename type_t, uint_t size>
88-
inline constexpr bool is_unsigned_array_v<const std::array<type_t, size>> = is_unsigned_v<type_t> && true;
89+
inline constexpr bool is_unsigned_array_v<const std::array<type_t, size>> = is_unsigned_v<type_t>;
8990

9091

9192

include/slimcpplib/long_math_gcc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,17 @@ inline uint_t popcnt(uint64_t value) noexcept
126126
////////////////////////////////////////////////////////////////////////////////////////////////////
127127
inline uint_t nlz(uint8_t value) noexcept
128128
{
129-
return value ? __builtin_clz(value) : bit_count_v<uint8_t>;
129+
constexpr uint_t offset = bit_count_v<decltype(__builtin_clz(value))> - bit_count_v<uint8_t>;
130+
return value ? __builtin_clz(value) - offset : bit_count_v<uint8_t>;
130131
}
131132

132133

133134

134135
////////////////////////////////////////////////////////////////////////////////////////////////////
135136
inline uint_t nlz(uint16_t value) noexcept
136137
{
137-
return value ? __builtin_clz(value) : bit_count_v<uint16_t>;
138+
constexpr uint_t offset = bit_count_v<decltype(__builtin_clz(value))> - bit_count_v<uint16_t>;
139+
return value ? __builtin_clz(value) - offset : bit_count_v<uint16_t>;
138140
}
139141

140142

@@ -151,7 +153,7 @@ inline uint_t nlz(uint32_t value) noexcept
151153
inline uint_t nlz(uint64_t value) noexcept
152154
{
153155
#ifdef __x86_64__
154-
return value ? __builtin_clz(value) : bit_count_v<uint64_t>;
156+
return value ? __builtin_clzl(value) : bit_count_v<uint64_t>;
155157
#else
156158
const uint32_t value_hi = static_cast<uint32_t>(half_hi(value));
157159
const uint32_t value_lo = static_cast<uint32_t>(half_lo(value));

include/slimcpplib/long_math_long.h

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,181 @@ class long_uint_t;
5353
template<typename type_t, uint_t size>
5454
class long_int_t;
5555

56+
} // namespace slim
57+
58+
namespace std
59+
{
60+
////////////////////////////////////////////////////////////////////////////////////////////////////
61+
// class numeric_limits<slim::long_uint_t<type_t, size>>
62+
////////////////////////////////////////////////////////////////////////////////////////////////////
63+
template<typename type_t, size_t size>
64+
struct numeric_limits<slim::long_uint_t<type_t, size>> {
65+
public:
66+
67+
using long_uint_t = slim::long_uint_t<type_t, size>;
68+
69+
static const bool is_specialized = true;
70+
static const bool is_signed = false;
71+
static const bool is_integer = true;
72+
static const bool is_exact = true;
73+
74+
static const bool has_infinity = false;
75+
static const bool has_quiet_NaN = false;
76+
static const bool has_signaling_NaN = false;
77+
static const float_denorm_style has_denorm = denorm_absent;
78+
static const bool has_denorm_loss = false;
79+
static const float_round_style round_style = round_toward_zero;
80+
81+
static const bool is_iec559 = false;
82+
static const bool is_bounded = false;
83+
static const bool is_modulo = false;
84+
85+
static const int digits = 0;
86+
static const int digits10 = 0;
87+
static const int radix = 2;
88+
89+
static const int min_exponent = 0;
90+
static const int min_exponent10 = 0;
91+
static const int max_exponent = 0;
92+
static const int max_exponent10 = 0;
93+
94+
static const bool traps = false;
95+
static const bool tinyness_before = false;
96+
97+
static long_uint_t min() noexcept
98+
{
99+
return long_uint_t(0);
100+
}
101+
102+
static constexpr long_uint_t lowest() noexcept
103+
{
104+
return long_uint_t(0);
105+
}
106+
107+
static long_uint_t max() noexcept
108+
{
109+
return long_uint_t(-1);
110+
}
111+
112+
static long_uint_t epsilon() noexcept
113+
{
114+
return long_uint_t(0);
115+
}
116+
117+
static long_uint_t round_error() noexcept
118+
{
119+
return long_uint_t(0);
120+
}
121+
122+
static long_uint_t infinity() noexcept
123+
{
124+
return long_uint_t(0);
125+
}
126+
127+
static long_uint_t quiet_NaN() noexcept
128+
{
129+
return long_uint_t(0);
130+
}
131+
132+
static long_uint_t signaling_NaN() noexcept
133+
{
134+
return long_uint_t(0);
135+
}
136+
137+
static long_uint_t denorm_min() noexcept
138+
{
139+
return long_uint_t(0);
140+
}
141+
};
142+
56143

57144

145+
////////////////////////////////////////////////////////////////////////////////////////////////////
146+
// class numeric_limits<slim::long_int_t<type_t, size>>
147+
////////////////////////////////////////////////////////////////////////////////////////////////////
148+
template<typename type_t, size_t size>
149+
struct numeric_limits<slim::long_int_t<type_t, size>> {
150+
public:
151+
using long_int_t = slim::long_int_t<type_t, size>;
152+
153+
static const bool is_specialized = true;
154+
static const bool is_signed = true;
155+
static const bool is_integer = true;
156+
static const bool is_exact = true;
157+
158+
static const bool has_infinity = false;
159+
static const bool has_quiet_NaN = false;
160+
static const bool has_signaling_NaN = false;
161+
static const float_denorm_style has_denorm = denorm_absent;
162+
static const bool has_denorm_loss = false;
163+
static const float_round_style round_style = round_toward_zero;
164+
165+
static const bool is_iec559 = false;
166+
static const bool is_bounded = false;
167+
static const bool is_modulo = false;
168+
169+
static const int digits = 0;
170+
static const int digits10 = 0;
171+
static const int radix = 2;
172+
173+
static const int min_exponent = 0;
174+
static const int min_exponent10 = 0;
175+
static const int max_exponent = 0;
176+
static const int max_exponent10 = 0;
177+
178+
static const bool traps = false;
179+
static const bool tinyness_before = false;
180+
181+
static long_int_t min() noexcept
182+
{
183+
return slim::long_uint_t<type_t, size>(1) << (slim::bit_count_v<long_int_t> - 1);
184+
}
185+
186+
static constexpr long_int_t lowest() noexcept
187+
{
188+
return min();
189+
}
190+
191+
static long_int_t max() noexcept
192+
{
193+
return ~slim::long_uint_t<type_t, size>(min());
194+
}
195+
196+
static long_int_t epsilon() noexcept
197+
{
198+
return long_int_t(0);
199+
}
200+
201+
static long_int_t round_error() noexcept
202+
{
203+
return long_int_t(0);
204+
}
205+
206+
static long_int_t infinity() noexcept
207+
{
208+
return long_int_t(0);
209+
}
210+
211+
static long_int_t quiet_NaN() noexcept
212+
{
213+
return long_int_t(0);
214+
}
215+
216+
static long_int_t signaling_NaN() noexcept
217+
{
218+
return long_int_t(0);
219+
}
220+
221+
static long_int_t denorm_min() noexcept
222+
{
223+
return long_int_t(0);
224+
}
225+
};
226+
227+
} // namespace std
228+
229+
namespace slim
230+
{
58231
////////////////////////////////////////////////////////////////////////////////////////////////////
59232
// make_unsigned_t
60233
////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)