xref: /haiku/headers/libs/zydis/Zycore/Comparison.h (revision 1003e004e6c97eb60657a98928dd334e141c59ee)
1*1003e004SJérôme Duval /***************************************************************************************************
2*1003e004SJérôme Duval 
3*1003e004SJérôme Duval   Zyan Core Library (Zycore-C)
4*1003e004SJérôme Duval 
5*1003e004SJérôme Duval   Original Author : Florian Bernd
6*1003e004SJérôme Duval 
7*1003e004SJérôme Duval  * Permission is hereby granted, free of charge, to any person obtaining a copy
8*1003e004SJérôme Duval  * of this software and associated documentation files (the "Software"), to deal
9*1003e004SJérôme Duval  * in the Software without restriction, including without limitation the rights
10*1003e004SJérôme Duval  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11*1003e004SJérôme Duval  * copies of the Software, and to permit persons to whom the Software is
12*1003e004SJérôme Duval  * furnished to do so, subject to the following conditions:
13*1003e004SJérôme Duval  *
14*1003e004SJérôme Duval  * The above copyright notice and this permission notice shall be included in all
15*1003e004SJérôme Duval  * copies or substantial portions of the Software.
16*1003e004SJérôme Duval  *
17*1003e004SJérôme Duval  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*1003e004SJérôme Duval  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*1003e004SJérôme Duval  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20*1003e004SJérôme Duval  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*1003e004SJérôme Duval  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*1003e004SJérôme Duval  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*1003e004SJérôme Duval  * SOFTWARE.
24*1003e004SJérôme Duval 
25*1003e004SJérôme Duval ***************************************************************************************************/
26*1003e004SJérôme Duval 
27*1003e004SJérôme Duval /**
28*1003e004SJérôme Duval  * @file
29*1003e004SJérôme Duval  * Defines prototypes of general-purpose comparison functions.
30*1003e004SJérôme Duval  */
31*1003e004SJérôme Duval 
32*1003e004SJérôme Duval #ifndef ZYCORE_COMPARISON_H
33*1003e004SJérôme Duval #define ZYCORE_COMPARISON_H
34*1003e004SJérôme Duval 
35*1003e004SJérôme Duval #include <Zycore/Defines.h>
36*1003e004SJérôme Duval #include <Zycore/Types.h>
37*1003e004SJérôme Duval 
38*1003e004SJérôme Duval #ifdef __cplusplus
39*1003e004SJérôme Duval extern "C" {
40*1003e004SJérôme Duval #endif
41*1003e004SJérôme Duval 
42*1003e004SJérôme Duval /* ============================================================================================== */
43*1003e004SJérôme Duval /* Enums and types                                                                                */
44*1003e004SJérôme Duval /* ============================================================================================== */
45*1003e004SJérôme Duval 
46*1003e004SJérôme Duval /**
47*1003e004SJérôme Duval  * Defines the `ZyanEqualityComparison` function prototype.
48*1003e004SJérôme Duval  *
49*1003e004SJérôme Duval  * @param   left    A pointer to the first element.
50*1003e004SJérôme Duval  * @param   right   A pointer to the second element.
51*1003e004SJérôme Duval  *
52*1003e004SJérôme Duval  * @return  This function should return `ZYAN_TRUE` if the `left` element equals the `right` one
53*1003e004SJérôme Duval  *          or `ZYAN_FALSE`, if not.
54*1003e004SJérôme Duval  */
55*1003e004SJérôme Duval typedef ZyanBool (*ZyanEqualityComparison)(const void* left, const void* right);
56*1003e004SJérôme Duval 
57*1003e004SJérôme Duval /**
58*1003e004SJérôme Duval  * Defines the `ZyanComparison` function prototype.
59*1003e004SJérôme Duval  *
60*1003e004SJérôme Duval  * @param   left    A pointer to the first element.
61*1003e004SJérôme Duval  * @param   right   A pointer to the second element.
62*1003e004SJérôme Duval  *
63*1003e004SJérôme Duval  * @return  This function should return values in the following range:
64*1003e004SJérôme Duval  *          `left == right -> result == 0`
65*1003e004SJérôme Duval  *          `left <  right -> result  < 0`
66*1003e004SJérôme Duval  *          `left >  right -> result  > 0`
67*1003e004SJérôme Duval  */
68*1003e004SJérôme Duval typedef ZyanI32 (*ZyanComparison)(const void* left, const void* right);
69*1003e004SJérôme Duval 
70*1003e004SJérôme Duval /* ============================================================================================== */
71*1003e004SJérôme Duval /* Macros                                                                                         */
72*1003e004SJérôme Duval /* ============================================================================================== */
73*1003e004SJérôme Duval 
74*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
75*1003e004SJérôme Duval /* Equality comparison functions                                                                  */
76*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
77*1003e004SJérôme Duval 
78*1003e004SJérôme Duval /**
79*1003e004SJérôme Duval  * Declares a generic equality comparison function for an integral data-type.
80*1003e004SJérôme Duval  *
81*1003e004SJérôme Duval  * @param   name    The name of the function.
82*1003e004SJérôme Duval  * @param   type    The name of the integral data-type.
83*1003e004SJérôme Duval  */
84*1003e004SJérôme Duval #define ZYAN_DECLARE_EQUALITY_COMPARISON(name, type) \
85*1003e004SJérôme Duval     ZyanBool name(const type* left, const type* right) \
86*1003e004SJérôme Duval     { \
87*1003e004SJérôme Duval         ZYAN_ASSERT(left); \
88*1003e004SJérôme Duval         ZYAN_ASSERT(right); \
89*1003e004SJérôme Duval         \
90*1003e004SJérôme Duval         return (*left == *right) ? ZYAN_TRUE : ZYAN_FALSE; \
91*1003e004SJérôme Duval     }
92*1003e004SJérôme Duval 
93*1003e004SJérôme Duval /**
94*1003e004SJérôme Duval  * Declares a generic equality comparison function that compares a single integral
95*1003e004SJérôme Duval  *          data-type field of a struct.
96*1003e004SJérôme Duval  *
97*1003e004SJérôme Duval  * @param   name        The name of the function.
98*1003e004SJérôme Duval  * @param   type        The name of the integral data-type.
99*1003e004SJérôme Duval  * @param   field_name  The name of the struct field.
100*1003e004SJérôme Duval  */
101*1003e004SJérôme Duval #define ZYAN_DECLARE_EQUALITY_COMPARISON_FOR_FIELD(name, type, field_name) \
102*1003e004SJérôme Duval     ZyanBool name(const type* left, const type* right) \
103*1003e004SJérôme Duval     { \
104*1003e004SJérôme Duval         ZYAN_ASSERT(left); \
105*1003e004SJérôme Duval         ZYAN_ASSERT(right); \
106*1003e004SJérôme Duval         \
107*1003e004SJérôme Duval         return (left->field_name == right->field_name) ? ZYAN_TRUE : ZYAN_FALSE; \
108*1003e004SJérôme Duval     }
109*1003e004SJérôme Duval 
110*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
111*1003e004SJérôme Duval /* Comparison functions                                                                           */
112*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
113*1003e004SJérôme Duval 
114*1003e004SJérôme Duval /**
115*1003e004SJérôme Duval  * Declares a generic comparison function for an integral data-type.
116*1003e004SJérôme Duval  *
117*1003e004SJérôme Duval  * @param   name    The name of the function.
118*1003e004SJérôme Duval  * @param   type    The name of the integral data-type.
119*1003e004SJérôme Duval  */
120*1003e004SJérôme Duval #define ZYAN_DECLARE_COMPARISON(name, type) \
121*1003e004SJérôme Duval     ZyanI32 name(const type* left, const type* right) \
122*1003e004SJérôme Duval     { \
123*1003e004SJérôme Duval         ZYAN_ASSERT(left); \
124*1003e004SJérôme Duval         ZYAN_ASSERT(right); \
125*1003e004SJérôme Duval         \
126*1003e004SJérôme Duval         if (*left < *right) \
127*1003e004SJérôme Duval         { \
128*1003e004SJérôme Duval             return -1; \
129*1003e004SJérôme Duval         } \
130*1003e004SJérôme Duval         if (*left > *right) \
131*1003e004SJérôme Duval         { \
132*1003e004SJérôme Duval             return  1; \
133*1003e004SJérôme Duval         } \
134*1003e004SJérôme Duval         return 0; \
135*1003e004SJérôme Duval     }
136*1003e004SJérôme Duval 
137*1003e004SJérôme Duval /**
138*1003e004SJérôme Duval  * Declares a generic comparison function that compares a single integral data-type field
139*1003e004SJérôme Duval  *          of a struct.
140*1003e004SJérôme Duval  *
141*1003e004SJérôme Duval  * @param   name        The name of the function.
142*1003e004SJérôme Duval  * @param   type        The name of the integral data-type.
143*1003e004SJérôme Duval  * @param   field_name  The name of the struct field.
144*1003e004SJérôme Duval  */
145*1003e004SJérôme Duval #define ZYAN_DECLARE_COMPARISON_FOR_FIELD(name, type, field_name) \
146*1003e004SJérôme Duval     ZyanI32 name(const type* left, const type* right) \
147*1003e004SJérôme Duval     { \
148*1003e004SJérôme Duval         ZYAN_ASSERT(left); \
149*1003e004SJérôme Duval         ZYAN_ASSERT(right); \
150*1003e004SJérôme Duval         \
151*1003e004SJérôme Duval         if (left->field_name < right->field_name) \
152*1003e004SJérôme Duval         { \
153*1003e004SJérôme Duval             return -1; \
154*1003e004SJérôme Duval         } \
155*1003e004SJérôme Duval         if (left->field_name > right->field_name) \
156*1003e004SJérôme Duval         { \
157*1003e004SJérôme Duval             return  1; \
158*1003e004SJérôme Duval         } \
159*1003e004SJérôme Duval         return 0; \
160*1003e004SJérôme Duval     }
161*1003e004SJérôme Duval 
162*1003e004SJérôme Duval  /* ---------------------------------------------------------------------------------------------- */
163*1003e004SJérôme Duval 
164*1003e004SJérôme Duval /* ============================================================================================== */
165*1003e004SJérôme Duval /* Exported functions                                                                             */
166*1003e004SJérôme Duval /* ============================================================================================== */
167*1003e004SJérôme Duval 
168*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
169*1003e004SJérôme Duval /* Default equality comparison functions                                                          */
170*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
171*1003e004SJérôme Duval 
172*1003e004SJérôme Duval /**
173*1003e004SJérôme Duval  * Defines a default equality comparison function for pointer values.
174*1003e004SJérôme Duval  *
175*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
176*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
177*1003e004SJérôme Duval  *
178*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
179*1003e004SJérôme Duval  *          not.
180*1003e004SJérôme Duval  */
181*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsPointer, void* const)
182*1003e004SJérôme Duval 
183*1003e004SJérôme Duval /**
184*1003e004SJérôme Duval  * Defines a default equality comparison function for `ZyanBool` values.
185*1003e004SJérôme Duval  *
186*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
187*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
188*1003e004SJérôme Duval  *
189*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
190*1003e004SJérôme Duval  *          not.
191*1003e004SJérôme Duval  */
192*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsBool, ZyanBool)
193*1003e004SJérôme Duval 
194*1003e004SJérôme Duval /**
195*1003e004SJérôme Duval  * Defines a default equality comparison function for 8-bit numeric values.
196*1003e004SJérôme Duval  *
197*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
198*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
199*1003e004SJérôme Duval  *
200*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
201*1003e004SJérôme Duval  *          not.
202*1003e004SJérôme Duval  */
203*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric8, ZyanU8)
204*1003e004SJérôme Duval 
205*1003e004SJérôme Duval /**
206*1003e004SJérôme Duval  * Defines a default equality comparison function for 16-bit numeric values.
207*1003e004SJérôme Duval  *
208*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
209*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
210*1003e004SJérôme Duval  *
211*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
212*1003e004SJérôme Duval  *          not.
213*1003e004SJérôme Duval  */
214*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric16, ZyanU16)
215*1003e004SJérôme Duval 
216*1003e004SJérôme Duval /**
217*1003e004SJérôme Duval  * Defines a default equality comparison function for 32-bit numeric values.
218*1003e004SJérôme Duval  *
219*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
220*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
221*1003e004SJérôme Duval  *
222*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
223*1003e004SJérôme Duval  *          not.
224*1003e004SJérôme Duval  */
225*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric32, ZyanU32)
226*1003e004SJérôme Duval 
227*1003e004SJérôme Duval /**
228*1003e004SJérôme Duval  * Defines a default equality comparison function for 64-bit numeric values.
229*1003e004SJérôme Duval  *
230*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
231*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
232*1003e004SJérôme Duval  *
233*1003e004SJérôme Duval  * @return  Returns `ZYAN_TRUE` if the `left` value equals the `right` one or `ZYAN_FALSE`, if
234*1003e004SJérôme Duval  *          not.
235*1003e004SJérôme Duval  */
236*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric64, ZyanU64)
237*1003e004SJérôme Duval 
238*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
239*1003e004SJérôme Duval /* Default comparison functions                                                                   */
240*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
241*1003e004SJérôme Duval 
242*1003e004SJérôme Duval /**
243*1003e004SJérôme Duval  * Defines a default comparison function for pointer values.
244*1003e004SJérôme Duval  *
245*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
246*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
247*1003e004SJérôme Duval  *
248*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
249*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
250*1003e004SJérôme Duval  */
251*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanComparePointer, void* const)
252*1003e004SJérôme Duval 
253*1003e004SJérôme Duval /**
254*1003e004SJérôme Duval  * Defines a default comparison function for `ZyanBool` values.
255*1003e004SJérôme Duval  *
256*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
257*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
258*1003e004SJérôme Duval  *
259*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
260*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
261*1003e004SJérôme Duval  */
262*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareBool, ZyanBool)
263*1003e004SJérôme Duval 
264*1003e004SJérôme Duval /**
265*1003e004SJérôme Duval  * Defines a default comparison function for 8-bit numeric values.
266*1003e004SJérôme Duval  *
267*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
268*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
269*1003e004SJérôme Duval  *
270*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
271*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
272*1003e004SJérôme Duval  */
273*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric8, ZyanU8)
274*1003e004SJérôme Duval 
275*1003e004SJérôme Duval /**
276*1003e004SJérôme Duval  * Defines a default comparison function for 16-bit numeric values.
277*1003e004SJérôme Duval  *
278*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
279*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
280*1003e004SJérôme Duval  *
281*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
282*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
283*1003e004SJérôme Duval  */
284*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric16, ZyanU16)
285*1003e004SJérôme Duval 
286*1003e004SJérôme Duval /**
287*1003e004SJérôme Duval  * Defines a default comparison function for 32-bit numeric values.
288*1003e004SJérôme Duval  *
289*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
290*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
291*1003e004SJérôme Duval  *
292*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
293*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
294*1003e004SJérôme Duval  */
295*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric32, ZyanU32)
296*1003e004SJérôme Duval 
297*1003e004SJérôme Duval /**
298*1003e004SJérôme Duval  * Defines a default comparison function for 64-bit numeric values.
299*1003e004SJérôme Duval  *
300*1003e004SJérôme Duval  * @param   left    A pointer to the first value.
301*1003e004SJérôme Duval  * @param   right   A pointer to the second value.
302*1003e004SJérôme Duval  *
303*1003e004SJérôme Duval  * @return  Returns `0` if the `left` value equals the `right` one, `-1` if the `left` value is
304*1003e004SJérôme Duval  *          less than the `right` one, or `1` if the `left` value is greater than the `right` one.
305*1003e004SJérôme Duval  */
306*1003e004SJérôme Duval ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric64, ZyanU64)
307*1003e004SJérôme Duval 
308*1003e004SJérôme Duval /* ---------------------------------------------------------------------------------------------- */
309*1003e004SJérôme Duval 
310*1003e004SJérôme Duval /* ============================================================================================== */
311*1003e004SJérôme Duval 
312*1003e004SJérôme Duval #ifdef __cplusplus
313*1003e004SJérôme Duval }
314*1003e004SJérôme Duval #endif
315*1003e004SJérôme Duval 
316*1003e004SJérôme Duval #endif /* ZYCORE_COMPARISON_H */
317