@@ -95,25 +95,23 @@ quad_to_quad_same_value_check(const quad_value *in_val, QuadBackendType backend_
9595 memcpy (&roundtrip.sleef_value , &temp, sizeof (Sleef_quad));
9696 }
9797
98- // Compare in SLEEF domain
99- if (Sleef_iunordq1 (in_val->sleef_value , roundtrip.sleef_value ))
98+ // Compare in SLEEF domain && signbit preserved
99+ bool is_sign_preserved = (quad_signbit (&in_val->sleef_value ) == quad_signbit (&roundtrip.sleef_value ));
100+ if (Sleef_iunordq1 (in_val->sleef_value , roundtrip.sleef_value ) && is_sign_preserved)
100101 return 1 ; // Both NaN
101- if (Sleef_icmpeqq1 (in_val->sleef_value , roundtrip.sleef_value ))
102+ if (Sleef_icmpeqq1 (in_val->sleef_value , roundtrip.sleef_value ) && is_sign_preserved )
102103 return 1 ; // Equal
103- if (Sleef_icmpeqq1 (in_val->sleef_value , QUAD_ZERO ) && Sleef_icmpeqq1 (roundtrip.sleef_value , QUAD_ZERO ))
104- return 1 ; // Both zeros
105104 }
106105 else {
107106 // Input was longdouble, output is SLEEF
108107 // Convert SLEEF back to longdouble for comparison
109- roundtrip.longdouble_value = static_cast <long double >(Sleef_cast_to_doubleq1 (out_val->sleef_value ));
108+ roundtrip.longdouble_value = static_cast <long double >(cast_sleef_to_double (out_val->sleef_value ));
110109
111- // Compare in longdouble domain
112- if (std::isnan (in_val->longdouble_value ) && std::isnan (roundtrip.longdouble_value ))
110+ // Compare in longdouble domain && signbit preserved
111+ bool is_sign_preserved = (ld_signbit (&in_val->longdouble_value ) == ld_signbit (&roundtrip.longdouble_value ));
112+ if ((std::isnan (in_val->longdouble_value ) && std::isnan (roundtrip.longdouble_value )) && is_sign_preserved)
113113 return 1 ;
114- if (in_val->longdouble_value == roundtrip.longdouble_value )
115- return 1 ;
116- if (in_val->longdouble_value == 0 .0L && roundtrip.longdouble_value == 0 .0L )
114+ if ((in_val->longdouble_value == roundtrip.longdouble_value ) && is_sign_preserved)
117115 return 1 ;
118116 }
119117
@@ -158,7 +156,7 @@ quad_to_quad_strided_loop(PyArrayMethod_Context *context, char *const data[],
158156 quad_value out_val;
159157 if (backend_in == BACKEND_SLEEF )
160158 {
161- out_val.longdouble_value = static_cast <long double >(Sleef_cast_to_doubleq1 (in_val.sleef_value ));
159+ out_val.longdouble_value = static_cast <long double >(cast_sleef_to_double (in_val.sleef_value ));
162160 }
163161 else
164162 {
@@ -441,25 +439,20 @@ quad_to_string_same_value_check(const quad_value *in_val, const char *str_buf, n
441439 }
442440 free (truncated_str);
443441
444- // Compare original and roundtripped values
442+ // Compare original and roundtripped values along with signbit
445443 if (backend == BACKEND_SLEEF ) {
446444 // NaN == NaN for same_value purposes
447- if (Sleef_iunordq1 (in_val->sleef_value , roundtrip.sleef_value ))
448- return 1 ;
449- if (Sleef_icmpeqq1 (in_val->sleef_value , roundtrip.sleef_value ))
445+ bool is_sign_preserved = (quad_signbit (&in_val->sleef_value ) == quad_signbit (&roundtrip.sleef_value ));
446+ if (Sleef_iunordq1 (in_val->sleef_value , roundtrip.sleef_value ) && is_sign_preserved)
450447 return 1 ;
451- // Handle -0.0 == +0.0 case
452- if (Sleef_icmpeqq1 (in_val->sleef_value , QUAD_ZERO ) &&
453- Sleef_icmpeqq1 (roundtrip.sleef_value , QUAD_ZERO ))
448+ if (Sleef_icmpeqq1 (in_val->sleef_value , roundtrip.sleef_value ) && is_sign_preserved)
454449 return 1 ;
455450 }
456451 else {
457- if (std::isnan (in_val->longdouble_value ) && std::isnan (roundtrip.longdouble_value ))
458- return 1 ;
459- if (in_val->longdouble_value == roundtrip.longdouble_value )
452+ bool is_sign_preserved = (ld_signbit (&in_val->longdouble_value ) == ld_signbit (&roundtrip.longdouble_value ));
453+ if ((std::isnan (in_val->longdouble_value ) && std::isnan (roundtrip.longdouble_value )) && is_sign_preserved)
460454 return 1 ;
461- // Handle -0.0 == +0.0 case
462- if (in_val->longdouble_value == 0 .0L && roundtrip.longdouble_value == 0 .0L )
455+ if ((in_val->longdouble_value == roundtrip.longdouble_value ) && is_sign_preserved)
463456 return 1 ;
464457 }
465458
@@ -1316,7 +1309,7 @@ inline npy_half
13161309from_quad<spec_npy_half>(const quad_value *x, QuadBackendType backend)
13171310{
13181311 if (backend == BACKEND_SLEEF ) {
1319- double d = Sleef_cast_to_doubleq1 (x->sleef_value );
1312+ double d = cast_sleef_to_double (x->sleef_value );
13201313 return npy_double_to_half (d);
13211314 }
13221315 else {
@@ -1329,7 +1322,7 @@ inline float
13291322from_quad<float >(const quad_value *x, QuadBackendType backend)
13301323{
13311324 if (backend == BACKEND_SLEEF ) {
1332- return (float )Sleef_cast_to_doubleq1 (x->sleef_value );
1325+ return (float )cast_sleef_to_double (x->sleef_value );
13331326 }
13341327 else {
13351328 return (float )x->longdouble_value ;
@@ -1341,7 +1334,7 @@ inline double
13411334from_quad<double >(const quad_value *x, QuadBackendType backend)
13421335{
13431336 if (backend == BACKEND_SLEEF ) {
1344- return Sleef_cast_to_doubleq1 (x->sleef_value );
1337+ return cast_sleef_to_double (x->sleef_value );
13451338 }
13461339 else {
13471340 return (double )x->longdouble_value ;
@@ -1353,7 +1346,7 @@ inline long double
13531346from_quad<long double >(const quad_value *x, QuadBackendType backend)
13541347{
13551348 if (backend == BACKEND_SLEEF ) {
1356- return (long double )Sleef_cast_to_doubleq1 (x->sleef_value );
1349+ return (long double )cast_sleef_to_double (x->sleef_value );
13571350 }
13581351 else {
13591352 return x->longdouble_value ;
@@ -1367,23 +1360,20 @@ static inline int quad_to_numpy_same_value_check(const quad_value *x, QuadBacken
13671360 quad_value roundtrip = to_quad<T>(*y, backend);
13681361 if (backend == BACKEND_SLEEF )
13691362 {
1370- if (Sleef_iunordq1 (x->sleef_value , roundtrip.sleef_value ))
1363+ bool is_sign_preserved = (quad_signbit (&x->sleef_value ) == quad_signbit (&roundtrip.sleef_value ));
1364+ if (Sleef_iunordq1 (x->sleef_value , roundtrip.sleef_value ) && is_sign_preserved)
13711365 return 1 ;
1372- if (Sleef_icmpeqq1 (x->sleef_value , roundtrip.sleef_value ))
1373- return 1 ;
1374- // Handle -0.0 == +0.0 case: both zeros are considered equal for same_value casting
1375- if (Sleef_icmpeqq1 (x->sleef_value , QUAD_ZERO ) && Sleef_icmpeqq1 (roundtrip.sleef_value , QUAD_ZERO ))
1366+ if (Sleef_icmpeqq1 (x->sleef_value , roundtrip.sleef_value ) && is_sign_preserved)
13761367 return 1 ;
13771368 }
13781369 else
13791370 {
1380- if (std::isnan (x->longdouble_value ) && std::isnan (roundtrip.longdouble_value ))
1381- return 1 ;
1382- if (x->longdouble_value == roundtrip.longdouble_value )
1371+ bool is_sign_preserved = (ld_signbit (&x->longdouble_value ) == ld_signbit (&roundtrip.longdouble_value ));
1372+ if ((std::isnan (x->longdouble_value ) && std::isnan (roundtrip.longdouble_value )) && is_sign_preserved)
13831373 return 1 ;
1384- // Handle -0.0 == +0.0 case for longdouble backend
1385- if (x->longdouble_value == 0 .0L && roundtrip.longdouble_value == 0 .0L )
1374+ if ((x->longdouble_value == roundtrip.longdouble_value ) && is_sign_preserved)
13861375 return 1 ;
1376+
13871377 }
13881378 Sleef_quad sleef_val = quad_to_sleef_quad (x, backend);
13891379 const char *val_str = quad_to_string_adaptive_cstr (&sleef_val, QUAD_STR_WIDTH );
0 commit comments