From c00e3e1c3dac4bc4cdf70029f1fc2a2f08f133cc Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Tue, 7 Apr 2020 08:40:56 -0700 Subject: [PATCH 1/2] Add cast_to_unsigned_integer Cast an integer or a pointer VAL to unsigned integer with proper type. --- include/libc-pointer-arith.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/include/libc-pointer-arith.h b/include/libc-pointer-arith.h index b4d716c727..3468c2f141 100644 --- a/include/libc-pointer-arith.h +++ b/include/libc-pointer-arith.h @@ -24,19 +24,31 @@ /* 1 if 'type' is a pointer type, 0 otherwise. */ # define __pointer_type(type) (__builtin_classify_type ((type) 0) == 5) -/* intptr_t if P is true, or T if P is false. */ -# define __integer_if_pointer_type_sub(T, P) \ +/* INTPTR_T if P is true, or T if P is false. */ +# define __integer_if_pointer_type_sub(T, P, INTPTR_T) \ __typeof__ (*(0 ? (__typeof__ (0 ? (T *) 0 : (void *) (P))) 0 \ - : (__typeof__ (0 ? (intptr_t *) 0 : (void *) (!(P)))) 0)) + : (__typeof__ (0 ? (INTPTR_T *) 0 : (void *) (!(P)))) 0)) /* intptr_t if EXPR has a pointer type, or the type of EXPR otherwise. */ # define __integer_if_pointer_type(expr) \ __integer_if_pointer_type_sub(__typeof__ ((__typeof__ (expr)) 0), \ - __pointer_type (__typeof__ (expr))) + __pointer_type (__typeof__ (expr)), \ + intptr_t) + +/* uintptr_t if EXPR has a pointer type, or the type of EXPR otherwise. */ +# define __unsigned_integer_if_pointer_type(expr) \ + __integer_if_pointer_type_sub(__typeof__ ((__typeof__ (expr)) 0), \ + __pointer_type (__typeof__ (expr)), \ + uintptr_t) /* Cast an integer or a pointer VAL to integer with proper type. */ # define cast_to_integer(val) ((__integer_if_pointer_type (val)) (val)) +/* Cast an integer or a pointer VAL to unsigned integer with proper + type. */ +# define cast_to_unsigned_integer(val) \ + ((__unsigned_integer_if_pointer_type (val)) (val)) + /* Align a value by rounding down to closest size. e.g. Using size of 4096, we get this behavior: {4095, 4096, 4097} = {0, 4096, 4096}. */ -- 2.25.2