Grok  10.0.3
grk_intmath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2022 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  * This source code incorporates work covered by the BSD 2-clause license.
18  * Please see the LICENSE file in the root directory for details.
19  *
20  */
21 
22 #pragma once
23 
24 namespace grk
25 {
32 template<typename T>
33 uint32_t ceildiv(T a, T b)
34 {
35  assert(b);
36  return (uint32_t)((a + (uint64_t)b - 1) / b);
37 }
38 
39 template<typename T>
40 T ceildivpow2(T a, uint32_t b)
41 {
42  return (T)((a + ((uint64_t)1 << b) - 1) >> b);
43 }
48 static inline uint32_t floordivpow2(uint32_t a, uint32_t b)
49 {
50  return a >> b;
51 }
58 uint8_t floorlog2(uint32_t a);
59 
66 static inline int32_t fix_mul(int32_t a, int32_t b)
67 {
68 #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
69  int64_t temp = __emul(a, b);
70 #else
71  int64_t temp = (int64_t)a * (int64_t)b;
72 #endif
73  temp += 4096; // round by adding "0.5" in 13-bit fixed point
74  assert((temp >> 13) <= (int64_t)0x7FFFFFFF);
75  assert((temp >> 13) >= (-(int64_t)0x7FFFFFFF - (int64_t)1));
76 
77  // return to N-bit precision
78  return (int32_t)(temp >> 13);
79 }
80 } // namespace grk
Copyright (C) 2016-2022 Grok Image Compression Inc.
Definition: ICacheable.h:20
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grk_intmath.h:33
static uint32_t floordivpow2(uint32_t a, uint32_t b)
Divide an unsigned integer by a power of 2 and round downwards.
Definition: grk_intmath.h:48
static int32_t fix_mul(int32_t a, int32_t b)
Multiply two fixed-point numbers.
Definition: grk_intmath.h:66
uint8_t floorlog2(uint32_t a)
Get logarithm of an integer and round downwards.
Definition: grk_intmath.cpp:22
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:40