2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "cx/compare.h"
33 int cx_cmp_int(void const *i1, void const *i2) {
34 int a = *((const int*) i1);
35 int b = *((const int*) i2);
39 return a < b ? -1 : 1;
43 int cx_cmp_longint(void const *i1, void const *i2) {
44 long int a = *((const long int*) i1);
45 long int b = *((const long int*) i2);
49 return a < b ? -1 : 1;
53 int cx_cmp_longlong(void const *i1, void const *i2) {
54 long long a = *((const long long*) i1);
55 long long b = *((const long long*) i2);
59 return a < b ? -1 : 1;
63 int cx_cmp_int16(void const *i1, void const *i2) {
64 int16_t a = *((const int16_t*) i1);
65 int16_t b = *((const int16_t*) i2);
69 return a < b ? -1 : 1;
73 int cx_cmp_int32(void const *i1, void const *i2) {
74 int32_t a = *((const int32_t*) i1);
75 int32_t b = *((const int32_t*) i2);
79 return a < b ? -1 : 1;
83 int cx_cmp_int64(void const *i1, void const *i2) {
84 int64_t a = *((const int64_t*) i1);
85 int64_t b = *((const int64_t*) i2);
89 return a < b ? -1 : 1;
93 int cx_cmp_uint(void const *i1, void const *i2) {
94 unsigned int a = *((const unsigned int*) i1);
95 unsigned int b = *((const unsigned int*) i2);
99 return a < b ? -1 : 1;
103 int cx_cmp_ulongint(void const *i1, void const *i2) {
104 unsigned long int a = *((const unsigned long int*) i1);
105 unsigned long int b = *((const unsigned long int*) i2);
109 return a < b ? -1 : 1;
113 int cx_cmp_ulonglong(void const *i1, void const *i2) {
114 unsigned long long a = *((const unsigned long long*) i1);
115 unsigned long long b = *((const unsigned long long*) i2);
119 return a < b ? -1 : 1;
123 int cx_cmp_uint16(void const *i1, void const *i2) {
124 uint16_t a = *((const uint16_t*) i1);
125 uint16_t b = *((const uint16_t*) i2);
129 return a < b ? -1 : 1;
133 int cx_cmp_uint32(void const *i1, void const *i2) {
134 uint32_t a = *((const uint32_t*) i1);
135 uint32_t b = *((const uint32_t*) i2);
139 return a < b ? -1 : 1;
143 int cx_cmp_uint64(void const *i1, void const *i2) {
144 uint64_t a = *((const uint64_t*) i1);
145 uint64_t b = *((const uint64_t*) i2);
149 return a < b ? -1 : 1;
153 int cx_cmp_float(void const *f1, void const *f2) {
154 float a = *((const float*) f1);
155 float b = *((const float*) f2);
156 if (fabsf(a - b) < 1e-6f) {
159 return a < b ? -1 : 1;
167 double a = *((const double *) d1);
168 double b = *((const double *) d2);
169 if (fabs(a - b) < 1e-14) {
172 return a < b ? -1 : 1;
180 intptr_t p1 = *(const intptr_t *) ptr1;
181 intptr_t p2 = *(const intptr_t *) ptr2;
185 return p1 < p2 ? -1 : 1;
193 uintptr_t p1 = *(const uintptr_t *) ptr1;
194 uintptr_t p2 = *(const uintptr_t *) ptr2;
198 return p1 < p2 ? -1 : 1;