Skip to content

Commit b7a7b2b

Browse files
committed
address review comments by coderabbit
Signed-off-by: Elazar Gershuni <[email protected]>
1 parent fecfa5e commit b7a7b2b

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/crab/interval.hpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,7 @@ class interval_t final {
489489
case 16: return truncate_to<int16_t>();
490490
case 32: return truncate_to<int32_t>();
491491
case 64: return truncate_to<int64_t>();
492-
default: {
493-
CRAB_ERROR("invalid width");
494-
}
492+
default: CRAB_ERROR("Invalid width ", width);
495493
}
496494
}
497495

@@ -503,9 +501,7 @@ class interval_t final {
503501
case 16: return truncate_to<uint16_t>();
504502
case 32: return truncate_to<uint32_t>();
505503
case 64: return truncate_to<uint64_t>();
506-
default: {
507-
CRAB_ERROR("invalid width");
508-
}
504+
default: CRAB_ERROR("Invalid width ", width);
509505
}
510506
}
511507

@@ -535,7 +531,7 @@ class interval_t final {
535531
case 16: return full<int16_t>();
536532
case 32: return full<int32_t>();
537533
case 64: return full<int64_t>();
538-
default: throw std::exception();
534+
default: CRAB_ERROR("Invalid width ", width);
539535
}
540536
}
541537

@@ -545,10 +541,10 @@ class interval_t final {
545541
static interval_t unsigned_int(const int width) {
546542
switch (width) {
547543
case 8: return full<uint8_t>();
548-
case 32: return full<uint32_t>();
549544
case 16: return full<uint16_t>();
545+
case 32: return full<uint32_t>();
550546
case 64: return full<uint64_t>();
551-
default: throw std::exception();
547+
default: CRAB_ERROR("Invalid width ", width);
552548
}
553549
}
554550

@@ -561,7 +557,7 @@ class interval_t final {
561557
case 16: return nonnegative<int16_t>();
562558
case 32: return nonnegative<int32_t>();
563559
case 64: return nonnegative<int64_t>();
564-
default: throw std::exception();
560+
default: CRAB_ERROR("Invalid width ", width);
565561
}
566562
}
567563

@@ -574,7 +570,7 @@ class interval_t final {
574570
case 16: return negative<int16_t>();
575571
case 32: return negative<int32_t>();
576572
case 64: return negative<int64_t>();
577-
default: throw std::exception();
573+
default: CRAB_ERROR("Invalid width ", width);
578574
}
579575
}
580576

@@ -601,15 +597,15 @@ class interval_t final {
601597

602598
interval_t unsigned_high(bool is64) const = delete;
603599
// Return an interval in the range [INT_MAX+1, UINT_MAX], which can only
604-
// be represented as a uvalue. The svalue equivalent using the same
605-
// width would be negative_int().
600+
// be represented as a uvalue.
601+
// The svalue equivalent using the same width would be negative_int().
606602
static interval_t unsigned_high(const int width) {
607603
switch (width) {
608-
case 8: return high<uint8_t>(); ;
604+
case 8: return high<uint8_t>();
609605
case 16: return high<uint16_t>();
610606
case 32: return high<uint32_t>();
611607
case 64: return high<uint64_t>();
612-
default: throw std::exception();
608+
default: CRAB_ERROR("Invalid width ", width);
613609
}
614610
}
615611

src/crab_utils/bignums.hpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ T truncate_to(const cpp_int& n) {
2626
return static_cast<T>(static_cast<U>(n & mask));
2727
}
2828

29+
// coderabbit implementation - TODO try in the future
30+
template <std::unsigned_integral U>
31+
U truncate_to_coderabbit(const cpp_int& n) {
32+
constexpr U mask = std::numeric_limits<U>::max();
33+
return static_cast<U>(n & mask);
34+
}
35+
36+
// coderabbit implementation - TODO try in the future
37+
template <std::signed_integral S>
38+
S truncate_to_coderabbit(const cpp_int& n) {
39+
using U = std::make_unsigned_t<S>;
40+
constexpr uint32_t size = sizeof(S) * CHAR_BIT;
41+
constexpr U mask = (U{1} << size) - 1;
42+
const auto truncated = n & mask;
43+
if (truncated >= cpp_int{1} << (size - 1)) {
44+
truncated -= cpp_int{1} << size;
45+
}
46+
return static_cast<S>(truncated);
47+
}
48+
2949
template <std::integral T>
3050
bool fits(const cpp_int& n) {
3151
return std::numeric_limits<T>::min() <= n && n <= std::numeric_limits<T>::max();
@@ -128,15 +148,15 @@ class z_number final {
128148

129149
template <std::unsigned_integral U>
130150
[[nodiscard]]
131-
uint64_t cast_to() const {
151+
U cast_to() const {
132152
using S = std::make_signed_t<U>;
133153
if (fits<U>()) {
134154
return (U)_n;
135155
} else if (fits<S>()) {
136156
// Convert 32 bits from int32_t to uint32_t.
137157
return (U)(S)_n;
138158
} else {
139-
CRAB_ERROR("z_number ", _n.str(), " does not fit into an unsigned 32-bit integer");
159+
CRAB_ERROR("z_number ", _n.str(), " does not fit into ", typeid(U).name());
140160
}
141161
}
142162

@@ -154,8 +174,8 @@ class z_number final {
154174

155175
template <std::signed_integral S>
156176
[[nodiscard]]
157-
int32_t cast_to() const {
158-
return (S)cast_to_sint64();
177+
S cast_to() const {
178+
return static_cast<S>(cast_to_sint64());
159179
}
160180

161181
// For 64-bit operations, get the value as a signed 64-bit integer.
@@ -174,7 +194,7 @@ class z_number final {
174194
// For 32-bit operations, get the low 32 bits as a signed integer.
175195
[[nodiscard]]
176196
int32_t cast_to_sint32() const {
177-
return (int32_t)cast_to_sint64();
197+
return cast_to<int32_t>();
178198
}
179199

180200
// Allow casting to int32_t or int64_t as needed for finite width operations.

0 commit comments

Comments
 (0)