Blame include/libc-pointer-arith.h

Packit 6c4009
/* Helper macros for pointer arithmetic.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _LIBC_POINTER_ARITH_H
Packit 6c4009
#define _LIBC_POINTER_ARITH_H 1
Packit 6c4009
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
/* 1 if 'type' is a pointer type, 0 otherwise.  */
Packit 6c4009
# define __pointer_type(type) (__builtin_classify_type ((type) 0) == 5)
Packit 6c4009
Packit 6c4009
/* intptr_t if P is true, or T if P is false.  */
Packit 6c4009
# define __integer_if_pointer_type_sub(T, P) \
Packit 6c4009
  __typeof__ (*(0 ? (__typeof__ (0 ? (T *) 0 : (void *) (P))) 0 \
Packit 6c4009
		  : (__typeof__ (0 ? (intptr_t *) 0 : (void *) (!(P)))) 0))
Packit 6c4009
Packit 6c4009
/* intptr_t if EXPR has a pointer type, or the type of EXPR otherwise.  */
Packit 6c4009
# define __integer_if_pointer_type(expr) \
Packit 6c4009
  __integer_if_pointer_type_sub(__typeof__ ((__typeof__ (expr)) 0), \
Packit 6c4009
				__pointer_type (__typeof__ (expr)))
Packit 6c4009
Packit 6c4009
/* Cast an integer or a pointer VAL to integer with proper type.  */
Packit 6c4009
# define cast_to_integer(val) ((__integer_if_pointer_type (val)) (val))
Packit 6c4009
Packit 6c4009
/* Align a value by rounding down to closest size.
Packit 6c4009
   e.g. Using size of 4096, we get this behavior:
Packit 6c4009
	{4095, 4096, 4097} = {0, 4096, 4096}.  */
Packit 6c4009
#define ALIGN_DOWN(base, size)	((base) & -((__typeof__ (base)) (size)))
Packit 6c4009
Packit 6c4009
/* Align a value by rounding up to closest size.
Packit 6c4009
   e.g. Using size of 4096, we get this behavior:
Packit 6c4009
	{4095, 4096, 4097} = {4096, 4096, 8192}.
Packit 6c4009
Packit 6c4009
  Note: The size argument has side effects (expanded multiple times).  */
Packit 6c4009
#define ALIGN_UP(base, size)	ALIGN_DOWN ((base) + (size) - 1, (size))
Packit 6c4009
Packit 6c4009
/* Same as ALIGN_DOWN(), but automatically casts when base is a pointer.  */
Packit 6c4009
#define PTR_ALIGN_DOWN(base, size) \
Packit 6c4009
  ((__typeof__ (base)) ALIGN_DOWN ((uintptr_t) (base), (size)))
Packit 6c4009
Packit 6c4009
/* Same as ALIGN_UP(), but automatically casts when base is a pointer.  */
Packit 6c4009
#define PTR_ALIGN_UP(base, size) \
Packit 6c4009
  ((__typeof__ (base)) ALIGN_UP ((uintptr_t) (base), (size)))
Packit 6c4009
Packit 6c4009
#endif