# 向上取整算法

int(a+b1b)
（其中a是实际使用的内存， b是对齐值）

## 公式推导

A=NB+M(M[0,B1])$假设 A = NB +M (M \in \left[0,B-1\right])$

AB=N+MB$\because\frac{A}{B} = N + \frac{M}{B}$

M[0,B1]$\because M \in\left[0, B-1\right]$

ABNB+B1BA+B1B$\therefore \frac{A}{B} \leq \frac{NB +B -1}{B} \leq \frac{A + B -1}{B}$

A=NB+M(M[0,B1])$假设 A = NB +M (M \in \left[0,B-1\right])$

M=0UP(AB)=N$当M = 0时UP(\frac{A}{B}) = N$

M0UP(AB)=N$当M \neq 0时，UP(\frac{A}{B}) = N$

M=0int(A+B1B)=int(NB+B1B)=int(N+11B)$当M = 0时int(\frac{A+B-1}{B} )=int(\frac{NB+B -1}{B}) = int(N + 1 - \frac{1}{B})$

1B<1$\because \frac{1}{B} < 1$

int(A+B1B)$\therefore int(\frac{A+B-1}{B} )$

M0int(A+B1B)=int(NB+M+B1B)=int(N+1+M1B)$当 M \neq 0 时int(\frac{A+B-1}{B} ) = int(\frac{NB +M + B -1}{B}) = int(N + 1 + \frac{M-1}{B})$

M=1int(A+B1B)=int(N+1+M1B)=N+1$当M = 1时 int(\frac{A+B-1}{B} ) = int(N + 1 + \frac{M-1}{B}) = N + 1$

1<MB1M1B<1$当1 < M \leq B-1时 \frac{M -1}{B} < 1$

int(A+B1B)=int(N+1+M1B)=N$\therefore int(\frac{A+B-1}{B} ) = int(N + 1 + \frac{M-1}{B}) = N$

UP(AB)=int(A+B1B)$UP(\frac{A}{B}) = int(\frac{A + B - 1}{B})$

int(A+B1B)×B$int(\frac{A + B - 1}{B}) \times B$

## 补充

long(A + B - 1) &~ (B - 1)