clz_ctz.h
Go to the documentation of this file.00001
00037 #ifndef CLZ_CTH_H
00038 #define CLZ_CTH_H
00039
00051 #define clz(x) compiler_demux_size(sizeof(x), clz, (x))
00052
00061 __always_inline static uint8_t clz8(uint8_t x)
00062 {
00063 uint8_t bit = 0;
00064
00065 if (x & 0xf0) {
00066 x >>= 4;
00067 } else {
00068 bit += 4;
00069 }
00070
00071 if (x & 0x0c) {
00072 x >>= 2;
00073 } else {
00074 bit += 2;
00075 }
00076
00077 if (!(x & 0x02)) {
00078 bit++;
00079 }
00080
00081 return bit;
00082
00083 }
00084
00093 __always_inline static uint8_t clz16(uint16_t x)
00094 {
00095 uint8_t bit = 0;
00096
00097 if (x & 0xff00) {
00098 x >>= 8;
00099 } else {
00100 bit += 8;
00101 }
00102
00103 return bit + clz8(x);
00104 }
00105
00114 __always_inline static uint8_t clz32(uint32_t x)
00115 {
00116 uint8_t bit = 0;
00117
00118 if (x & 0xffff0000) {
00119 x >>= 16;
00120 } else {
00121 bit += 16;
00122 }
00123
00124 return bit + clz16(x);
00125 }
00126
00138 #define ctz(x) compiler_demux_size(sizeof(x), ctz, (x))
00139
00148 __always_inline static uint8_t ctz8(uint8_t x)
00149 {
00150 uint8_t bit = 0;
00151
00152 if (!(x & 0x0f)) {
00153 bit += 4;
00154 x >>= 4;
00155 }
00156 if (!(x & 0x03)) {
00157 bit += 2;
00158 x >>= 2;
00159 }
00160 if (!(x & 0x01))
00161 bit++;
00162
00163 return bit;
00164 }
00165
00174 __always_inline static uint8_t ctz16(uint16_t x)
00175 {
00176 uint8_t bit = 0;
00177
00178 if (!(x & 0x00ff)) {
00179 bit += 8;
00180 x >>= 8;
00181 }
00182
00183 return bit + ctz8(x);
00184 }
00185
00194 __always_inline static uint8_t ctz32(uint32_t x)
00195 {
00196 uint8_t bit = 0;
00197
00198 if (!(x & 0x0000ffff)) {
00199 bit += 16;
00200 x >>= 16;
00201 }
00202
00203 return bit + ctz16(x);
00204 }
00205
00206 #endif