\doxysection{bit16gen.\+h}
\label{bit16gen_8h_source}\index{include/osmocom/core/bit16gen.h@{include/osmocom/core/bit16gen.h}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{1 \textcolor{comment}{/*}}
\DoxyCodeLine{2 \textcolor{comment}{ * bit16gen.h}}
\DoxyCodeLine{3 \textcolor{comment}{ *}}
\DoxyCodeLine{4 \textcolor{comment}{ * Copyright (C) 2014  Max <max.suraev@fairwaves.co>}}
\DoxyCodeLine{5 \textcolor{comment}{ *}}
\DoxyCodeLine{6 \textcolor{comment}{ * All Rights Reserved}}
\DoxyCodeLine{7 \textcolor{comment}{ *}}
\DoxyCodeLine{8 \textcolor{comment}{ * This program is free software; you can redistribute it and/or modify}}
\DoxyCodeLine{9 \textcolor{comment}{ * it under the terms of the GNU General Public License as published by}}
\DoxyCodeLine{10 \textcolor{comment}{ * the Free Software Foundation; either version 2 of the License, or}}
\DoxyCodeLine{11 \textcolor{comment}{ * (at your option) any later version.}}
\DoxyCodeLine{12 \textcolor{comment}{ *}}
\DoxyCodeLine{13 \textcolor{comment}{ * This program is distributed in the hope that it will be useful,}}
\DoxyCodeLine{14 \textcolor{comment}{ * but WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{15 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the}}
\DoxyCodeLine{16 \textcolor{comment}{ * GNU General Public License for more details.}}
\DoxyCodeLine{17 \textcolor{comment}{ */}}
\DoxyCodeLine{18 }
\DoxyCodeLine{19 \textcolor{preprocessor}{\#pragma once}}
\DoxyCodeLine{20 }
\DoxyCodeLine{21 \textcolor{preprocessor}{\#include <osmocom/core/utils.h>}}
\DoxyCodeLine{22 }
\DoxyCodeLine{28 \textcolor{keyword}{static} \textcolor{keyword}{inline} uint16\_t osmo\_load16le\_ext(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *p, uint8\_t n)}
\DoxyCodeLine{29 \{}
\DoxyCodeLine{30         uint8\_t i;}
\DoxyCodeLine{31         uint16\_t r = 0;}
\DoxyCodeLine{32         \textcolor{keyword}{const} uint8\_t *q = (uint8\_t *)p;}
\DoxyCodeLine{33         OSMO\_ASSERT(n <= \textcolor{keyword}{sizeof}(r));}
\DoxyCodeLine{34         \textcolor{keywordflow}{for}(i = 0; i < n; r |= ((uint16\_t)q[i] << (8 * i)), i++);}
\DoxyCodeLine{35         \textcolor{keywordflow}{return} r;}
\DoxyCodeLine{36 \}}
\DoxyCodeLine{37 }
\DoxyCodeLine{45 \textcolor{keyword}{static} \textcolor{keyword}{inline} uint16\_t osmo\_load16be\_ext(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *p, uint8\_t n)}
\DoxyCodeLine{46 \{}
\DoxyCodeLine{47         uint8\_t i;}
\DoxyCodeLine{48         uint16\_t r = 0;}
\DoxyCodeLine{49         \textcolor{keyword}{const} uint8\_t *q = (uint8\_t *)p;}
\DoxyCodeLine{50         OSMO\_ASSERT(n <= \textcolor{keyword}{sizeof}(r));}
\DoxyCodeLine{51         \textcolor{keywordflow}{for}(i = 0; i < n; r |= ((uint16\_t)q[i] << (16 -\/ 8* (1 + i))), i++);}
\DoxyCodeLine{52         \textcolor{keywordflow}{return} r;}
\DoxyCodeLine{53 \}}
\DoxyCodeLine{54 }
\DoxyCodeLine{60 \textcolor{keyword}{static} \textcolor{keyword}{inline} uint16\_t osmo\_load16be\_ext\_2(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *p, uint8\_t n)}
\DoxyCodeLine{61 \{}
\DoxyCodeLine{62         uint8\_t i;}
\DoxyCodeLine{63         uint16\_t r = 0;}
\DoxyCodeLine{64         \textcolor{keyword}{const} uint8\_t *q = (uint8\_t *)p;}
\DoxyCodeLine{65         OSMO\_ASSERT(n <= \textcolor{keyword}{sizeof}(r));}
\DoxyCodeLine{66         \textcolor{keywordflow}{for}(i = 0; i < n; r |= ((uint16\_t)q[i] << (16 -\/ 8* (1 + i + (\textcolor{keyword}{sizeof}(r) -\/ n)))), i++);}
\DoxyCodeLine{67         \textcolor{keywordflow}{return} r;}
\DoxyCodeLine{68 \}}
\DoxyCodeLine{69 }
\DoxyCodeLine{70 }
\DoxyCodeLine{76 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} osmo\_store16le\_ext(uint16\_t x, \textcolor{keywordtype}{void} *p, uint8\_t n)}
\DoxyCodeLine{77 \{}
\DoxyCodeLine{78         uint8\_t i;}
\DoxyCodeLine{79         uint8\_t *q = (uint8\_t *)p;}
\DoxyCodeLine{80         OSMO\_ASSERT(n <= \textcolor{keyword}{sizeof}(x));}
\DoxyCodeLine{81         \textcolor{keywordflow}{for}(i = 0; i < n; q[i] = (x >> i * 8) \& 0xFF, i++);}
\DoxyCodeLine{82 \}}
\DoxyCodeLine{83 }
\DoxyCodeLine{89 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} osmo\_store16be\_ext(uint16\_t x, \textcolor{keywordtype}{void} *p, uint8\_t n)}
\DoxyCodeLine{90 \{}
\DoxyCodeLine{91         uint8\_t i;}
\DoxyCodeLine{92         uint8\_t *q = (uint8\_t *)p;}
\DoxyCodeLine{93         OSMO\_ASSERT(n <= \textcolor{keyword}{sizeof}(x));}
\DoxyCodeLine{94         \textcolor{keywordflow}{for}(i = 0; i < n; q[i] = (x >> ((n -\/ 1 -\/ i) * 8)) \& 0xFF, i++);}
\DoxyCodeLine{95 \}}
\DoxyCodeLine{96 }
\DoxyCodeLine{97 }
\DoxyCodeLine{98 \textcolor{comment}{/* Convenience function for most-\/used cases */}}
\DoxyCodeLine{99 }
\DoxyCodeLine{100 }
\DoxyCodeLine{102 \textcolor{keyword}{static} \textcolor{keyword}{inline} uint16\_t osmo\_load16le(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *p)}
\DoxyCodeLine{103 \{}
\DoxyCodeLine{104         \textcolor{keywordflow}{return} osmo\_load16le\_ext(p, 16 / 8);}
\DoxyCodeLine{105 \}}
\DoxyCodeLine{106 }
\DoxyCodeLine{108 \textcolor{keyword}{static} \textcolor{keyword}{inline} uint16\_t osmo\_load16be(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *p)}
\DoxyCodeLine{109 \{}
\DoxyCodeLine{110         \textcolor{keywordflow}{return} osmo\_load16be\_ext(p, 16 / 8);}
\DoxyCodeLine{111 \}}
\DoxyCodeLine{112 }
\DoxyCodeLine{113 }
\DoxyCodeLine{115 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} osmo\_store16le(uint16\_t x, \textcolor{keywordtype}{void} *p)}
\DoxyCodeLine{116 \{}
\DoxyCodeLine{117         osmo\_store16le\_ext(x, p, 16 / 8);}
\DoxyCodeLine{118 \}}
\DoxyCodeLine{119 }
\DoxyCodeLine{121 \textcolor{keyword}{static} \textcolor{keyword}{inline} \textcolor{keywordtype}{void} osmo\_store16be(uint16\_t x, \textcolor{keywordtype}{void} *p)}
\DoxyCodeLine{122 \{}
\DoxyCodeLine{123         osmo\_store16be\_ext(x, p, 16 / 8);}
\DoxyCodeLine{124 \}}

\end{DoxyCode}
