bit64gen.h
Go to the documentation of this file.
/*
* bit64gen.h
*
* Copyright (C) 2014 Max <max.suraev@fairwaves.co>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <osmocom/core/utils.h>
static inline uint64_t osmo_load64le_ext(const void *p, uint8_t n)
{
uint8_t i;
uint64_t r = 0;
const uint8_t *q = (uint8_t *)p;
OSMO_ASSERT(n <= sizeof(r));
for(i = 0; i < n; r |= ((uint64_t)q[i] << (8 * i)), i++);
return r;
}
static inline uint64_t osmo_load64be_ext(const void *p, uint8_t n)
{
uint8_t i;
uint64_t r = 0;
const uint8_t *q = (uint8_t *)p;
OSMO_ASSERT(n <= sizeof(r));
for(i = 0; i < n; r |= ((uint64_t)q[i] << (64 - 8* (1 + i))), i++);
return r;
}
static inline uint64_t osmo_load64be_ext_2(const void *p, uint8_t n)
{
uint8_t i;
uint64_t r = 0;
const uint8_t *q = (uint8_t *)p;
OSMO_ASSERT(n <= sizeof(r));
for(i = 0; i < n; r |= ((uint64_t)q[i] << (64 - 8* (1 + i + (sizeof(r) - n)))), i++);
return r;
}
static inline void osmo_store64le_ext(uint64_t x, void *p, uint8_t n)
{
uint8_t i;
uint8_t *q = (uint8_t *)p;
OSMO_ASSERT(n <= sizeof(x));
for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++);
}
static inline void osmo_store64be_ext(uint64_t x, void *p, uint8_t n)
{
uint8_t i;
uint8_t *q = (uint8_t *)p;
OSMO_ASSERT(n <= sizeof(x));
for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++);
}
/* Convenience function for most-used cases */
static inline uint64_t osmo_load64le(const void *p)
{
return osmo_load64le_ext(p, 64 / 8);
}
static inline uint64_t osmo_load64be(const void *p)
{
return osmo_load64be_ext(p, 64 / 8);
}
static inline void osmo_store64le(uint64_t x, void *p)
{
osmo_store64le_ext(x, p, 64 / 8);
}
static inline void osmo_store64be(uint64_t x, void *p)
{
osmo_store64be_ext(x, p, 64 / 8);
}
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:32
osmo_load64le
static uint64_t osmo_load64le(const void *p)
load unaligned 64-bit integer (little-endian encoding)
Definition: bit64gen.h:106
osmo_store64be
static void osmo_store64be(uint64_t x, void *p)
store unaligned 64-bit integer (big-endian encoding)
Definition: bit64gen.h:125
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:49
osmo_load64be
static uint64_t osmo_load64be(const void *p)
load unaligned 64-bit integer (big-endian encoding)
Definition: bit64gen.h:112
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:93
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:64
osmo_store64le
static void osmo_store64le(uint64_t x, void *p)
store unaligned 64-bit integer (little-endian encoding)
Definition: bit64gen.h:119
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:80
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
utils.h