00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CU_SIZE_H
00019 #define CU_SIZE_H
00020
00021 #include <cu/fwd.h>
00022 #include <cu/int.h>
00023
00024 CU_BEGIN_DECLARATIONS
00025
00026
00027
00028 #if CUCONF_SIZEOF_SIZE_T == 4
00029 # define CUP_SIZE_NAME(pre, suf) pre##uint32_##suf
00030 #elif CUCONF_SIZEOF_SIZE_T == 8
00031 # define CUP_SIZE_NAME(pre, suf) pre##uint64_##suf
00032 #else
00033 # error Unexpected CUCONF_SIZEOF_SIZE_T
00034 #endif
00035
00036
00037 CU_SINLINE size_t cu_size_min(size_t n, size_t m) { return n < m? n : m; }
00038
00039
00040 CU_SINLINE size_t cu_size_max(size_t n, size_t m) { return n > m? n : m; }
00041
00042
00043 CU_SINLINE int cu_size_cmp(size_t n, size_t m)
00044 { return n < m? -1 : n > m? 1 : 0; }
00045
00046
00047
00048 CU_SINLINE size_t cu_size_lmask(unsigned int b)
00049 { return ((size_t)(1) << b) - (size_t)1; }
00050
00051
00052 CU_SINLINE size_t cu_size_dcover(size_t x)
00053 { return CUP_SIZE_NAME(cu_,dcover)(x); }
00054
00055
00056 CU_SINLINE size_t cu_size_ucover(size_t x) { return x | ~(x - 1); }
00057
00058
00059 CU_SINLINE size_t cu_size_ceil_div(size_t n, size_t m)
00060 { return (n + m - 1)/m; }
00061
00062
00063
00064 CU_SINLINE size_t cu_size_mulceil(size_t n, size_t m)
00065 { return cu_size_ceil_div(n, m)*m; }
00066
00067
00068
00069 CU_SINLINE size_t cu_size_mulfloor(size_t n, size_t m)
00070 { return (n/m)*m; }
00071
00072
00073
00074 CU_SINLINE size_t cu_size_scal2ceil(size_t n, unsigned int b)
00075 { return (n + cu_size_lmask(b)) & ~cu_size_lmask(b); }
00076
00077
00078
00079 CU_SINLINE size_t cu_size_scal2floor(size_t n, unsigned int b)
00080 { return n & ~cu_size_lmask(b); }
00081
00082
00083 CU_SINLINE size_t cu_size_alignceil(size_t n)
00084 { return cu_size_mulceil(n, CUCONF_MAXALIGN); }
00085
00086
00087 CU_SINLINE size_t cu_size_alignfloor(size_t n)
00088 { return cu_size_mulfloor(n, CUCONF_MAXALIGN); }
00089
00090
00091
00092 CU_SINLINE size_t cu_size_exp2ceil(size_t n)
00093 { return cu_size_dcover(n - (size_t)1) + (size_t)1; }
00094
00095
00096
00097 CU_SINLINE size_t cu_size_exp2floor(size_t n)
00098 { return (cu_size_dcover(n) >> 1) + (size_t)1; }
00099
00100
00101
00102 CU_SINLINE unsigned int cu_size_floor_log2(size_t n)
00103 { return CUP_SIZE_NAME(cu_,floor_log2)(n); }
00104
00105
00106
00107 CU_SINLINE unsigned int cu_size_ceil_log2(size_t n)
00108 { return CUP_SIZE_NAME(cu_,ceil_log2)(n); }
00109
00110
00111 CU_SINLINE unsigned int cu_size_log2_lowbit(size_t n)
00112 { return CUP_SIZE_NAME(cu_,log2_lowbit)(n); }
00113
00114
00115 CU_SINLINE ptrdiff_t cu_ptrdiff_min(ptrdiff_t n, ptrdiff_t m)
00116 { return n < m? n : m; }
00117
00118
00119 CU_SINLINE ptrdiff_t cu_ptrdiff_max(ptrdiff_t n, ptrdiff_t m)
00120 { return n > m? n : m; }
00121
00122
00123 CU_SINLINE ptrdiff_t cu_ptrdiff_abs(ptrdiff_t n)
00124 { return n >= 0? n : -n; }
00125
00126
00127 CU_END_DECLARATIONS
00128
00129 #endif