-
-
Notifications
You must be signed in to change notification settings - Fork 15
FIX: SLEEF migration to 4.0 #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
259225c
b3a7a1e
74fa409
568225d
bab4d0a
dde614b
1ec36d6
3d37460
7db9e43
e5c6361
c918666
cfb8e38
4b44a6e
2430dda
fda5ce2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,13 @@ typedef Sleef_quad (*unary_op_quad_def)(const Sleef_quad *); | |
| // Unary Quad operations with 2 outputs (for modf, frexp) | ||
| typedef void (*unary_op_2out_quad_def)(const Sleef_quad *, Sleef_quad *, Sleef_quad *); | ||
|
|
||
| static Sleef_quad | ||
| [[maybe_unused]] static Sleef_quad | ||
| quad_negative(const Sleef_quad *op) | ||
| { | ||
| return Sleef_negq1(*op); | ||
| } | ||
|
|
||
| static Sleef_quad | ||
| [[maybe_unused]] static Sleef_quad | ||
|
||
| quad_positive(const Sleef_quad *op) | ||
| { | ||
| return *op; | ||
|
|
@@ -929,15 +929,15 @@ static inline Sleef_quad quad_set_words64(int64_t hx, uint64_t lx) | |
| static inline Sleef_quad | ||
| quad_nextafter(const Sleef_quad *x, const Sleef_quad *y) | ||
| { | ||
| int64_t hx, hy, ix, iy; | ||
| int64_t hx, hy, ix; | ||
| uint64_t lx, ly; | ||
|
|
||
| quad_get_words64(&hx, &lx, *x); | ||
| quad_get_words64(&hy, &ly, *y); | ||
|
|
||
| // extracting absolute value | ||
| ix = hx & 0x7fffffffffffffffLL; | ||
| iy = hy & 0x7fffffffffffffffLL; | ||
| (void)ly; // unused but needed for quad_get_words64 | ||
SwayamInSync marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // NaN if either is NaN | ||
| if (Sleef_iunordq1(*x, *y)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,12 +134,17 @@ NumPyOS_ascii_strtoq(const char *s, QuadBackendType backend, quad_value *out_val | |
| } | ||
| } | ||
|
|
||
| // Set NaN value (sign is ignored for NaN) | ||
| // Set NaN value with sign preserved | ||
| if (backend == BACKEND_SLEEF) { | ||
| out_value->sleef_value = QUAD_PRECISION_NAN; | ||
| Sleef_quad nan_val = QUAD_PRECISION_NAN; | ||
| // Apply sign to NaN (negative NaN has sign bit set) | ||
| if (sign < 0) { | ||
| nan_val = Sleef_negq1(nan_val); | ||
| } | ||
| out_value->sleef_value = nan_val; | ||
| } | ||
| else { | ||
| out_value->longdouble_value = nanl(""); | ||
| out_value->longdouble_value = sign < 0 ? -nanl("") : nanl(""); | ||
| } | ||
|
|
||
| if (endptr) { | ||
|
|
@@ -157,15 +162,98 @@ int cstring_to_quad(const char *str, QuadBackendType backend, quad_value *out_va | |
| char **endptr, bool require_full_parse) | ||
| { | ||
| if(backend == BACKEND_SLEEF) { | ||
| out_value->sleef_value = Sleef_strtoq(str, endptr); | ||
| // SLEEF 4.0's Sleef_strtoq doesn't properly set endptr to indicate | ||
SwayamInSync marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // where parsing stopped. It always sets endptr to the end of the string. | ||
| // We need to manually validate and track the parse position. | ||
|
|
||
| const char *p = str; | ||
|
|
||
| // Skip leading whitespace | ||
| while (ascii_isspace(*p)) { | ||
| p++; | ||
| } | ||
|
|
||
| // Handle optional sign | ||
| if (*p == '+' || *p == '-') { | ||
| p++; | ||
| } | ||
|
|
||
| // Must have at least one digit or decimal point followed by digit | ||
| int has_digits = 0; | ||
|
|
||
| // Parse integer part | ||
| while (ascii_isdigit(*p)) { | ||
| has_digits = 1; | ||
| p++; | ||
| } | ||
|
|
||
| // Parse decimal point and fractional part | ||
| if (*p == '.') { | ||
| p++; | ||
| while (ascii_isdigit(*p)) { | ||
| has_digits = 1; | ||
| p++; | ||
| } | ||
| } | ||
|
|
||
| // Must have at least one digit somewhere | ||
| if (!has_digits) { | ||
| if (endptr) *endptr = (char *)str; | ||
| return -1; | ||
| } | ||
|
|
||
| // Parse optional exponent | ||
| if (*p == 'e' || *p == 'E') { | ||
| const char *exp_start = p; | ||
| p++; | ||
|
|
||
| // Optional sign in exponent | ||
| if (*p == '+' || *p == '-') { | ||
| p++; | ||
| } | ||
|
|
||
| // Must have at least one digit in exponent | ||
| if (!ascii_isdigit(*p)) { | ||
| // Invalid exponent, backtrack | ||
| p = exp_start; | ||
| } else { | ||
| while (ascii_isdigit(*p)) { | ||
| p++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Now p points to where valid parsing ends | ||
| // SLEEF 4.0's Sleef_strtoq has a bug where it doesn't properly stop at whitespace | ||
| // or other delimiters. We need to create a null-terminated substring. | ||
| size_t len = p - str; | ||
| char *temp = (char *)malloc(len + 1); | ||
| if (!temp) { | ||
| if (endptr) *endptr = (char *)str; | ||
| return -1; | ||
| } | ||
| memcpy(temp, str, len); | ||
| temp[len] = '\0'; | ||
|
|
||
| // Call Sleef_strtoq with the bounded string | ||
| char *sleef_endptr; | ||
| out_value->sleef_value = Sleef_strtoq(temp, &sleef_endptr); | ||
|
||
| free(temp); | ||
|
|
||
| // Set endptr to our calculated position | ||
| if (endptr) { | ||
| *endptr = (char *)p; | ||
| } | ||
|
|
||
| } else { | ||
| out_value->longdouble_value = strtold(str, endptr); | ||
| } | ||
| if(*endptr == str) | ||
|
|
||
| if(endptr && *endptr == str) | ||
| return -1; // parse error - nothing was parsed | ||
|
|
||
| // If full parse is required | ||
| if(require_full_parse && **endptr != '\0') | ||
| if(require_full_parse && endptr && **endptr != '\0') | ||
| return -1; // parse error - characters remain to be converted | ||
|
|
||
| return 0; // success | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, I would think
static inlinefunctions shouldn't warn this, but OK. I wonder if it uses__attribute__to force-inline, but should also add theinlineanyway.