libosmocore 1.13.1.1-afb4 Osmocom core library

bit64gen.h
Go to the documentation of this file.
1/*
2 * bit64gen.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
19#pragma once
20
21#include <osmocom/core/utils.h>
22
28 static inline uint64_t osmo_load64le_ext(const void *p, uint8_t n)
29{
30 uint8_t i;
31 uint64_t r = 0;
32 const uint8_t *q = (uint8_t *)p;
33 OSMO_ASSERT(n <= sizeof(r));
34 for(i = 0; i < n; r |= ((uint64_t)q[i] << (8 * i)), i++);
35 return r;
36}
37
45 static inline uint64_t osmo_load64be_ext(const void *p, uint8_t n)
46{
47 uint8_t i;
48 uint64_t r = 0;
49 const uint8_t *q = (uint8_t *)p;
50 OSMO_ASSERT(n <= sizeof(r));
51 for(i = 0; i < n; r |= ((uint64_t)q[i] << (64 - 8* (1 + i))), i++);
52 return r;
53}
54
60 static inline uint64_t osmo_load64be_ext_2(const void *p, uint8_t n)
61{
62 uint8_t i;
63 uint64_t r = 0;
64 const uint8_t *q = (uint8_t *)p;
65 OSMO_ASSERT(n <= sizeof(r));
66 for(i = 0; i < n; r |= ((uint64_t)q[i] << (64 - 8* (1 + i + (sizeof(r) - n)))), i++);
67 return r;
68}
69
70
76 static inline void osmo_store64le_ext(uint64_t x, void *p, uint8_t n)
77{
78 uint8_t i;
79 uint8_t *q = (uint8_t *)p;
80 OSMO_ASSERT(n <= sizeof(x));
81 for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++);
82}
83
89 static inline void osmo_store64be_ext(uint64_t x, void *p, uint8_t n)
90{
91 uint8_t i;
92 uint8_t *q = (uint8_t *)p;
93 OSMO_ASSERT(n <= sizeof(x));
94 for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++);
95}
96
97
98/* Convenience function for most-used cases */
99
100
102 static inline uint64_t osmo_load64le(const void *p)
103{
104 return osmo_load64le_ext(p, 64 / 8);
105}
106
108 static inline uint64_t osmo_load64be(const void *p)
109{
110 return osmo_load64be_ext(p, 64 / 8);
111}
112
113
115 static inline void osmo_store64le(uint64_t x, void *p)
116{
117 osmo_store64le_ext(x, p, 64 / 8);
118}
119
121 static inline void osmo_store64be(uint64_t x, void *p)
122{
123 osmo_store64be_ext(x, p, 64 / 8);
124}
osmo_load64le_ext
static uint64_t osmo_load64le_ext(const void *p, uint8_t n)
load unaligned n-byte integer (little-endian encoding) into uint64_t, into the least significant octe...
Definition: bit64gen.h:28
osmo_load64le
static uint64_t osmo_load64le(const void *p)
load unaligned 64-bit integer (little-endian encoding)
Definition: bit64gen.h:102
osmo_store64be
static void osmo_store64be(uint64_t x, void *p)
store unaligned 64-bit integer (big-endian encoding)
Definition: bit64gen.h:121
osmo_load64be_ext
static uint64_t osmo_load64be_ext(const void *p, uint8_t n)
load unaligned n-byte integer (big-endian encoding) into uint64_t, into the MOST significant octets.
Definition: bit64gen.h:45
osmo_load64be
static uint64_t osmo_load64be(const void *p)
load unaligned 64-bit integer (big-endian encoding)
Definition: bit64gen.h:108
osmo_store64be_ext
static void osmo_store64be_ext(uint64_t x, void *p, uint8_t n)
store unaligned n-byte integer (big-endian encoding) from uint64_t
Definition: bit64gen.h:89
osmo_load64be_ext_2
static uint64_t osmo_load64be_ext_2(const void *p, uint8_t n)
load unaligned n-byte integer (big-endian encoding) into uint64_t, into the least significant octets.
Definition: bit64gen.h:60
osmo_store64le
static void osmo_store64le(uint64_t x, void *p)
store unaligned 64-bit integer (little-endian encoding)
Definition: bit64gen.h:115
osmo_store64le_ext
static void osmo_store64le_ext(uint64_t x, void *p, uint8_t n)
store unaligned n-byte integer (little-endian encoding) from uint64_t
Definition: bit64gen.h:76
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:113
x
uint32_t x
Definition: jhash.h:0
utils.h