1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/compare.c Sat Nov 05 17:50:04 2022 +0100 1.3 @@ -0,0 +1,182 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "cx/compare.h" 1.33 + 1.34 +#include <stdint.h> 1.35 +#include <math.h> 1.36 + 1.37 +int cx_cmp_int(void const *i1, void const *i2) { 1.38 + int a = *((const int*) i1); 1.39 + int b = *((const int*) i2); 1.40 + if (a == b) { 1.41 + return 0; 1.42 + } else { 1.43 + return a < b ? -1 : 1; 1.44 + } 1.45 +} 1.46 + 1.47 +int cx_cmp_longint(void const *i1, void const *i2) { 1.48 + long int a = *((const long int*) i1); 1.49 + long int b = *((const long int*) i2); 1.50 + if (a == b) { 1.51 + return 0; 1.52 + } else { 1.53 + return a < b ? -1 : 1; 1.54 + } 1.55 +} 1.56 + 1.57 +int cx_cmp_longlong(void const *i1, void const *i2) { 1.58 + long long a = *((const long long*) i1); 1.59 + long long b = *((const long long*) i2); 1.60 + if (a == b) { 1.61 + return 0; 1.62 + } else { 1.63 + return a < b ? -1 : 1; 1.64 + } 1.65 +} 1.66 + 1.67 +int cx_cmp_int16(void const *i1, void const *i2) { 1.68 + int16_t a = *((const int16_t*) i1); 1.69 + int16_t b = *((const int16_t*) i2); 1.70 + if (a == b) { 1.71 + return 0; 1.72 + } else { 1.73 + return a < b ? -1 : 1; 1.74 + } 1.75 +} 1.76 + 1.77 +int cx_cmp_int32(void const *i1, void const *i2) { 1.78 + int32_t a = *((const int32_t*) i1); 1.79 + int32_t b = *((const int32_t*) i2); 1.80 + if (a == b) { 1.81 + return 0; 1.82 + } else { 1.83 + return a < b ? -1 : 1; 1.84 + } 1.85 +} 1.86 + 1.87 +int cx_cmp_int64(void const *i1, void const *i2) { 1.88 + int64_t a = *((const int64_t*) i1); 1.89 + int64_t b = *((const int64_t*) i2); 1.90 + if (a == b) { 1.91 + return 0; 1.92 + } else { 1.93 + return a < b ? -1 : 1; 1.94 + } 1.95 +} 1.96 + 1.97 +int cx_cmp_uint(void const *i1, void const *i2) { 1.98 + unsigned int a = *((const unsigned int*) i1); 1.99 + unsigned int b = *((const unsigned int*) i2); 1.100 + if (a == b) { 1.101 + return 0; 1.102 + } else { 1.103 + return a < b ? -1 : 1; 1.104 + } 1.105 +} 1.106 + 1.107 +int cx_cmp_ulongint(void const *i1, void const *i2) { 1.108 + unsigned long int a = *((const unsigned long int*) i1); 1.109 + unsigned long int b = *((const unsigned long int*) i2); 1.110 + if (a == b) { 1.111 + return 0; 1.112 + } else { 1.113 + return a < b ? -1 : 1; 1.114 + } 1.115 +} 1.116 + 1.117 +int cx_cmp_ulonglong(void const *i1, void const *i2) { 1.118 + unsigned long long a = *((const unsigned long long*) i1); 1.119 + unsigned long long b = *((const unsigned long long*) i2); 1.120 + if (a == b) { 1.121 + return 0; 1.122 + } else { 1.123 + return a < b ? -1 : 1; 1.124 + } 1.125 +} 1.126 + 1.127 +int cx_cmp_uint16(void const *i1, void const *i2) { 1.128 + uint16_t a = *((const uint16_t*) i1); 1.129 + uint16_t b = *((const uint16_t*) i2); 1.130 + if (a == b) { 1.131 + return 0; 1.132 + } else { 1.133 + return a < b ? -1 : 1; 1.134 + } 1.135 +} 1.136 + 1.137 +int cx_cmp_uint32(void const *i1, void const *i2) { 1.138 + uint32_t a = *((const uint32_t*) i1); 1.139 + uint32_t b = *((const uint32_t*) i2); 1.140 + if (a == b) { 1.141 + return 0; 1.142 + } else { 1.143 + return a < b ? -1 : 1; 1.144 + } 1.145 +} 1.146 + 1.147 +int cx_cmp_uint64(void const *i1, void const *i2) { 1.148 + uint64_t a = *((const uint64_t*) i1); 1.149 + uint64_t b = *((const uint64_t*) i2); 1.150 + if (a == b) { 1.151 + return 0; 1.152 + } else { 1.153 + return a < b ? -1 : 1; 1.154 + } 1.155 +} 1.156 + 1.157 +int cx_cmp_float(void const *f1, void const *f2) { 1.158 + float a = *((const float*) f1); 1.159 + float b = *((const float*) f2); 1.160 + if (fabsf(a - b) < 1e-6f) { 1.161 + return 0; 1.162 + } else { 1.163 + return a < b ? -1 : 1; 1.164 + } 1.165 +} 1.166 + 1.167 +int cx_cmp_double(void const *d1, void const *d2) { 1.168 + double a = *((const double*) d1); 1.169 + double b = *((const double*) d2); 1.170 + if (fabs(a - b) < 1e-14) { 1.171 + return 0; 1.172 + } else { 1.173 + return a < b ? -1 : 1; 1.174 + } 1.175 +} 1.176 + 1.177 +int cx_cmp_ptr(void const *ptr1, void const *ptr2) { 1.178 + const intptr_t p1 = (const intptr_t) ptr1; 1.179 + const intptr_t p2 = (const intptr_t) ptr2; 1.180 + if (p1 == p2) { 1.181 + return 0; 1.182 + } else { 1.183 + return p1 < p2 ? -1 : 1; 1.184 + } 1.185 +}