@@ -1065,11 +1065,21 @@ inline quad_value
10651065to_quad<spec_npy_half>(npy_half x, QuadBackendType backend)
10661066{
10671067 quad_value result;
1068+ double d = npy_half_to_double (x);
10681069 if (backend == BACKEND_SLEEF ) {
1069- result.sleef_value = Sleef_cast_from_doubleq1 (npy_half_to_double (x));
1070+ if (std::isnan (d)) {
1071+ result.sleef_value = std::signbit (d) ? QUAD_PRECISION_NEG_NAN : QUAD_PRECISION_NAN ;
1072+ }
1073+ else if (std::isinf (d)) {
1074+ result.sleef_value = (d > 0 ) ? QUAD_PRECISION_INF : QUAD_PRECISION_NINF ;
1075+ }
1076+ else {
1077+ Sleef_quad temp = Sleef_cast_from_doubleq1 (d);
1078+ std::memcpy (&result.sleef_value , &temp, sizeof (Sleef_quad));
1079+ }
10701080 }
10711081 else {
1072- result.longdouble_value = (long double )npy_half_to_double (x) ;
1082+ result.longdouble_value = (long double )d ;
10731083 }
10741084 return result;
10751085}
@@ -1390,7 +1400,61 @@ static inline int quad_to_numpy_same_value_check(const quad_value *x, QuadBacken
13901400 quad_value roundtrip = to_quad<T>(*y, backend);
13911401 if (backend == BACKEND_SLEEF )
13921402 {
1393- bool is_sign_preserved = (quad_signbit (&x->sleef_value ) == quad_signbit (&roundtrip.sleef_value ));
1403+
1404+ // debug statements
1405+ union {
1406+ Sleef_quad q;
1407+ struct {
1408+ uint64_t lo;
1409+ uint64_t hi;
1410+ } bits;
1411+ } input_bits, roundtrip_bits, canonical_nan_bits, canonical_neg_nan_bits;
1412+
1413+ input_bits.q = x->sleef_value ;
1414+ roundtrip_bits.q = roundtrip.sleef_value ;
1415+ canonical_nan_bits.q = QUAD_PRECISION_NAN ;
1416+ canonical_neg_nan_bits.q = QUAD_PRECISION_NEG_NAN ;
1417+
1418+ int input_signbit = quad_signbit (&x->sleef_value );
1419+ int roundtrip_signbit = quad_signbit (&roundtrip.sleef_value );
1420+ int input_isnan = Sleef_iunordq1 (x->sleef_value , x->sleef_value );
1421+ int roundtrip_isnan = Sleef_iunordq1 (roundtrip.sleef_value , roundtrip.sleef_value );
1422+ int both_unord = Sleef_iunordq1 (x->sleef_value , roundtrip.sleef_value );
1423+ int are_equal = Sleef_icmpeqq1 (x->sleef_value , roundtrip.sleef_value );
1424+ bool is_sign_preserved = (input_signbit == roundtrip_signbit);
1425+
1426+ if (input_isnan || roundtrip_isnan) {
1427+ fprintf (stderr, " \n === DEBUG: NaN detected in same_value_check ===\n " );
1428+ fprintf (stderr, " Input bits: hi=0x%016llx lo=0x%016llx\n " ,
1429+ (unsigned long long )input_bits.bits .hi ,
1430+ (unsigned long long )input_bits.bits .lo );
1431+ fprintf (stderr, " Roundtrip bits: hi=0x%016llx lo=0x%016llx\n " ,
1432+ (unsigned long long )roundtrip_bits.bits .hi ,
1433+ (unsigned long long )roundtrip_bits.bits .lo );
1434+ fprintf (stderr, " QUAD_PRECISION_NAN: hi=0x%016llx lo=0x%016llx\n " ,
1435+ (unsigned long long )canonical_nan_bits.bits .hi ,
1436+ (unsigned long long )canonical_nan_bits.bits .lo );
1437+ fprintf (stderr, " QUAD_PREC_NEG_NAN: hi=0x%016llx lo=0x%016llx\n " ,
1438+ (unsigned long long )canonical_neg_nan_bits.bits .hi ,
1439+ (unsigned long long )canonical_neg_nan_bits.bits .lo );
1440+ fprintf (stderr, " input_signbit=%d, roundtrip_signbit=%d\n " ,
1441+ input_signbit, roundtrip_signbit);
1442+ fprintf (stderr, " input_isnan=%d, roundtrip_isnan=%d\n " ,
1443+ input_isnan, roundtrip_isnan);
1444+ fprintf (stderr, " both_unord=%d, are_equal=%d, is_sign_preserved=%d\n " ,
1445+ both_unord, are_equal, is_sign_preserved ? 1 : 0 );
1446+
1447+ // Also debug the intermediate value
1448+ fprintf (stderr, " Intermediate value y (as double): %.17g\n " , (double )(*y));
1449+ fprintf (stderr, " std::signbit(y): %d\n " , std::signbit ((double )(*y)));
1450+ fprintf (stderr, " std::isnan(y): %d\n " , std::isnan ((double )(*y)));
1451+ fprintf (stderr, " === END DEBUG ===\n\n " );
1452+ fflush (stderr);
1453+ }
1454+
1455+ // DONE
1456+
1457+ // bool is_sign_preserved = (quad_signbit(&x->sleef_value) == quad_signbit(&roundtrip.sleef_value));
13941458 // check if input is NaN and roundtrip is NaN with same sign
13951459 if (quad_isnan (&x->sleef_value ) && quad_isnan (&roundtrip.sleef_value ) && is_sign_preserved)
13961460 return 1 ;
0 commit comments