libosmocore  1.5.1
Osmocom core library
bit16gen.h
Go to the documentation of this file.
1 /*
2  * bit16gen.h
3  *
4  * Copyright (C) 2014 Max <max.suraev@fairwaves.co>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #pragma once
24 
25 #include <osmocom/core/utils.h>
26 
32 static inline uint16_t osmo_load16le_ext(const void *p, uint8_t n)
33 {
34  uint8_t i;
35  uint16_t r = 0;
36  const uint8_t *q = (uint8_t *)p;
37  OSMO_ASSERT(n <= sizeof(r));
38  for(i = 0; i < n; r |= ((uint16_t)q[i] << (8 * i)), i++);
39  return r;
40 }
41 
49 static inline uint16_t osmo_load16be_ext(const void *p, uint8_t n)
50 {
51  uint8_t i;
52  uint16_t r = 0;
53  const uint8_t *q = (uint8_t *)p;
54  OSMO_ASSERT(n <= sizeof(r));
55  for(i = 0; i < n; r |= ((uint16_t)q[i] << (16 - 8* (1 + i))), i++);
56  return r;
57 }
58 
64 static inline uint16_t osmo_load16be_ext_2(const void *p, uint8_t n)
65 {
66  uint8_t i;
67  uint16_t r = 0;
68  const uint8_t *q = (uint8_t *)p;
69  OSMO_ASSERT(n <= sizeof(r));
70  for(i = 0; i < n; r |= ((uint16_t)q[i] << (16 - 8* (1 + i + (sizeof(r) - n)))), i++);
71  return r;
72 }
73 
74 
80 static inline void osmo_store16le_ext(uint16_t x, void *p, uint8_t n)
81 {
82  uint8_t i;
83  uint8_t *q = (uint8_t *)p;
84  OSMO_ASSERT(n <= sizeof(x));
85  for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++);
86 }
87 
93 static inline void osmo_store16be_ext(uint16_t x, void *p, uint8_t n)
94 {
95  uint8_t i;
96  uint8_t *q = (uint8_t *)p;
97  OSMO_ASSERT(n <= sizeof(x));
98  for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++);
99 }
100 
101 
102 /* Convenience function for most-used cases */
103 
104 
106 static inline uint16_t osmo_load16le(const void *p)
107 {
108  return osmo_load16le_ext(p, 16 / 8);
109 }
110 
112 static inline uint16_t osmo_load16be(const void *p)
113 {
114  return osmo_load16be_ext(p, 16 / 8);
115 }
116 
117 
119 static inline void osmo_store16le(uint16_t x, void *p)
120 {
121  osmo_store16le_ext(x, p, 16 / 8);
122 }
123 
125 static inline void osmo_store16be(uint16_t x, void *p)
126 {
127  osmo_store16be_ext(x, p, 16 / 8);
128 }
osmo_load16le
static uint16_t osmo_load16le(const void *p)
load unaligned 16-bit integer (little-endian encoding)
Definition: bit16gen.h:106
n
write Write running configuration to or terminal n Write configuration to the copy running config startup Copy configuration n Copy running config to n Copy running config to startup write Write running configuration to or terminal n Write to terminal n
OSMO_ASSERT
#define OSMO_ASSERT(exp)
Helper macro to terminate when an assertion fails.
Definition: utils.h:104
osmo_load16be_ext_2
static uint16_t osmo_load16be_ext_2(const void *p, uint8_t n)
load unaligned n-byte integer (big-endian encoding) into uint16_t, into the least significant octets.
Definition: bit16gen.h:64
osmo_store16be_ext
static void osmo_store16be_ext(uint16_t x, void *p, uint8_t n)
store unaligned n-byte integer (big-endian encoding) from uint16_t
Definition: bit16gen.h:93
osmo_load16be
static uint16_t osmo_load16be(const void *p)
load unaligned 16-bit integer (big-endian encoding)
Definition: bit16gen.h:112
utils.h
osmo_load16le_ext
static uint16_t osmo_load16le_ext(const void *p, uint8_t n)
load unaligned n-byte integer (little-endian encoding) into uint16_t, into the least significant octe...
Definition: bit16gen.h:32
osmo_store16le_ext
static void osmo_store16le_ext(uint16_t x, void *p, uint8_t n)
store unaligned n-byte integer (little-endian encoding) from uint16_t
Definition: bit16gen.h:80
osmo_store16be
static void osmo_store16be(uint16_t x, void *p)
store unaligned 16-bit integer (big-endian encoding)
Definition: bit16gen.h:125
osmo_load16be_ext
static uint16_t osmo_load16be_ext(const void *p, uint8_t n)
load unaligned n-byte integer (big-endian encoding) into uint16_t, into the MOST significant octets.
Definition: bit16gen.h:49
osmo_store16le
static void osmo_store16le(uint16_t x, void *p)
store unaligned 16-bit integer (little-endian encoding)
Definition: bit16gen.h:119