Grok 20.3.2
intmath.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <cassert>
21#include <cstdint>
22
23namespace grk
24{
31template<typename T>
32uint32_t ceildiv(T a, T b)
33{
34 assert(b);
35 return (uint32_t)((a + (uint64_t)b - 1) / b);
36}
37
38template<typename T>
39T ceildivpow2(T a, uint8_t b)
40{
41 return (T)((a + ((uint64_t)1 << b) - 1) >> b);
42}
43
47static inline uint32_t floordivpow2(uint32_t a, uint8_t b)
48{
49 return a >> b;
50}
51
58static inline int32_t fix_mul(int32_t a, int32_t b)
59{
60 int64_t temp = (int64_t)a * (int64_t)b;
61 temp += 4096; // round by adding "0.5" in 13-bit fixed point
62 assert((temp >> 13) <= (int64_t)0x7FFFFFFF);
63 assert((temp >> 13) >= (-(int64_t)0x7FFFFFFF - (int64_t)1));
64
65 // return to N-bit precision
66 return (int32_t)(temp >> 13);
67}
68
74static inline uint8_t floorlog2(uint32_t a)
75{
76 uint8_t l;
77 for(l = 0; a > 1; l++)
78 {
79 a >>= 1;
80 }
81 return l;
82}
83
84} // namespace grk
ResWindow.
Definition CompressedChunkCache.h:36
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition intmath.h:32
static uint32_t floordivpow2(uint32_t a, uint8_t b)
Divide an unsigned integer by a power of 2 and round downwards.
Definition intmath.h:47
static uint8_t floorlog2(uint32_t a)
Get logarithm of an integer and round downwards.
Definition intmath.h:74
T ceildivpow2(T a, uint8_t b)
Definition intmath.h:39
static int32_t fix_mul(int32_t a, int32_t b)
Multiply two fixed-point numbers.
Definition intmath.h:58