File: minmax.h

package info (click to toggle)
drgn 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,852 kB
  • sloc: python: 74,992; ansic: 54,589; awk: 423; makefile: 351; sh: 99
file content (65 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

/**
 * @file
 *
 * Minimum/maximum operations.
 *
 * See @ref MinMaxOperations.
 */

#ifndef DRGN_MINMAX_H
#define DRGN_MINMAX_H

#include "pp.h"

/**
 * @ingroup Internals
 *
 * @defgroup MinMaxOperations Minimum/maximum operations
 *
 * Generic minimum/maximum operations.
 *
 * @{
 */

/** Get the minimum of two expressions with compatible types. */
#define min(x, y) cmp_once_impl(x, y, PP_UNIQUE(_x), PP_UNIQUE(_y), <)
/** Get the maximum of two expressions with compatible types. */
#define max(x, y) cmp_once_impl(x, y, PP_UNIQUE(_x), PP_UNIQUE(_y), >)
/** @cond */
#define cmp_once_impl(x, y, unique_x, unique_y, op) ({				\
	__auto_type unique_x = (x);						\
	__auto_type unique_y = (y);						\
	/* Generate a warning if x and y do not have compatible types. */	\
	(void)(&unique_x == &unique_y);						\
	unique_x op unique_y ? unique_x : unique_y;				\
})
/** @endcond */

/**
 * Get the minimum of two integer constant expressions, resulting in an integer
 * constant expression.
 */
#define min_iconst(x, y) cmp_iconst_impl(x, y, <)
/**
 * Get the maximum of two integer constant expressions, resulting in an integer
 * constant expression.
 */
#define max_iconst(x, y) cmp_iconst_impl(x, y, >)
/** @cond */
#define cmp_iconst_impl(x, y, op)						\
	/*									\
	 * Enforce that the arguments are integer constant expressions. The	\
	 * size of a non-VLA array must be an integer constant expression, and	\
	 * a compound literal cannot be a VLA. Evaluates to non-zero to fall	\
	 * through to the comparison.						\
	 */									\
	(sizeof((char [(x) * 0 + (y) * 0 + 1]){0}) &&				\
	 (x) op (y) ? (x) : (y))
/** @endcond */

/** @} */

#endif /* DRGN_MINMAX_H */