Skip to content

Conversation

@SwayamInSync
Copy link
Member

closes #46

Copilot Summary

This pull request adds support for sorting and argsort operations for the custom QuadPrecDType (quad-precision floating point) in both the SLEEF and long double backends. It introduces a new compare function at the C level, registers it with NumPy's dtype slots, and provides comprehensive tests to ensure correct sorting behavior, including handling of special values like NaN and Inf.

Sorting support for QuadPrecDType:

  • Added a quadprec_compare function in dtype.c that implements NumPy's compare function, supporting both SLEEF and long double backends, and ensuring NaN values are sorted to the end.
  • Registered the new compare function in the QuadPrecDType_Slots array so that NumPy's sort and argsort operations can use it.

Testing for sorting and argsort:

  • Added a new TestSortingOperations test suite in test_quaddtype.py to verify sorting and argsort for various input types, including integers, floats, negative numbers, empty arrays, and arrays with special values (NaN, Inf).
  • Included parameterized tests to check sort stability and correctness across different sorting algorithms (quicksort, mergesort, heapsort, stable) for both backends.

@ngoldbaum
Copy link
Member

@MaanasArora - any chance you can give this PR a review?

Copy link

@MaanasArora MaanasArora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SwayamInSync, @ngoldbaum, given we use the older ArrFunc API, this looks good to me at least from the NumPy side, just some nits! But do note there is a new DType sort_compare API in numpy/numpy#30415, if that goes in this should be refactored because that registers the new-style array methods instead. But that shouldn't be too hard so I don't think we need to hold this up!

src/csrc/dtype.c Outdated
return 0;
}
if (a_is_nan) {
return 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add /* NaN goes to the end */ here too?

src/csrc/dtype.c Outdated
long double val_b = *(long double *)b;

// NaN is considered greater than all other values for sorting
int a_is_nan = (val_a != val_a);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this safe under all compilers? Is it better to use isnan? Not very familiar with long doubles in C sorry!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its portable (IEEE nan property) but I agree its a bad readability, will be better to do isnan

{NPY_DT_getitem, &quadprec_getitem},
{NPY_DT_default_descr, &quadprec_default_descr},
{NPY_DT_get_constant, &quadprec_get_constant},
{NPY_DT_PyArray_ArrFuncs_compare, &quadprec_compare},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, as in the main comment this will be set for deprecation soon, technically is already an older version of the API.

@SwayamInSync
Copy link
Member Author

SwayamInSync commented Jan 16, 2026

But do note there is a new DType sort_compare API in numpy/numpy#30415, if that goes in this should be refactored because that registers the new-style array methods instead.

I see so then this old slot will be removed? or we keep both for legacy support?

@MaanasArora
Copy link

The old slot won't be removed anytime soon but the new slot is highly recommended, so using it here as soon as it is ready could help encourage dtype authors IMO, and allows descending support too. The ArrayMethod API already allows registering full sort-loops, this is just a missing piece to allow registering sorts just using the compare function.

value_str = mp.nstr(mp.mpf(str(value)), 33)
expected_str = mp.nstr(mp_value, 33)
assert value_str == expected_str, f"QuadPrecision({val}) ** {pow} = {value_str}, expected {expected_str}"
class TestSortingOperations:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a newline above

Copy link
Contributor

@juntyr juntyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison implementation looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sort fails from lack of comparison function

4 participants