\doxysection{log2.\+h}
\label{log2_8h_source}\index{include/osmocom/core/log2.h@{include/osmocom/core/log2.h}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{1 \textcolor{comment}{/* SPDX-\/License-\/Identifier: GPL-\/2.0-\/or-\/later */}}
\DoxyCodeLine{2 \textcolor{comment}{/* Integer base 2 logarithm calculation}}
\DoxyCodeLine{3 \textcolor{comment}{ *}}
\DoxyCodeLine{4 \textcolor{comment}{ * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.}}
\DoxyCodeLine{5 \textcolor{comment}{ * Written by David Howells (dhowells@redhat.com)}}
\DoxyCodeLine{6 \textcolor{comment}{ */}}
\DoxyCodeLine{7 }
\DoxyCodeLine{8 \textcolor{preprocessor}{\#pragma once}}
\DoxyCodeLine{9 \textcolor{preprocessor}{\#include <stdint.h>}}
\DoxyCodeLine{10 }
\DoxyCodeLine{11 \textcolor{comment}{/* from linux/asm-\/generic/bitops/\{fls,fls64\}.h -\/ could later be enhanced}}
\DoxyCodeLine{12 \textcolor{comment}{ * with architecture specific optimized versions */}}
\DoxyCodeLine{13 }
\DoxyCodeLine{21 \textcolor{keyword}{static} \textcolor{keyword}{inline} \_\_attribute\_\_((always\_inline)) \textcolor{keywordtype}{int} fls(\textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} x)}
\DoxyCodeLine{22 \{}
\DoxyCodeLine{23         \textcolor{keywordtype}{int} r = 32;}
\DoxyCodeLine{24 }
\DoxyCodeLine{25         \textcolor{keywordflow}{if} (!x)}
\DoxyCodeLine{26                 \textcolor{keywordflow}{return} 0;}
\DoxyCodeLine{27         \textcolor{keywordflow}{if} (!(x \& 0xffff0000u)) \{}
\DoxyCodeLine{28                 x <<= 16;}
\DoxyCodeLine{29                 r -\/= 16;}
\DoxyCodeLine{30         \}}
\DoxyCodeLine{31         \textcolor{keywordflow}{if} (!(x \& 0xff000000u)) \{}
\DoxyCodeLine{32                 x <<= 8;}
\DoxyCodeLine{33                 r -\/= 8;}
\DoxyCodeLine{34         \}}
\DoxyCodeLine{35         \textcolor{keywordflow}{if} (!(x \& 0xf0000000u)) \{}
\DoxyCodeLine{36                 x <<= 4;}
\DoxyCodeLine{37                 r -\/= 4;}
\DoxyCodeLine{38         \}}
\DoxyCodeLine{39         \textcolor{keywordflow}{if} (!(x \& 0xc0000000u)) \{}
\DoxyCodeLine{40                 x <<= 2;}
\DoxyCodeLine{41                 r -\/= 2;}
\DoxyCodeLine{42         \}}
\DoxyCodeLine{43         \textcolor{keywordflow}{if} (!(x \& 0x80000000u)) \{}
\DoxyCodeLine{44                 x <<= 1;}
\DoxyCodeLine{45                 r -\/= 1;}
\DoxyCodeLine{46         \}}
\DoxyCodeLine{47         \textcolor{keywordflow}{return} r;}
\DoxyCodeLine{48 \}}
\DoxyCodeLine{49 }
\DoxyCodeLine{61 \textcolor{keyword}{static} \textcolor{keyword}{inline} \_\_attribute\_\_((always\_inline)) \textcolor{keywordtype}{int} fls64(uint64\_t x)}
\DoxyCodeLine{62 \{}
\DoxyCodeLine{63         uint32\_t h = x >> 32;}
\DoxyCodeLine{64         \textcolor{keywordflow}{if} (h)}
\DoxyCodeLine{65                 \textcolor{keywordflow}{return} fls(h) + 32;}
\DoxyCodeLine{66         \textcolor{keywordflow}{return} fls(x);}
\DoxyCodeLine{67 \}}
\DoxyCodeLine{68 }
\DoxyCodeLine{69 \textcolor{comment}{/*}}
\DoxyCodeLine{70 \textcolor{comment}{ * non-\/constant log of base 2 calculators}}
\DoxyCodeLine{71 \textcolor{comment}{ * -\/ the arch may override these in asm/bitops.h if they can be implemented}}
\DoxyCodeLine{72 \textcolor{comment}{ *   more efficiently than using fls() and fls64()}}
\DoxyCodeLine{73 \textcolor{comment}{ * -\/ the arch is not required to handle n==0 if implementing the fallback}}
\DoxyCodeLine{74 \textcolor{comment}{ */}}
\DoxyCodeLine{75 \textcolor{preprocessor}{\#ifndef CONFIG\_ARCH\_HAS\_ILOG2\_U32}}
\DoxyCodeLine{76 \textcolor{keyword}{static} \textcolor{keyword}{inline} \_\_attribute\_\_((\textcolor{keyword}{const}))}
\DoxyCodeLine{77 int \_\_ilog2\_u32(uint32\_t n)}
\DoxyCodeLine{78 \{}
\DoxyCodeLine{79         \textcolor{keywordflow}{return} fls(n) -\/ 1;}
\DoxyCodeLine{80 \}}
\DoxyCodeLine{81 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{82 }
\DoxyCodeLine{83 \textcolor{preprocessor}{\#ifndef CONFIG\_ARCH\_HAS\_ILOG2\_U64}}
\DoxyCodeLine{84 \textcolor{keyword}{static} \textcolor{keyword}{inline} \_\_attribute\_\_((\textcolor{keyword}{const}))}
\DoxyCodeLine{85 int \_\_ilog2\_u64(uint64\_t n)}
\DoxyCodeLine{86 \{}
\DoxyCodeLine{87         \textcolor{keywordflow}{return} fls64(n) -\/ 1;}
\DoxyCodeLine{88 \}}
\DoxyCodeLine{89 \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{90 }
\DoxyCodeLine{98 \textcolor{preprocessor}{\#define const\_ilog2(n)                          \(\backslash\)}}
\DoxyCodeLine{99 \textcolor{preprocessor}{(                                               \(\backslash\)}}
\DoxyCodeLine{100 \textcolor{preprocessor}{        \_\_builtin\_constant\_p(n) ? (             \(\backslash\)}}
\DoxyCodeLine{101 \textcolor{preprocessor}{                (n) < 2 ? 0 :                   \(\backslash\)}}
\DoxyCodeLine{102 \textcolor{preprocessor}{                (n) \& (1ULL << 63) ? 63 :       \(\backslash\)}}
\DoxyCodeLine{103 \textcolor{preprocessor}{                (n) \& (1ULL << 62) ? 62 :       \(\backslash\)}}
\DoxyCodeLine{104 \textcolor{preprocessor}{                (n) \& (1ULL << 61) ? 61 :       \(\backslash\)}}
\DoxyCodeLine{105 \textcolor{preprocessor}{                (n) \& (1ULL << 60) ? 60 :       \(\backslash\)}}
\DoxyCodeLine{106 \textcolor{preprocessor}{                (n) \& (1ULL << 59) ? 59 :       \(\backslash\)}}
\DoxyCodeLine{107 \textcolor{preprocessor}{                (n) \& (1ULL << 58) ? 58 :       \(\backslash\)}}
\DoxyCodeLine{108 \textcolor{preprocessor}{                (n) \& (1ULL << 57) ? 57 :       \(\backslash\)}}
\DoxyCodeLine{109 \textcolor{preprocessor}{                (n) \& (1ULL << 56) ? 56 :       \(\backslash\)}}
\DoxyCodeLine{110 \textcolor{preprocessor}{                (n) \& (1ULL << 55) ? 55 :       \(\backslash\)}}
\DoxyCodeLine{111 \textcolor{preprocessor}{                (n) \& (1ULL << 54) ? 54 :       \(\backslash\)}}
\DoxyCodeLine{112 \textcolor{preprocessor}{                (n) \& (1ULL << 53) ? 53 :       \(\backslash\)}}
\DoxyCodeLine{113 \textcolor{preprocessor}{                (n) \& (1ULL << 52) ? 52 :       \(\backslash\)}}
\DoxyCodeLine{114 \textcolor{preprocessor}{                (n) \& (1ULL << 51) ? 51 :       \(\backslash\)}}
\DoxyCodeLine{115 \textcolor{preprocessor}{                (n) \& (1ULL << 50) ? 50 :       \(\backslash\)}}
\DoxyCodeLine{116 \textcolor{preprocessor}{                (n) \& (1ULL << 49) ? 49 :       \(\backslash\)}}
\DoxyCodeLine{117 \textcolor{preprocessor}{                (n) \& (1ULL << 48) ? 48 :       \(\backslash\)}}
\DoxyCodeLine{118 \textcolor{preprocessor}{                (n) \& (1ULL << 47) ? 47 :       \(\backslash\)}}
\DoxyCodeLine{119 \textcolor{preprocessor}{                (n) \& (1ULL << 46) ? 46 :       \(\backslash\)}}
\DoxyCodeLine{120 \textcolor{preprocessor}{                (n) \& (1ULL << 45) ? 45 :       \(\backslash\)}}
\DoxyCodeLine{121 \textcolor{preprocessor}{                (n) \& (1ULL << 44) ? 44 :       \(\backslash\)}}
\DoxyCodeLine{122 \textcolor{preprocessor}{                (n) \& (1ULL << 43) ? 43 :       \(\backslash\)}}
\DoxyCodeLine{123 \textcolor{preprocessor}{                (n) \& (1ULL << 42) ? 42 :       \(\backslash\)}}
\DoxyCodeLine{124 \textcolor{preprocessor}{                (n) \& (1ULL << 41) ? 41 :       \(\backslash\)}}
\DoxyCodeLine{125 \textcolor{preprocessor}{                (n) \& (1ULL << 40) ? 40 :       \(\backslash\)}}
\DoxyCodeLine{126 \textcolor{preprocessor}{                (n) \& (1ULL << 39) ? 39 :       \(\backslash\)}}
\DoxyCodeLine{127 \textcolor{preprocessor}{                (n) \& (1ULL << 38) ? 38 :       \(\backslash\)}}
\DoxyCodeLine{128 \textcolor{preprocessor}{                (n) \& (1ULL << 37) ? 37 :       \(\backslash\)}}
\DoxyCodeLine{129 \textcolor{preprocessor}{                (n) \& (1ULL << 36) ? 36 :       \(\backslash\)}}
\DoxyCodeLine{130 \textcolor{preprocessor}{                (n) \& (1ULL << 35) ? 35 :       \(\backslash\)}}
\DoxyCodeLine{131 \textcolor{preprocessor}{                (n) \& (1ULL << 34) ? 34 :       \(\backslash\)}}
\DoxyCodeLine{132 \textcolor{preprocessor}{                (n) \& (1ULL << 33) ? 33 :       \(\backslash\)}}
\DoxyCodeLine{133 \textcolor{preprocessor}{                (n) \& (1ULL << 32) ? 32 :       \(\backslash\)}}
\DoxyCodeLine{134 \textcolor{preprocessor}{                (n) \& (1ULL << 31) ? 31 :       \(\backslash\)}}
\DoxyCodeLine{135 \textcolor{preprocessor}{                (n) \& (1ULL << 30) ? 30 :       \(\backslash\)}}
\DoxyCodeLine{136 \textcolor{preprocessor}{                (n) \& (1ULL << 29) ? 29 :       \(\backslash\)}}
\DoxyCodeLine{137 \textcolor{preprocessor}{                (n) \& (1ULL << 28) ? 28 :       \(\backslash\)}}
\DoxyCodeLine{138 \textcolor{preprocessor}{                (n) \& (1ULL << 27) ? 27 :       \(\backslash\)}}
\DoxyCodeLine{139 \textcolor{preprocessor}{                (n) \& (1ULL << 26) ? 26 :       \(\backslash\)}}
\DoxyCodeLine{140 \textcolor{preprocessor}{                (n) \& (1ULL << 25) ? 25 :       \(\backslash\)}}
\DoxyCodeLine{141 \textcolor{preprocessor}{                (n) \& (1ULL << 24) ? 24 :       \(\backslash\)}}
\DoxyCodeLine{142 \textcolor{preprocessor}{                (n) \& (1ULL << 23) ? 23 :       \(\backslash\)}}
\DoxyCodeLine{143 \textcolor{preprocessor}{                (n) \& (1ULL << 22) ? 22 :       \(\backslash\)}}
\DoxyCodeLine{144 \textcolor{preprocessor}{                (n) \& (1ULL << 21) ? 21 :       \(\backslash\)}}
\DoxyCodeLine{145 \textcolor{preprocessor}{                (n) \& (1ULL << 20) ? 20 :       \(\backslash\)}}
\DoxyCodeLine{146 \textcolor{preprocessor}{                (n) \& (1ULL << 19) ? 19 :       \(\backslash\)}}
\DoxyCodeLine{147 \textcolor{preprocessor}{                (n) \& (1ULL << 18) ? 18 :       \(\backslash\)}}
\DoxyCodeLine{148 \textcolor{preprocessor}{                (n) \& (1ULL << 17) ? 17 :       \(\backslash\)}}
\DoxyCodeLine{149 \textcolor{preprocessor}{                (n) \& (1ULL << 16) ? 16 :       \(\backslash\)}}
\DoxyCodeLine{150 \textcolor{preprocessor}{                (n) \& (1ULL << 15) ? 15 :       \(\backslash\)}}
\DoxyCodeLine{151 \textcolor{preprocessor}{                (n) \& (1ULL << 14) ? 14 :       \(\backslash\)}}
\DoxyCodeLine{152 \textcolor{preprocessor}{                (n) \& (1ULL << 13) ? 13 :       \(\backslash\)}}
\DoxyCodeLine{153 \textcolor{preprocessor}{                (n) \& (1ULL << 12) ? 12 :       \(\backslash\)}}
\DoxyCodeLine{154 \textcolor{preprocessor}{                (n) \& (1ULL << 11) ? 11 :       \(\backslash\)}}
\DoxyCodeLine{155 \textcolor{preprocessor}{                (n) \& (1ULL << 10) ? 10 :       \(\backslash\)}}
\DoxyCodeLine{156 \textcolor{preprocessor}{                (n) \& (1ULL <<  9) ?  9 :       \(\backslash\)}}
\DoxyCodeLine{157 \textcolor{preprocessor}{                (n) \& (1ULL <<  8) ?  8 :       \(\backslash\)}}
\DoxyCodeLine{158 \textcolor{preprocessor}{                (n) \& (1ULL <<  7) ?  7 :       \(\backslash\)}}
\DoxyCodeLine{159 \textcolor{preprocessor}{                (n) \& (1ULL <<  6) ?  6 :       \(\backslash\)}}
\DoxyCodeLine{160 \textcolor{preprocessor}{                (n) \& (1ULL <<  5) ?  5 :       \(\backslash\)}}
\DoxyCodeLine{161 \textcolor{preprocessor}{                (n) \& (1ULL <<  4) ?  4 :       \(\backslash\)}}
\DoxyCodeLine{162 \textcolor{preprocessor}{                (n) \& (1ULL <<  3) ?  3 :       \(\backslash\)}}
\DoxyCodeLine{163 \textcolor{preprocessor}{                (n) \& (1ULL <<  2) ?  2 :       \(\backslash\)}}
\DoxyCodeLine{164 \textcolor{preprocessor}{                1) :                            \(\backslash\)}}
\DoxyCodeLine{165 \textcolor{preprocessor}{        -\/1)}}
\DoxyCodeLine{166 }
\DoxyCodeLine{177 \textcolor{preprocessor}{\#define ilog2(n) \(\backslash\)}}
\DoxyCodeLine{178 \textcolor{preprocessor}{( \(\backslash\)}}
\DoxyCodeLine{179 \textcolor{preprocessor}{        \_\_builtin\_constant\_p(n) ?       \(\backslash\)}}
\DoxyCodeLine{180 \textcolor{preprocessor}{        const\_ilog2(n) :                \(\backslash\)}}
\DoxyCodeLine{181 \textcolor{preprocessor}{        (sizeof(n) <= 4) ?              \(\backslash\)}}
\DoxyCodeLine{182 \textcolor{preprocessor}{        \_\_ilog2\_u32(n) :                \(\backslash\)}}
\DoxyCodeLine{183 \textcolor{preprocessor}{        \_\_ilog2\_u64(n)                  \(\backslash\)}}
\DoxyCodeLine{184 \textcolor{preprocessor}{ )}}

\end{DoxyCode}
