| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Math utility functions for vLLM.""" |
| 4 | |
| 5 | # Approximate value of 1/ln(2), used for log/exp base conversion |
| 6 | # Best FP32 approximation: 1.4426950216 (hex 0x3FB8AA3B) |
| 7 | RCP_LN2 = 1.4426950216 |
| 8 | |
| 9 | |
| 10 | def cdiv(a: int, b: int) -> int: |
| 11 | """Ceiling division.""" |
| 12 | return -(a // -b) |
| 13 | |
| 14 | |
| 15 | def next_power_of_2(n: int) -> int: |
| 16 | """The next power of 2 (inclusive).""" |
| 17 | return 1 if n < 1 else 1 << (n - 1).bit_length() |
| 18 | |
| 19 | |
| 20 | def round_up(x: int, y: int) -> int: |
| 21 | """Round up x to the nearest multiple of y.""" |
| 22 | return ((x + y - 1) // y) * y |
| 23 | |
| 24 | |
| 25 | def round_down(x: int, y: int) -> int: |
| 26 | """Round down x to the nearest multiple of y.""" |
| 27 | return (x // y) * y |
| 28 | |
| 29 | |
| 30 | def largest_power_of_2_divisor(n: int) -> int: |
| 31 | """Return the largest power-of-2 that divides *n* (isolate lowest set bit).""" |
| 32 | return n & (-n) |