Skip to content

Commit dc4ee25

Browse files
committed
simplify filtering logic for numeric guidance messages to match the other SDKs and make it possible to only send better examples
1 parent e6984a1 commit dc4ee25

1 file changed

Lines changed: 11 additions & 68 deletions

File tree

antithesis_sdk.h

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <utility>
4343

4444
namespace antithesis {
45-
inline const char* SDK_VERSION = "0.4.6";
45+
inline const char* SDK_VERSION = "0.4.7";
4646
inline const char* PROTOCOL_VERSION = "1.1.0";
4747

4848
struct JSON; struct JSONArray;
@@ -605,16 +605,16 @@ namespace antithesis::internal::assertions {
605605
const char* message;
606606
LocationInfo location;
607607
GuidepostType type;
608-
// an approximation of (left - right) / 2; contains an absolute value and a sign bit
609-
std::pair<NumericValue, bool> extreme_half_gap;
608+
// (left - right)
609+
double extreme_gap;
610610

611611
NumericGuidepost(const char* message, LocationInfo&& location, GuidepostType type) :
612612
message(message), location(std::move(location)), type(type) {
613613
this->add_to_catalog();
614614
if (type == GUIDEPOST_MAXIMIZE) {
615-
extreme_half_gap = { std::numeric_limits<NumericValue>::max(), false };
615+
extreme_gap = -1.0 * std::numeric_limits<double>::max();
616616
} else {
617-
extreme_half_gap = { std::numeric_limits<NumericValue>::max(), true };
617+
extreme_gap = std::numeric_limits<double>::max();
618618
}
619619
}
620620

@@ -633,75 +633,18 @@ namespace antithesis::internal::assertions {
633633
get_lib_handler().output(catalog);
634634
}
635635

636-
std::pair<NumericValue, bool> compute_half_gap(NumericValue left, NumericValue right) {
637-
// An extremely baroque way to compute (left - right) / 2, rounded toward 0, without overflowing or underflowing
638-
if (std::is_integral_v<NumericValue>) {
639-
// If both numbers are odd then the gap doesn't change if we subtract 1 from both sides
640-
// Also subtracting 1 from both sides won't underflow
641-
if (left % 2 == 1 && right % 2 == 1)
642-
return compute_half_gap( left - 1, right - 1);
643-
// If one number is odd then we subtract 1 from the larger number
644-
// This rounds the computation toward 0 but again won't underflow
645-
if (left % 2 == 1 || right % 2 == 1) {
646-
if (left > right) {
647-
return compute_half_gap( left - 1, right );
648-
} else {
649-
return compute_half_gap( left, right - 1 );
650-
}
651-
}
652-
// At this point both numbers are even, so the midpoint calculation is exact
653-
NumericValue half_left = left / 2;
654-
NumericValue half_right = right / 2;
655-
NumericValue midpoint = half_left + half_right;
656-
// This won't overflow or underflow because we're subtracting the midpoint
657-
// We compute a positive value and a sign so that we don't have to do weird things with unsigned types
658-
if (left > right) {
659-
return { midpoint - right, true };
660-
} else {
661-
return { right - midpoint, false };
662-
}
663-
} else {
664-
// If it's floating point we don't need to worry about overflowing, just do the arithmetic
665-
return { left > right ? (left - right) / 2 : (right - left) / 2, left > right };
666-
}
667-
}
668-
669-
bool should_send_value(std::pair<NumericValue, bool> half_gap) {
636+
bool should_send_value(double gap) {
670637
if (this->type == GUIDEPOST_MAXIMIZE) {
671-
if (half_gap.second && !extreme_half_gap.second) {
672-
// we're positive and the extreme value isn't; always send back
673-
return true;
674-
} else if (!half_gap.second && extreme_half_gap.second) {
675-
// we're negative and the extreme value is positive; never send back
676-
return false;
677-
} else if (half_gap.second && extreme_half_gap.second) {
678-
// both positive; send back if our absolute value is larger
679-
return half_gap.first > extreme_half_gap.first;
680-
} else {
681-
// both negative; send back if our absolute value is smaller
682-
return half_gap.first < extreme_half_gap.first;
683-
}
638+
return gap > extreme_gap;
684639
} else {
685-
if (half_gap.second && !extreme_half_gap.second) {
686-
// we're positive and the extreme value isn't; never send back
687-
return false;
688-
} else if (!half_gap.second && extreme_half_gap.second) {
689-
// we're negative and the extreme value is positive; always send back
690-
return true;
691-
} else if (half_gap.second && extreme_half_gap.second) {
692-
// both positive; send back if our absolute value is smaller
693-
return half_gap.first < extreme_half_gap.first;
694-
} else {
695-
// both negative; send back if our absolute value is larger
696-
return half_gap.first > extreme_half_gap.first;
697-
}
640+
return gap < extreme_gap;
698641
}
699642
}
700643

701644
[[clang::always_inline]] inline void send_guidance(Value value) {
702-
std::pair<NumericValue, bool> half_gap = compute_half_gap(value.first, value.second);
703-
if (should_send_value(half_gap)) {
704-
extreme_half_gap = half_gap;
645+
double gap = static_cast<double>(value.first) - static_cast<double>(value.second);
646+
if (should_send_value(gap)) {
647+
extreme_gap = gap;
705648
std::string id = make_key(this->message, this->location);
706649
JSON guidance{
707650
{"antithesis_guidance", JSON{

0 commit comments

Comments
 (0)