Sat, 28 Dec 2024 17:31:28 +0100
add cx_vcmp_* family of functions
fixes #538
CHANGELOG | file | annotate | diff | comparison | revisions | |
src/compare.c | file | annotate | diff | comparison | revisions | |
src/cx/compare.h | file | annotate | diff | comparison | revisions |
--- a/CHANGELOG Sat Dec 28 15:06:15 2024 +0100 +++ b/CHANGELOG Sat Dec 28 17:31:28 2024 +0100 @@ -13,6 +13,7 @@ * adds cxBufferAppend() * adds CX_BUFFER_COPY_ON_WRITE and CX_BUFFER_COPY_ON_EXTEND flags * adds cx_cmp_ptr() + * adds cx_vcmp_* family of functions * adds cx_sprintf() and several more variants * adds runtime constants to read out the actual SBO sizes * adds improved version of UCX 2 Test framework (now a self-contained header)
--- a/src/compare.c Sat Dec 28 15:06:15 2024 +0100 +++ b/src/compare.c Sat Dec 28 17:31:28 2024 +0100 @@ -30,9 +30,21 @@ #include <math.h> +int cx_vcmp_int(int a, int b) { + if (a == b) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + int cx_cmp_int(const void *i1, const void *i2) { int a = *((const int *) i1); int b = *((const int *) i2); + return cx_vcmp_int(a, b); +} + +int cx_vcmp_longint(long int a, long int b) { if (a == b) { return 0; } else { @@ -43,6 +55,10 @@ int cx_cmp_longint(const void *i1, const void *i2) { long int a = *((const long int *) i1); long int b = *((const long int *) i2); + return cx_vcmp_longint(a, b); +} + +int cx_vcmp_longlong(long long a, long long b) { if (a == b) { return 0; } else { @@ -53,6 +69,10 @@ int cx_cmp_longlong(const void *i1, const void *i2) { long long a = *((const long long *) i1); long long b = *((const long long *) i2); + return cx_vcmp_longlong(a, b); +} + +int cx_vcmp_int16(int16_t a, int16_t b) { if (a == b) { return 0; } else { @@ -63,6 +83,10 @@ int cx_cmp_int16(const void *i1, const void *i2) { int16_t a = *((const int16_t *) i1); int16_t b = *((const int16_t *) i2); + return cx_vcmp_int16(a, b); +} + +int cx_vcmp_int32(int32_t a, int32_t b) { if (a == b) { return 0; } else { @@ -73,6 +97,10 @@ int cx_cmp_int32(const void *i1, const void *i2) { int32_t a = *((const int32_t *) i1); int32_t b = *((const int32_t *) i2); + return cx_vcmp_int32(a, b); +} + +int cx_vcmp_int64(int64_t a, int64_t b) { if (a == b) { return 0; } else { @@ -83,6 +111,10 @@ int cx_cmp_int64(const void *i1, const void *i2) { int64_t a = *((const int64_t *) i1); int64_t b = *((const int64_t *) i2); + return cx_vcmp_int64(a, b); +} + +int cx_vcmp_uint(unsigned int a, unsigned int b) { if (a == b) { return 0; } else { @@ -93,6 +125,10 @@ int cx_cmp_uint(const void *i1, const void *i2) { unsigned int a = *((const unsigned int *) i1); unsigned int b = *((const unsigned int *) i2); + return cx_vcmp_uint(a, b); +} + +int cx_vcmp_ulongint(unsigned long int a, unsigned long int b) { if (a == b) { return 0; } else { @@ -103,6 +139,10 @@ int cx_cmp_ulongint(const void *i1, const void *i2) { unsigned long int a = *((const unsigned long int *) i1); unsigned long int b = *((const unsigned long int *) i2); + return cx_vcmp_ulongint(a, b); +} + +int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b) { if (a == b) { return 0; } else { @@ -113,6 +153,10 @@ int cx_cmp_ulonglong(const void *i1, const void *i2) { unsigned long long a = *((const unsigned long long *) i1); unsigned long long b = *((const unsigned long long *) i2); + return cx_vcmp_ulonglong(a, b); +} + +int cx_vcmp_uint16(uint16_t a, uint16_t b) { if (a == b) { return 0; } else { @@ -123,6 +167,10 @@ int cx_cmp_uint16(const void *i1, const void *i2) { uint16_t a = *((const uint16_t *) i1); uint16_t b = *((const uint16_t *) i2); + return cx_vcmp_uint16(a, b); +} + +int cx_vcmp_uint32(uint32_t a, uint32_t b) { if (a == b) { return 0; } else { @@ -133,6 +181,10 @@ int cx_cmp_uint32(const void *i1, const void *i2) { uint32_t a = *((const uint32_t *) i1); uint32_t b = *((const uint32_t *) i2); + return cx_vcmp_uint32(a, b); +} + +int cx_vcmp_uint64(uint64_t a, uint64_t b) { if (a == b) { return 0; } else { @@ -143,7 +195,11 @@ int cx_cmp_uint64(const void *i1, const void *i2) { uint64_t a = *((const uint64_t *) i1); uint64_t b = *((const uint64_t *) i2); - if (a == b) { + return cx_vcmp_uint64(a, b); +} + +int cx_vcmp_float(float a, float b) { + if (fabsf(a - b) < 1e-6f) { return 0; } else { return a < b ? -1 : 1; @@ -153,7 +209,11 @@ int cx_cmp_float(const void *f1, const void *f2) { float a = *((const float *) f1); float b = *((const float *) f2); - if (fabsf(a - b) < 1e-6f) { + return cx_vcmp_float(a, b); +} + +int cx_vcmp_double(double a, double b) { + if (fabs(a - b) < 1e-14) { return 0; } else { return a < b ? -1 : 1; @@ -166,10 +226,14 @@ ) { double a = *((const double *) d1); double b = *((const double *) d2); - if (fabs(a - b) < 1e-14) { + return cx_vcmp_double(a, b); +} + +int cx_vcmp_intptr(intptr_t p1, intptr_t p2) { + if (p1 == p2) { return 0; } else { - return a < b ? -1 : 1; + return p1 < p2 ? -1 : 1; } } @@ -179,6 +243,10 @@ ) { intptr_t p1 = *(const intptr_t *) ptr1; intptr_t p2 = *(const intptr_t *) ptr2; + return cx_vcmp_intptr(p1, p2); +} + +int cx_vcmp_uintptr(uintptr_t p1, uintptr_t p2) { if (p1 == p2) { return 0; } else { @@ -192,11 +260,7 @@ ) { uintptr_t p1 = *(const uintptr_t *) ptr1; uintptr_t p2 = *(const uintptr_t *) ptr2; - if (p1 == p2) { - return 0; - } else { - return p1 < p2 ? -1 : 1; - } + return cx_vcmp_uintptr(p1, p2); } int cx_cmp_ptr(
--- a/src/cx/compare.h Sat Dec 28 15:06:15 2024 +0100 +++ b/src/cx/compare.h Sat Dec 28 17:31:28 2024 +0100 @@ -43,7 +43,16 @@ #endif /** - * A comparator function comparing two collection elements. + * A comparator function comparing two arbitrary values. + * + * All functions from compare.h with the cx_cmp prefix are + * compatible with this signature and can be used as + * compare function for collections, or other implementations + * that need to be type-agnostic. + * + * For simple comparisons the cx_vcmp family of functions + * can be used, but they are NOT compatible with this function + * pointer. */ cx_attr_nonnull cx_attr_nodiscard @@ -65,6 +74,17 @@ int cx_cmp_int(const void *i1, const void *i2); /** + * Compares two ints. + * + * @param i1 integer one + * @param i2 integer two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_int(int i1, int i2); + +/** * Compares two integers of type long int. * * @param i1 pointer to long integer one @@ -77,6 +97,17 @@ int cx_cmp_longint(const void *i1, const void *i2); /** + * Compares two long ints. + * + * @param i1 long integer one + * @param i2 long integer two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_longint(long int i1, long int i2); + +/** * Compares two integers of type long long. * * @param i1 pointer to long long one @@ -89,6 +120,17 @@ int cx_cmp_longlong(const void *i1, const void *i2); /** + * Compares twolong long ints. + * + * @param i1 long long int one + * @param i2 long long int two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_longlong(long long int i1, long long int i2); + +/** * Compares two integers of type int16_t. * * @param i1 pointer to int16_t one @@ -101,6 +143,17 @@ int cx_cmp_int16(const void *i1, const void *i2); /** + * Compares two integers of type int16_t. + * + * @param i1 int16_t one + * @param i2 int16_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_int16(int16_t i1, int16_t i2); + +/** * Compares two integers of type int32_t. * * @param i1 pointer to int32_t one @@ -113,6 +166,17 @@ int cx_cmp_int32(const void *i1, const void *i2); /** + * Compares two integers of type int32_t. + * + * @param i1 int32_t one + * @param i2 int32_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_int32(int32_t i1, int32_t i2); + +/** * Compares two integers of type int64_t. * * @param i1 pointer to int64_t one @@ -125,6 +189,17 @@ int cx_cmp_int64(const void *i1, const void *i2); /** + * Compares two integers of type int64_t. + * + * @param i1 int64_t one + * @param i2 int64_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_int64(int64_t i1, int64_t i2); + +/** * Compares two integers of type unsigned int. * * @param i1 pointer to unsigned integer one @@ -137,6 +212,17 @@ int cx_cmp_uint(const void *i1, const void *i2); /** + * Compares two unsigned ints. + * + * @param i1 unsigned integer one + * @param i2 unsigned integer two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_uint(unsigned int i1, unsigned int i2); + +/** * Compares two integers of type unsigned long int. * * @param i1 pointer to unsigned long integer one @@ -149,6 +235,17 @@ int cx_cmp_ulongint(const void *i1, const void *i2); /** + * Compares two unsigned long ints. + * + * @param i1 unsigned long integer one + * @param i2 unsigned long integer two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_ulongint(unsigned long int i1, unsigned long int i2); + +/** * Compares two integers of type unsigned long long. * * @param i1 pointer to unsigned long long one @@ -161,6 +258,17 @@ int cx_cmp_ulonglong(const void *i1, const void *i2); /** + * Compares two unsigned long long ints. + * + * @param i1 unsigned long long one + * @param i2 unsigned long long two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_ulonglong(unsigned long long int i1, unsigned long long int i2); + +/** * Compares two integers of type uint16_t. * * @param i1 pointer to uint16_t one @@ -173,6 +281,17 @@ int cx_cmp_uint16(const void *i1, const void *i2); /** + * Compares two integers of type uint16_t. + * + * @param i1 uint16_t one + * @param i2 uint16_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_uint16(uint16_t i1, uint16_t i2); + +/** * Compares two integers of type uint32_t. * * @param i1 pointer to uint32_t one @@ -185,6 +304,17 @@ int cx_cmp_uint32(const void *i1, const void *i2); /** + * Compares two integers of type uint32_t. + * + * @param i1 uint32_t one + * @param i2 uint32_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_uint32(uint32_t i1, uint32_t i2); + +/** * Compares two integers of type uint64_t. * * @param i1 pointer to uint64_t one @@ -197,6 +327,17 @@ int cx_cmp_uint64(const void *i1, const void *i2); /** + * Compares two integers of type uint64_t. + * + * @param i1 uint64_t one + * @param i2 uint64_t two + * @return -1, if i1 is less than i2, 0 if both are equal, + * 1 if i1 is greater than i2 + */ +cx_attr_nodiscard +int cx_vcmp_uint64(uint64_t i1, uint64_t i2); + +/** * Compares two real numbers of type float with precision 1e-6f. * * @param f1 pointer to float one @@ -209,6 +350,17 @@ int cx_cmp_float(const void *f1, const void *f2); /** + * Compares two real numbers of type float with precision 1e-6f. + * + * @param f1 float one + * @param f2 float two + * @return -1, if f1 is less than f2, 0 if both are equal, + * 1 if f1 is greater than f2 + */ +cx_attr_nodiscard +int cx_vcmp_float(float f1, float f2); + +/** * Compares two real numbers of type double with precision 1e-14. * * @param d1 pointer to double one @@ -221,6 +373,17 @@ int cx_cmp_double(const void *d1, const void *d2); /** + * Convenience function + * + * @param d1 double one + * @param d2 double two + * @return -1, if d1 is less than d2, 0 if both are equal, + * 1 if d1 is greater than d2 + */ +cx_attr_nodiscard +int cx_vcmp_double(double d1, double d2); + +/** * Compares the integer representation of two pointers. * * @param ptr1 pointer to pointer one (const intptr_t*) @@ -233,6 +396,17 @@ int cx_cmp_intptr(const void *ptr1, const void *ptr2); /** + * Compares the integer representation of two pointers. + * + * @param ptr1 pointer one + * @param ptr2 pointer two + * @return -1 if ptr1 is less than ptr2, 0 if both are equal, + * 1 if ptr1 is greater than ptr2 + */ +cx_attr_nodiscard +int cx_vcmp_intptr(intptr_t ptr1, intptr_t ptr2); + +/** * Compares the unsigned integer representation of two pointers. * * @param ptr1 pointer to pointer one (const uintptr_t*) @@ -245,6 +419,17 @@ int cx_cmp_uintptr(const void *ptr1, const void *ptr2); /** + * Compares the unsigned integer representation of two pointers. + * + * @param ptr1 pointer one + * @param ptr2 pointer two + * @return -1 if ptr1 is less than ptr2, 0 if both are equal, + * 1 if ptr1 is greater than ptr2 + */ +cx_attr_nodiscard +int cx_vcmp_uintptr(uintptr_t ptr1, uintptr_t ptr2); + +/** * Compares the pointers specified in the arguments without de-referencing. * * @param ptr1 pointer one