universe@601: /* universe@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@601: * universe@601: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@601: * universe@601: * Redistribution and use in source and binary forms, with or without universe@601: * modification, are permitted provided that the following conditions are met: universe@601: * universe@601: * 1. Redistributions of source code must retain the above copyright universe@601: * notice, this list of conditions and the following disclaimer. universe@601: * universe@601: * 2. Redistributions in binary form must reproduce the above copyright universe@601: * notice, this list of conditions and the following disclaimer in the universe@601: * documentation and/or other materials provided with the distribution. universe@601: * universe@601: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@601: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@601: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@601: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@601: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@601: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@601: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@601: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@601: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@601: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@601: * POSSIBILITY OF SUCH DAMAGE. universe@601: */ universe@601: universe@601: #include "cx/compare.h" universe@601: universe@601: #include universe@601: universe@601: int cx_cmp_int(void const *i1, void const *i2) { universe@678: int a = *((const int *) i1); universe@678: int b = *((const int *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_longint(void const *i1, void const *i2) { universe@678: long int a = *((const long int *) i1); universe@678: long int b = *((const long int *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_longlong(void const *i1, void const *i2) { universe@678: long long a = *((const long long *) i1); universe@678: long long b = *((const long long *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_int16(void const *i1, void const *i2) { universe@678: int16_t a = *((const int16_t *) i1); universe@678: int16_t b = *((const int16_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_int32(void const *i1, void const *i2) { universe@678: int32_t a = *((const int32_t *) i1); universe@678: int32_t b = *((const int32_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_int64(void const *i1, void const *i2) { universe@678: int64_t a = *((const int64_t *) i1); universe@678: int64_t b = *((const int64_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_uint(void const *i1, void const *i2) { universe@678: unsigned int a = *((const unsigned int *) i1); universe@678: unsigned int b = *((const unsigned int *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_ulongint(void const *i1, void const *i2) { universe@678: unsigned long int a = *((const unsigned long int *) i1); universe@678: unsigned long int b = *((const unsigned long int *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_ulonglong(void const *i1, void const *i2) { universe@678: unsigned long long a = *((const unsigned long long *) i1); universe@678: unsigned long long b = *((const unsigned long long *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_uint16(void const *i1, void const *i2) { universe@678: uint16_t a = *((const uint16_t *) i1); universe@678: uint16_t b = *((const uint16_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_uint32(void const *i1, void const *i2) { universe@678: uint32_t a = *((const uint32_t *) i1); universe@678: uint32_t b = *((const uint32_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_uint64(void const *i1, void const *i2) { universe@678: uint64_t a = *((const uint64_t *) i1); universe@678: uint64_t b = *((const uint64_t *) i2); universe@601: if (a == b) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@601: int cx_cmp_float(void const *f1, void const *f2) { universe@678: float a = *((const float *) f1); universe@678: float b = *((const float *) f2); universe@601: if (fabsf(a - b) < 1e-6f) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@631: int cx_cmp_double( universe@631: void const *d1, universe@631: void const *d2 universe@631: ) { universe@631: double a = *((const double *) d1); universe@631: double b = *((const double *) d2); universe@601: if (fabs(a - b) < 1e-14) { universe@601: return 0; universe@601: } else { universe@601: return a < b ? -1 : 1; universe@601: } universe@601: } universe@601: universe@631: int cx_cmp_intptr( universe@631: void const *ptr1, universe@631: void const *ptr2 universe@631: ) { universe@631: intptr_t p1 = *(const intptr_t *) ptr1; universe@631: intptr_t p2 = *(const intptr_t *) ptr2; universe@601: if (p1 == p2) { universe@601: return 0; universe@601: } else { universe@631: return p1 < p2 ? -1 : 1; universe@601: } universe@601: } universe@631: universe@631: int cx_cmp_uintptr( universe@631: void const *ptr1, universe@631: void const *ptr2 universe@631: ) { universe@631: uintptr_t p1 = *(const uintptr_t *) ptr1; universe@631: uintptr_t p2 = *(const uintptr_t *) ptr2; universe@631: if (p1 == p2) { universe@631: return 0; universe@631: } else { universe@631: return p1 < p2 ? -1 : 1; universe@631: } universe@631: } universe@631: universe@762: int cx_cmp_ptr( universe@762: void const *ptr1, universe@762: void const *ptr2 universe@762: ) { universe@762: uintptr_t p1 = (uintptr_t) ptr1; universe@762: uintptr_t p2 = (uintptr_t) ptr2; universe@762: if (p1 == p2) { universe@762: return 0; universe@762: } else { universe@762: return p1 < p2 ? -1 : 1; universe@762: } universe@762: }