Grok  7.6.6
grk_intmath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 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> uint32_t ceildiv(T a, T b) {
33  assert(b);
34  return (uint32_t)((a + (uint64_t) b - 1) / b);
35 }
36 
37 template<typename T> T ceildivpow2(T a, uint32_t b) {
38  return (T)((a + ((uint64_t) 1 << b) - 1) >> b);
39 }
40 
47 static inline uint32_t uint64_ceildivpow2(uint64_t a, uint32_t b) {
48  return (uint32_t)((a + ((uint64_t) 1 << b) - 1) >> b);
49 }
50 
55 static inline uint32_t uint_floordivpow2(uint32_t a, uint32_t b) {
56  return a >> b;
57 }
63 template<typename T> T floorlog2(uint32_t a) {
64  T l;
65  for (l = 0; a > 1; l++) {
66  a >>= 1;
67  }
68  return l;
69 }
70 
77 static inline int32_t int_fix_mul(int32_t a, int32_t b) {
78 #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
79  int64_t temp = __emul(a, b);
80 #else
81  int64_t temp = (int64_t) a * (int64_t) b;
82 #endif
83  temp += 4096; //round by adding "0.5" in 13-bit fixed point
84  assert((temp >> 13) <= (int64_t) 0x7FFFFFFF);
85  assert((temp >> 13) >= (-(int64_t) 0x7FFFFFFF - (int64_t) 1));
86 
87  // return to N-bit precision
88  return (int32_t)(temp >> 13);
89 }
90 }
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
static uint32_t uint64_ceildivpow2(uint64_t a, uint32_t b)
Divide a 64-bit integer by a power of 2 and round upwards.
Definition: grk_intmath.h:47
static uint32_t uint_floordivpow2(uint32_t a, uint32_t b)
Divide an unsigned integer by a power of 2 and round downwards.
Definition: grk_intmath.h:55
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grk_intmath.h:32
static int32_t int_fix_mul(int32_t a, int32_t b)
Multiply two fixed-point numbers.
Definition: grk_intmath.h:77
T floorlog2(uint32_t a)
Get logarithm of an integer and round downwards.
Definition: grk_intmath.h:63
T ceildivpow2(T a, uint32_t b)
Definition: grk_intmath.h:37