| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/compare.h" | ||
| 30 | |||
| 31 | #include <math.h> | ||
| 32 | |||
| 33 | 328958 | int cx_vcmp_int(int a, int b) { | |
| 34 |
2/2✓ Branch 0 taken 4221 times.
✓ Branch 1 taken 324737 times.
|
328958 | if (a == b) { |
| 35 | 4221 | return 0; | |
| 36 | } else { | ||
| 37 |
2/2✓ Branch 0 taken 44752 times.
✓ Branch 1 taken 279985 times.
|
324737 | return a < b ? -1 : 1; |
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 328958 | int cx_cmp_int(const void *i1, const void *i2) { | |
| 42 | 328958 | int a = *((const int *) i1); | |
| 43 | 328958 | int b = *((const int *) i2); | |
| 44 | 328958 | return cx_vcmp_int(a, b); | |
| 45 | } | ||
| 46 | |||
| 47 | 8 | int cx_vcmp_longint(long int a, long int b) { | |
| 48 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (a == b) { |
| 49 | 2 | return 0; | |
| 50 | } else { | ||
| 51 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | 8 | int cx_cmp_longint(const void *i1, const void *i2) { | |
| 56 | 8 | long int a = *((const long int *) i1); | |
| 57 | 8 | long int b = *((const long int *) i2); | |
| 58 | 8 | return cx_vcmp_longint(a, b); | |
| 59 | } | ||
| 60 | |||
| 61 | 8 | int cx_vcmp_longlong(long long a, long long b) { | |
| 62 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (a == b) { |
| 63 | 2 | return 0; | |
| 64 | } else { | ||
| 65 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 8 | int cx_cmp_longlong(const void *i1, const void *i2) { | |
| 70 | 8 | long long a = *((const long long *) i1); | |
| 71 | 8 | long long b = *((const long long *) i2); | |
| 72 | 8 | return cx_vcmp_longlong(a, b); | |
| 73 | } | ||
| 74 | |||
| 75 | 8 | int cx_vcmp_int16(int16_t a, int16_t b) { | |
| 76 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (a == b) { |
| 77 | 2 | return 0; | |
| 78 | } else { | ||
| 79 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 8 | int cx_cmp_int16(const void *i1, const void *i2) { | |
| 84 | 8 | int16_t a = *((const int16_t *) i1); | |
| 85 | 8 | int16_t b = *((const int16_t *) i2); | |
| 86 | 8 | return cx_vcmp_int16(a, b); | |
| 87 | } | ||
| 88 | |||
| 89 | 8 | int cx_vcmp_int32(int32_t a, int32_t b) { | |
| 90 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (a == b) { |
| 91 | 2 | return 0; | |
| 92 | } else { | ||
| 93 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | 8 | int cx_cmp_int32(const void *i1, const void *i2) { | |
| 98 | 8 | int32_t a = *((const int32_t *) i1); | |
| 99 | 8 | int32_t b = *((const int32_t *) i2); | |
| 100 | 8 | return cx_vcmp_int32(a, b); | |
| 101 | } | ||
| 102 | |||
| 103 | 8 | int cx_vcmp_int64(int64_t a, int64_t b) { | |
| 104 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (a == b) { |
| 105 | 2 | return 0; | |
| 106 | } else { | ||
| 107 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | 8 | int cx_cmp_int64(const void *i1, const void *i2) { | |
| 112 | 8 | int64_t a = *((const int64_t *) i1); | |
| 113 | 8 | int64_t b = *((const int64_t *) i2); | |
| 114 | 8 | return cx_vcmp_int64(a, b); | |
| 115 | } | ||
| 116 | |||
| 117 | 6 | int cx_vcmp_uint(unsigned int a, unsigned int b) { | |
| 118 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 119 | 2 | return 0; | |
| 120 | } else { | ||
| 121 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | 6 | int cx_cmp_uint(const void *i1, const void *i2) { | |
| 126 | 6 | unsigned int a = *((const unsigned int *) i1); | |
| 127 | 6 | unsigned int b = *((const unsigned int *) i2); | |
| 128 | 6 | return cx_vcmp_uint(a, b); | |
| 129 | } | ||
| 130 | |||
| 131 | 6 | int cx_vcmp_ulongint(unsigned long int a, unsigned long int b) { | |
| 132 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 133 | 2 | return 0; | |
| 134 | } else { | ||
| 135 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | 6 | int cx_cmp_ulongint(const void *i1, const void *i2) { | |
| 140 | 6 | unsigned long int a = *((const unsigned long int *) i1); | |
| 141 | 6 | unsigned long int b = *((const unsigned long int *) i2); | |
| 142 | 6 | return cx_vcmp_ulongint(a, b); | |
| 143 | } | ||
| 144 | |||
| 145 | 6 | int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b) { | |
| 146 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 147 | 2 | return 0; | |
| 148 | } else { | ||
| 149 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | 6 | int cx_cmp_ulonglong(const void *i1, const void *i2) { | |
| 154 | 6 | unsigned long long a = *((const unsigned long long *) i1); | |
| 155 | 6 | unsigned long long b = *((const unsigned long long *) i2); | |
| 156 | 6 | return cx_vcmp_ulonglong(a, b); | |
| 157 | } | ||
| 158 | |||
| 159 | 6 | int cx_vcmp_uint16(uint16_t a, uint16_t b) { | |
| 160 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 161 | 2 | return 0; | |
| 162 | } else { | ||
| 163 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | 6 | int cx_cmp_uint16(const void *i1, const void *i2) { | |
| 168 | 6 | uint16_t a = *((const uint16_t *) i1); | |
| 169 | 6 | uint16_t b = *((const uint16_t *) i2); | |
| 170 | 6 | return cx_vcmp_uint16(a, b); | |
| 171 | } | ||
| 172 | |||
| 173 | 6 | int cx_vcmp_uint32(uint32_t a, uint32_t b) { | |
| 174 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 175 | 2 | return 0; | |
| 176 | } else { | ||
| 177 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | 6 | int cx_cmp_uint32(const void *i1, const void *i2) { | |
| 182 | 6 | uint32_t a = *((const uint32_t *) i1); | |
| 183 | 6 | uint32_t b = *((const uint32_t *) i2); | |
| 184 | 6 | return cx_vcmp_uint32(a, b); | |
| 185 | } | ||
| 186 | |||
| 187 | 6 | int cx_vcmp_uint64(uint64_t a, uint64_t b) { | |
| 188 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (a == b) { |
| 189 | 2 | return 0; | |
| 190 | } else { | ||
| 191 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 6 | int cx_cmp_uint64(const void *i1, const void *i2) { | |
| 196 | 6 | uint64_t a = *((const uint64_t *) i1); | |
| 197 | 6 | uint64_t b = *((const uint64_t *) i2); | |
| 198 | 6 | return cx_vcmp_uint64(a, b); | |
| 199 | } | ||
| 200 | |||
| 201 | 18 | int cx_vcmp_float(float a, float b) { | |
| 202 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | if (fabsf(a - b) < 1e-6f) { |
| 203 | 12 | return 0; | |
| 204 | } else { | ||
| 205 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | 8 | int cx_cmp_float(const void *f1, const void *f2) { | |
| 210 | 8 | float a = *((const float *) f1); | |
| 211 | 8 | float b = *((const float *) f2); | |
| 212 | 8 | return cx_vcmp_float(a, b); | |
| 213 | } | ||
| 214 | |||
| 215 | 21 | int cx_vcmp_double(double a, double b) { | |
| 216 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
|
21 | if (fabs(a - b) < 1e-14) { |
| 217 | 15 | return 0; | |
| 218 | } else { | ||
| 219 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | 8 | int cx_cmp_double( | |
| 224 | const void *d1, | ||
| 225 | const void *d2 | ||
| 226 | ) { | ||
| 227 | 8 | double a = *((const double *) d1); | |
| 228 | 8 | double b = *((const double *) d2); | |
| 229 | 8 | return cx_vcmp_double(a, b); | |
| 230 | } | ||
| 231 | |||
| 232 | 8 | int cx_vcmp_intptr(intptr_t p1, intptr_t p2) { | |
| 233 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (p1 == p2) { |
| 234 | 2 | return 0; | |
| 235 | } else { | ||
| 236 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | return p1 < p2 ? -1 : 1; |
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | 8 | int cx_cmp_intptr( | |
| 241 | const void *ptr1, | ||
| 242 | const void *ptr2 | ||
| 243 | ) { | ||
| 244 | 8 | intptr_t p1 = *(const intptr_t *) ptr1; | |
| 245 | 8 | intptr_t p2 = *(const intptr_t *) ptr2; | |
| 246 | 8 | return cx_vcmp_intptr(p1, p2); | |
| 247 | } | ||
| 248 | |||
| 249 | 6 | int cx_vcmp_uintptr(uintptr_t p1, uintptr_t p2) { | |
| 250 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (p1 == p2) { |
| 251 | 2 | return 0; | |
| 252 | } else { | ||
| 253 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return p1 < p2 ? -1 : 1; |
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | 6 | int cx_cmp_uintptr( | |
| 258 | const void *ptr1, | ||
| 259 | const void *ptr2 | ||
| 260 | ) { | ||
| 261 | 6 | uintptr_t p1 = *(const uintptr_t *) ptr1; | |
| 262 | 6 | uintptr_t p2 = *(const uintptr_t *) ptr2; | |
| 263 | 6 | return cx_vcmp_uintptr(p1, p2); | |
| 264 | } | ||
| 265 | |||
| 266 | 6 | int cx_cmp_ptr( | |
| 267 | const void *ptr1, | ||
| 268 | const void *ptr2 | ||
| 269 | ) { | ||
| 270 | 6 | uintptr_t p1 = (uintptr_t) ptr1; | |
| 271 | 6 | uintptr_t p2 = (uintptr_t) ptr2; | |
| 272 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (p1 == p2) { |
| 273 | 2 | return 0; | |
| 274 | } else { | ||
| 275 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return p1 < p2 ? -1 : 1; |
| 276 | } | ||
| 277 | } | ||
| 278 |