Grok  7.6.0
grok_intmath.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2020 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 
30 static inline uint32_t uint_subs(uint32_t a, uint32_t b)
31 {
32  return (a >= b) ? a - b : 0;
33 }
34 
41 static inline uint32_t uint_adds(uint32_t a, uint32_t b) {
42  uint64_t sum = (uint64_t) a + (uint64_t) b;
43  return (uint32_t)(-(int32_t)(sum >> 32)) | (uint32_t) sum;
44 }
45 
52 template<typename T> uint32_t ceildiv(T a, T b) {
53  assert(b);
54  return (uint32_t)((a + (uint64_t) b - 1) / b);
55 }
56 
57 template<typename T> T ceildivpow2(T a, uint32_t b) {
58  return (T)((a + ((uint64_t) 1 << b) - 1) >> b);
59 }
60 
67 static inline uint32_t uint64_ceildivpow2(uint64_t a, uint32_t b) {
68  return (uint32_t)((a + ((uint64_t) 1 << b) - 1) >> b);
69 }
70 
75 static inline uint32_t uint_floordivpow2(uint32_t a, uint32_t b) {
76  return a >> b;
77 }
83 template<typename T> T floorlog2(uint32_t a) {
84  T l;
85  for (l = 0; a > 1; l++) {
86  a >>= 1;
87  }
88  return l;
89 }
90 
97 static inline int32_t int_fix_mul(int32_t a, int32_t b) {
98 #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
99  int64_t temp = __emul(a, b);
100 #else
101  int64_t temp = (int64_t) a * (int64_t) b;
102 #endif
103  temp += 4096; //round by adding "0.5" in 13-bit fixed point
104  assert((temp >> 13) <= (int64_t) 0x7FFFFFFF);
105  assert((temp >> 13) >= (-(int64_t) 0x7FFFFFFF - (int64_t) 1));
106 
107  // return to N-bit precision
108  return (int32_t)(temp >> 13);
109 }
110 }
grk::floorlog2
T floorlog2(uint32_t a)
Get logarithm of an integer and round downwards.
Definition: grok_intmath.h:83
grk::uint_floordivpow2
static uint32_t uint_floordivpow2(uint32_t a, uint32_t b)
Divide an unsigned integer by a power of 2 and round downwards.
Definition: grok_intmath.h:75
grk::ceildivpow2
T ceildivpow2(T a, uint32_t b)
Definition: grok_intmath.h:57
grk::ceildiv
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition: grok_intmath.h:52
grk::uint_subs
static uint32_t uint_subs(uint32_t a, uint32_t b)
Get the saturated difference of two unsigned integers.
Definition: grok_intmath.h:30
grk::int_fix_mul
static int32_t int_fix_mul(int32_t a, int32_t b)
Multiply two fixed-point numbers.
Definition: grok_intmath.h:97
grk
Copyright (C) 2016-2020 Grok Image Compression Inc.
Definition: BitIO.h:27
grk::uint_adds
static uint32_t uint_adds(uint32_t a, uint32_t b)
Get the saturated sum of two unsigned integers.
Definition: grok_intmath.h:41
grk::uint64_ceildivpow2
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: grok_intmath.h:67