include/linux/minmax.h

Source file repositories/reference/linux-study-clean/include/linux/minmax.h

File Facts

System
Linux kernel
Corpus path
include/linux/minmax.h
Extension
.h
Size
10206 bytes
Lines
320
Domain
Core OS
Bucket
Core Kernel Interface
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

#ifndef _LINUX_MINMAX_H
#define _LINUX_MINMAX_H

#include <linux/build_bug.h>
#include <linux/compiler.h>
#include <linux/const.h>
#include <linux/types.h>

/*
 * min()/max()/clamp() macros must accomplish several things:
 *
 * - Avoid multiple evaluations of the arguments (so side-effects like
 *   "x++" happen only once) when non-constant.
 * - Perform signed v unsigned type-checking (to generate compile
 *   errors instead of nasty runtime surprises).
 * - Unsigned char/short are always promoted to signed int and can be
 *   compared against signed or unsigned arguments.
 * - Unsigned arguments can be compared against non-negative signed constants.
 * - Comparison of a signed argument against an unsigned constant fails
 *   even if the constant is below __INT_MAX__ and could be cast to int.
 */
#define __typecheck(x, y) \
	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))

/*
 * __sign_use for integer expressions:
 *   bit #0 set if ok for unsigned comparisons
 *   bit #1 set if ok for signed comparisons
 *
 * In particular, statically non-negative signed integer expressions
 * are ok for both.
 *
 * NOTE! Unsigned types smaller than 'int' are implicitly converted to 'int'
 * in expressions, and are accepted for signed conversions for now.
 * This is debatable.
 *
 * Note that 'x' is the original expression, and 'ux' is the unique variable
 * that contains the value.
 *
 * We use 'ux' for pure type checking, and 'x' for when we need to look at the
 * value (but without evaluating it for side effects!
 * Careful to only ever evaluate it with sizeof() or __builtin_constant_p() etc).
 *
 * Pointers end up being checked by the normal C type rules at the actual
 * comparison, and these expressions only need to be careful to not cause
 * warnings for pointer use.
 */
#define __sign_use(ux) (is_signed_type(typeof(ux)) ? \
	(2 + __is_nonneg(ux)) : (1 + 2 * (sizeof(ux) < 4)))

/*
 * Check whether a signed value is always non-negative.
 *
 * A cast is needed to avoid any warnings from values that aren't signed
 * integer types (in which case the result doesn't matter).
 *
 * On 64-bit any integer or pointer type can safely be cast to 'long long'.
 * But on 32-bit we need to avoid warnings about casting pointers to integers
 * of different sizes without truncating 64-bit values so 'long' or 'long long'
 * must be used depending on the size of the value.
 *
 * This does not work for 128-bit signed integers since the cast would truncate
 * them, but we do not use s128 types in the kernel (we do use 'u128',
 * but they are handled by the !is_signed_type() case).
 */
#if __SIZEOF_POINTER__ == __SIZEOF_LONG_LONG__
#define __is_nonneg(ux) statically_true((long long)(ux) >= 0)
#else
#define __is_nonneg(ux) statically_true( \
	(typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)))(ux) >= 0)
#endif

#define __types_ok(ux, uy) \
	(__sign_use(ux) & __sign_use(uy))

#define __types_ok3(ux, uy, uz) \
	(__sign_use(ux) & __sign_use(uy) & __sign_use(uz))

#define __cmp_op_min <
#define __cmp_op_max >

#define __cmp(op, x, y)	((x) __cmp_op_##op (y) ? (x) : (y))

#define __cmp_once_unique(op, type, x, y, ux, uy) \
	({ type ux = (x); type uy = (y); __cmp(op, ux, uy); })

#define __cmp_once(op, type, x, y) \
	__cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))

#define __careful_cmp_once(op, x, y, ux, uy) ({		\

Annotation

Implementation Notes