Nu:Tekt NTS-1 digital SDK  v1.1-0
int_math.h
Go to the documentation of this file.
1 /*
2  BSD 3-Clause License
3 
4  Copyright (c) 2018, KORG INC.
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12 
13  * Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17  * Neither the name of the copyright holder nor the names of its
18  contributors may be used to endorse or promote products derived from
19  this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 //*/
33 
46 #ifndef __int_math_h
47 #define __int_math_h
48 
49 #include <stdint.h>
50 
51 /*===========================================================================*/
52 /* Clipping Operations. */
53 /*===========================================================================*/
54 
62 static inline __attribute__((optimize("Ofast"), always_inline))
63 int32_t clipmaxi32(const int32_t x, const int32_t m) {
64  return (((x)>=m)?m:(x));
65 }
66 
69 static inline __attribute__((optimize("Ofast"), always_inline))
70 int32_t clipmini32(const int32_t m, const int32_t x) {
71  return (((x)<=m)?m:(x));
72 }
73 
76 static inline __attribute__((optimize("Ofast"), always_inline))
77 int32_t clipminmaxi32(const int32_t min, const int32_t x, const int32_t max) {
78  return (((x)>=max)?max:((x)<=min)?min:(x));
79 }
80 
83 static inline __attribute__((optimize("Ofast"), always_inline))
84 uint32_t clipmaxu32(const uint32_t x, const uint32_t m) {
85  return (((x)>=m)?m:(x));
86 }
87 
90 static inline __attribute__((optimize("Ofast"), always_inline))
91 uint32_t clipminu32(const uint32_t m, const uint32_t x) {
92  return (((x)<=m)?m:(x));
93 }
94 
97 static inline __attribute__((optimize("Ofast"), always_inline))
98 uint32_t clipminmaxu32(const uint32_t min, const uint32_t x, const uint32_t max) {
99  return (((x)>=max)?max:((x)<=min)?min:(x));
100 }
101 
102 
105 /*===========================================================================*/
106 /* Power of 2s. */
107 /*===========================================================================*/
108 
116 static inline __attribute__((always_inline))
117 uint32_t nextpow2_u32(uint32_t x) {
118  x--;
119  x |= x>>1; x |= x>>2;
120  x |= x>>4; x |= x>>8;
121  x |= x>>16;
122  return ++x;
123 }
124 
127 static inline __attribute__((always_inline))
128 uint8_t ispow2_u32(const uint32_t x) {
129  return x && !(x & (x-1));
130 }
131 
134 #endif // __int_math_h
135 
clipmaxu32
static uint32_t clipmaxu32(const uint32_t x, const uint32_t m)
Clip upper bound of unsigned integer x to m (inclusive)
Definition: int_math.h:84
clipminmaxu32
static uint32_t clipminmaxu32(const uint32_t min, const uint32_t x, const uint32_t max)
Clip unsigned integer x between min and max (inclusive)
Definition: int_math.h:98
clipminu32
static uint32_t clipminu32(const uint32_t m, const uint32_t x)
Clip lower bound of unsigned integer x to m (inclusive)
Definition: int_math.h:91
clipmaxi32
static int32_t clipmaxi32(const int32_t x, const int32_t m)
Clip upper bound of signed integer x to m (inclusive)
Definition: int_math.h:63
ispow2_u32
static uint8_t ispow2_u32(const uint32_t x)
Check if x is a power of 2.
Definition: int_math.h:128
clipmini32
static int32_t clipmini32(const int32_t m, const int32_t x)
Clip lower bound of signed integer x to m (inclusive)
Definition: int_math.h:70
clipminmaxi32
static int32_t clipminmaxi32(const int32_t min, const int32_t x, const int32_t max)
Clip signe integer x between min and max (inclusive)
Definition: int_math.h:77
nextpow2_u32
static uint32_t nextpow2_u32(uint32_t x)
Compute next power of 2 greater than x.
Definition: int_math.h:117