libosmocore  0.9.3
Osmocom core library
include/osmocom/core/bit64gen.h
00001 /*
00002  * bit64gen.h
00003  *
00004  * Copyright (C) 2014  Max <max.suraev@fairwaves.co>
00005  *
00006  * All Rights Reserved
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License along
00019  * with this program; if not, write to the Free Software Foundation, Inc.,
00020  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021  */
00022 
00023 #pragma once
00024 
00030 static inline uint64_t osmo_load64le_ext(const void *p, uint8_t n)
00031 {
00032         uint8_t i;
00033         uint64_t r = 0;
00034         const uint8_t *q = (uint8_t *)p;
00035         for(i = 0; i < n; r |= ((uint64_t)q[i] << (8 * i)), i++);
00036         return r;
00037 }
00038 
00044 static inline uint64_t osmo_load64be_ext(const void *p, uint8_t n)
00045 {
00046         uint8_t i;
00047         uint64_t r = 0;
00048         const uint8_t *q = (uint8_t *)p;
00049         for(i = 0; i < n; r |= ((uint64_t)q[i] << (64 - 8* (1 + i))), i++);
00050         return r;
00051 }
00052 
00053 
00059 static inline void osmo_store64le_ext(uint64_t x, void *p, uint8_t n)
00060 {
00061         uint8_t i;
00062         uint8_t *q = (uint8_t *)p;
00063         for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++);
00064 }
00065 
00071 static inline void osmo_store64be_ext(uint64_t x, void *p, uint8_t n)
00072 {
00073         uint8_t i;
00074         uint8_t *q = (uint8_t *)p;
00075         for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++);
00076 }
00077 
00078 
00079 /* Convenience function for most-used cases */
00080 
00081 
00083 static inline uint64_t osmo_load64le(const void *p)
00084 {
00085         return osmo_load64le_ext(p, 64 / 8);
00086 }
00087 
00089 static inline uint64_t osmo_load64be(const void *p)
00090 {
00091         return osmo_load64be_ext(p, 64 / 8);
00092 }
00093 
00094 
00096 static inline void osmo_store64le(uint64_t x, void *p)
00097 {
00098         osmo_store64le_ext(x, p, 64 / 8);
00099 }
00100 
00102 static inline void osmo_store64be(uint64_t x, void *p)
00103 {
00104         osmo_store64be_ext(x, p, 64 / 8);
00105 }