]> git.ozlabs.org Git - ccan/blob - ccan/order/order.c
order: Scalar comparison functions
[ccan] / ccan / order / order.c
1 /* CC0 license (public domain) - see LICENSE file for details */
2
3 #include <ccan/order/order.h>
4
5 #define SCALAR_ORDER(_oname, _type)                                     \
6         int _order_##_oname(const void *a,                              \
7                             const void *b,                              \
8                             void *ctx)                                  \
9         {                                                               \
10                 ptrdiff_t offset = ptr2int(ctx);                        \
11                 const _type *aa = (const _type *)((char *)a + offset);  \
12                 const _type *bb = (const _type *)((char *)b + offset);  \
13                                                                         \
14                 if (*aa < *bb) {                                        \
15                         return -1;                                      \
16                 } else if (*aa > *bb) {                                 \
17                         return 1;                                       \
18                 } else {                                                \
19                         assert(*aa == *bb);                             \
20                         return 0;                                       \
21                 }                                                       \
22         }                                                               \
23         int order_##_oname(const _type *a,                              \
24                            const _type *b,                              \
25                            void *ctx)                                   \
26         {                                                               \
27                 return _order_##_oname(a, b, int2ptr(0));               \
28         }                                                               \
29         int _order_##_oname##_reverse(const void *a,                    \
30                                       const void *b,                    \
31                                       void *ctx)                        \
32         {                                                               \
33                 return -_order_##_oname(a, b, ctx);                     \
34         }                                                               \
35         int order_##_oname##_reverse(const _type *a,                    \
36                                      const _type *b,                    \
37                                      void *ctx)                         \
38         {                                                               \
39                 return _order_##_oname##_reverse(a, b, int2ptr(0));     \
40         }                                                               \
41         int order_##_oname##_noctx(const void *a,                       \
42                                    const void *b)                       \
43         {                                                               \
44                 return _order_##_oname(a, b, int2ptr(0));               \
45         }                                                               \
46         int order_##_oname##_reverse_noctx(const void *a,               \
47                                            const void *b)               \
48         {                                                               \
49                 return _order_##_oname##_reverse(a, b, int2ptr(0));     \
50         }
51
52 SCALAR_ORDER(s8, int8_t)
53 SCALAR_ORDER(s16, int16_t)
54 SCALAR_ORDER(s32, int32_t)
55 SCALAR_ORDER(s64, int64_t)
56
57 SCALAR_ORDER(u8, uint8_t)
58 SCALAR_ORDER(u16, uint16_t)
59 SCALAR_ORDER(u32, uint32_t)
60 SCALAR_ORDER(u64, uint64_t)
61
62 SCALAR_ORDER(int, int)
63 SCALAR_ORDER(uint, unsigned int)
64 SCALAR_ORDER(long, long)
65 SCALAR_ORDER(ulong, unsigned long)
66 SCALAR_ORDER(size, size_t)
67 SCALAR_ORDER(ptrdiff, ptrdiff_t)
68
69 SCALAR_ORDER(float, float)
70 SCALAR_ORDER(double, double)