summaryrefslogtreecommitdiff
path: root/ccan/order/order.c
blob: 863a8e16e1425803621ee0bc68e37ee376693f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* CC0 license (public domain) - see LICENSE file for details */

#include <ccan/order/order.h>

#define SCALAR_ORDER(_oname, _type)					\
	int _order_##_oname(const void *a,				\
			    const void *b,				\
			    void *ctx)					\
	{								\
		ptrdiff_t offset = ptr2int(ctx);			\
		const _type *aa = (const _type *)((char *)a + offset);	\
		const _type *bb = (const _type *)((char *)b + offset);	\
									\
		if (*aa < *bb) {					\
			return -1;					\
		} else if (*aa > *bb) {					\
			return 1;					\
		} else {						\
			assert(*aa == *bb);				\
			return 0;					\
		}							\
	}								\
	int order_##_oname(const _type *a,				\
			   const _type *b,				\
			   void *ctx)					\
	{								\
		return _order_##_oname(a, b, int2ptr(0));		\
	}								\
	int _order_##_oname##_reverse(const void *a,			\
				      const void *b,			\
				      void *ctx)			\
	{								\
		return -_order_##_oname(a, b, ctx);			\
	}								\
	int order_##_oname##_reverse(const _type *a,			\
				     const _type *b,			\
				     void *ctx)				\
	{								\
		return _order_##_oname##_reverse(a, b, int2ptr(0));	\
	}								\
	int order_##_oname##_noctx(const void *a,			\
				   const void *b)			\
	{								\
		return _order_##_oname(a, b, int2ptr(0));		\
	}								\
	int order_##_oname##_reverse_noctx(const void *a,		\
					   const void *b)		\
	{								\
		return _order_##_oname##_reverse(a, b, int2ptr(0));	\
	}

SCALAR_ORDER(s8, int8_t)
SCALAR_ORDER(s16, int16_t)
SCALAR_ORDER(s32, int32_t)
SCALAR_ORDER(s64, int64_t)

SCALAR_ORDER(u8, uint8_t)
SCALAR_ORDER(u16, uint16_t)
SCALAR_ORDER(u32, uint32_t)
SCALAR_ORDER(u64, uint64_t)

SCALAR_ORDER(int, int)
SCALAR_ORDER(uint, unsigned int)
SCALAR_ORDER(long, long)
SCALAR_ORDER(ulong, unsigned long)
SCALAR_ORDER(size, size_t)
SCALAR_ORDER(ptrdiff, ptrdiff_t)

SCALAR_ORDER(float, float)
SCALAR_ORDER(double, double)