annotate Discovery/Inc/Utils.h @ 809:9602a7338f28

DevBugfix CO2 sensor in streaming mode: In standalone mode (no multiplexer) the CO2 sensor is working in streaming mode => it could happen that the OSTC received data before the scaling factor was requested. As result the CO2 values were calculated with a zero value. This problem has been fixed. In addition some code cleanup has been done. Unifying usage of changeBaudrate function instead of old baud selection based on protocol request of Discovery firmware.
author Ideenmodellierer
date Sun, 27 Aug 2023 20:51:13 +0200
parents 5f11787b4f42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 /// -*- coding: UTF-8 -*-
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 /// \file Discovery/Inc/Utils.h
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 /// \brief Various Utilities...
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 /// \author Heinrichs Weikamp
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 /// \date 2018
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 /// $Id$
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 /// This program is free software: you can redistribute it and/or modify
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 /// it under the terms of the GNU General Public License as published by
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 /// the Free Software Foundation, either version 3 of the License, or
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 /// (at your option) any later version.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 /// This program is distributed in the hope that it will be useful,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 /// GNU General Public License for more details.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 /// You should have received a copy of the GNU General Public License
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 //////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 #ifndef UTILS_H
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 #define UTILS_H
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 #include <stdint.h>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 #include <stdbool.h>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 #include <math.h>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 #ifdef FloatToFixed
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 #undef FloatToFixed
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 #undef IntToFixed
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 #undef FixedToFloat
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #undef FixedToInt
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 // 20.12 fixed-point operations
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 #define Fix(val) ((val)*4096)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 #define F(val) Fix(val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 #define FixSqrt2 5793
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 #define FixInvSqrt2 2896
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 #define FixSinPiDiv4 FixInvSqrt2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 #define FixCosPiDiv4 FixInvSqrt2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 #define FixSinPiDiv8 1567
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 #define FixCosPiDiv8 3784
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 static inline int32_t FloatToFixed(float val) { return val*4096.0f; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 static inline int32_t IntToFixed(int val) { return val<<12; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 static inline int32_t IntToFixedPlusHalf(int val) { return (val<<12)+0x800; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 static inline float FixedToFloat(int32_t val) { return (float)val/4096.0f; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 static inline int32_t FixedToInt(int32_t val) { return val>>12; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 static inline int32_t FixedToRoundedInt(int32_t val) { return (val+0x800)>>12; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 //static inline int32_t FixedMul(int32_t a,int32_t b) { return ((int64_t)a*(int64_t)b)>>16; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 static inline int32_t imin(int32_t a,int32_t b) { return a<b?a:b; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 static inline int32_t imax(int32_t a,int32_t b) { return a>b?a:b; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 static inline int32_t iabs(int32_t a) { return a<0?-a:a; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 static inline int32_t isign(int32_t a) { return a>0?1:a<0?-1:0; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 static inline int64_t imul64(int64_t a, int64_t b) { return (a*b)>>12; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 static inline int32_t imul(int32_t a, int32_t b) { return (int32_t)imul64(a,b); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 static inline int64_t idiv64(int64_t num,int64_t den) { return (num<<12)/den; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 static inline int32_t idiv(int32_t num,int32_t den) { return (int32_t)idiv64(num,den); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 static inline int64_t isq64(int64_t val) { return imul64(val,val); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 static inline int32_t isq(int32_t val) { return imul(val,val); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 static uint32_t sqrti(uint32_t n)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 uint32_t s,t;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 #define sqrtBit(k) \
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 t = s+(1UL<<(k-1)); t <<= k+1; if (n >= t) { n -= t; s |= 1UL<<k; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 s=0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 if(n>=1<<30) { n-=1<<30; s=1<<15; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 sqrtBit(14); sqrtBit(13); sqrtBit(12); sqrtBit(11); sqrtBit(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 sqrtBit(9); sqrtBit(8); sqrtBit(7); sqrtBit(6); sqrtBit(5);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 sqrtBit(4); sqrtBit(3); sqrtBit(2); sqrtBit(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 if(n>s<<1) s|=1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 #undef sqrtBit
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 return s;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 static inline int64_t isqrt64(int64_t val) { return sqrti((uint64_t)val<<12); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 static inline int32_t isqrt(int32_t val) { return sqrti((uint64_t)val<<12); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 static inline int64_t inorm64(int64_t a,int64_t b) { return sqrt((double)a*(double)a+(double)b*(double)b); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 static inline int32_t inorm(int32_t a,int32_t b) { return sqrt((double)a*(double)a+(double)b*(double)b); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 static inline int32_t ifloor(int a) { return a&~0xfff; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 static inline int32_t ifrac(int a) { return a&0xfff; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 int32_t isin(int a);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 static inline int32_t icos(int a) { return isin(a+1024); }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 static inline int32_t itan(int a) { return idiv(isin(a),icos(a)); } // TODO: use a table
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 static inline int32_t idegrees(int degrees) { return degrees*4096.0f/360.0f; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 static inline int32_t iradians(float radians) { return radians*4096.0f/2/3.1415926535897932f; }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 static inline int32_t ilerp(int32_t a,int32_t b,int32_t t)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 return a+imul(b-a,t);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 /*static inline int32_t islerp(int32_t a,int32_t b,int32_t t)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 return a;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 }*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 // Bitwise operations
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 //
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 static inline int CountBits32(uint32_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 val=(val&0x55555555)+((val&0xaaaaaaaa)>>1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 val=(val&0x33333333)+((val&0xcccccccc)>>2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 val=(val&0x0f0f0f0f)+((val&0xf0f0f0f0)>>4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 val=(val&0x00ff00ff)+((val&0xff00ff00)>>8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 val=(val&0x0000ffff)+((val&0xffff0000)>>16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 static inline int CountBits16(uint16_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 val=(val&0x5555)+((val&0xaaaa)>>1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 val=(val&0x3333)+((val&0xcccc)>>2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 val=(val&0x0f0f)+((val&0xf0f0)>>4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 val=(val&0x00ff)+((val&0xff00)>>8);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 static inline int CountBits8(uint8_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 val=(val&0x55)+((val&0xaa)>>1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 val=(val&0x33)+((val&0xcc)>>2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 val=(val&0x0f)+((val&0xf0)>>4);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 static inline uint32_t ReverseBits32(uint32_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 val=((val>>1)&0x55555555)|((val<<1)&0xaaaaaaaa);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 val=((val>>2)&0x33333333)|((val<<2)&0xcccccccc);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 val=((val>>4)&0x0f0f0f0f)|((val<<4)&0xf0f0f0f0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 val=((val>>8)&0x00ff00ff)|((val<<8)&0xff00ff00);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 val=((val>>16)&0x0000ffff)|((val<<16)&0xffff0000);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 static inline uint16_t ReverseBits16(uint16_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 val=((val>>1)&0x5555)|((val<<1)&0xaaaa);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 val=((val>>2)&0x3333)|((val<<2)&0xcccc);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 val=((val>>4)&0x0f0f)|((val<<4)&0xf0f0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 val=((val>>8)&0x00ff)|((val<<8)&0xff00);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173 static inline uint8_t ReverseBits8(uint8_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 val=((val>>1)&0x55)|((val<<1)&0xaa);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 val=((val>>2)&0x33)|((val<<2)&0xcc);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 val=((val>>4)&0x0f)|((val<<4)&0xf0);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178 return val;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181 static int LowestBitSet32(uint32_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183 if(val==0) return -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184 return CountBits32(val^val-1)-1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187 static int LowestBitSet16(uint16_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 if(val==0) return -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190 return CountBits16(val^val-1)-1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 static int LowestBitSet8(uint8_t val)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 if(val==0) return -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196 return CountBits8(val^val-1)-1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 #endif // UTILS_H