Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * bitXXgen.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 | /*! \brief load unaligned n-byte integer (little-endian encoding) into uintXX_t |
| 26 | * \param[in] p Buffer where integer is stored |
| 27 | * \param[in] n Number of bytes stored in p |
| 28 | * \returns XX bit unsigned integer |
| 29 | */ |
| 30 | static inline uintXX_t osmo_loadXXle_ext(const void *p, uint8_t n) |
| 31 | { |
| 32 | uint8_t i; |
| 33 | uintXX_t r = 0; |
| 34 | const uint8_t *q = (uint8_t *)p; |
| 35 | for(i = 0; i < n; r |= ((uintXX_t)q[i] << (8 * i)), i++); |
| 36 | return r; |
| 37 | } |
| 38 | |
| 39 | /*! \brief load unaligned n-byte integer (big-endian encoding) into uintXX_t |
| 40 | * \param[in] p Buffer where integer is stored |
| 41 | * \param[in] n Number of bytes stored in p |
| 42 | * \returns XX bit unsigned integer |
| 43 | */ |
| 44 | static inline uintXX_t osmo_loadXXbe_ext(const void *p, uint8_t n) |
| 45 | { |
| 46 | uint8_t i; |
| 47 | uintXX_t r = 0; |
| 48 | const uint8_t *q = (uint8_t *)p; |
| 49 | for(i = 0; i < n; r |= ((uintXX_t)q[i] << (XX - 8* (1 + i))), i++); |
| 50 | return r; |
| 51 | } |
| 52 | |
| 53 | |
Neels Hofmeyr | c84851b | 2015-10-09 00:01:06 +0200 | [diff] [blame] | 54 | /*! \brief store unaligned n-byte integer (little-endian encoding) from uintXX_t |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 55 | * \param[in] x unsigned XX bit integer |
| 56 | * \param[out] p Buffer to store integer |
| 57 | * \param[in] n Number of bytes to store |
| 58 | */ |
Sylvain Munaut | 3baa0d6 | 2014-06-16 16:38:31 +0200 | [diff] [blame] | 59 | static inline void osmo_storeXXle_ext(uintXX_t x, void *p, uint8_t n) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 60 | { |
| 61 | uint8_t i; |
Sylvain Munaut | 3baa0d6 | 2014-06-16 16:38:31 +0200 | [diff] [blame] | 62 | uint8_t *q = (uint8_t *)p; |
| 63 | for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Neels Hofmeyr | c84851b | 2015-10-09 00:01:06 +0200 | [diff] [blame] | 66 | /*! \brief store unaligned n-byte integer (big-endian encoding) from uintXX_t |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 67 | * \param[in] x unsigned XX bit integer |
| 68 | * \param[out] p Buffer to store integer |
| 69 | * \param[in] n Number of bytes to store |
| 70 | */ |
Sylvain Munaut | 3baa0d6 | 2014-06-16 16:38:31 +0200 | [diff] [blame] | 71 | static inline void osmo_storeXXbe_ext(uintXX_t x, void *p, uint8_t n) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 72 | { |
| 73 | uint8_t i; |
Sylvain Munaut | 3baa0d6 | 2014-06-16 16:38:31 +0200 | [diff] [blame] | 74 | uint8_t *q = (uint8_t *)p; |
| 75 | for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | /* Convenience function for most-used cases */ |
| 80 | |
| 81 | |
| 82 | /*! \brief load unaligned XX-bit integer (little-endian encoding) */ |
| 83 | static inline uintXX_t osmo_loadXXle(const void *p) |
| 84 | { |
| 85 | return osmo_loadXXle_ext(p, XX / 8); |
| 86 | } |
| 87 | |
| 88 | /*! \brief load unaligned XX-bit integer (big-endian encoding) */ |
| 89 | static inline uintXX_t osmo_loadXXbe(const void *p) |
| 90 | { |
| 91 | return osmo_loadXXbe_ext(p, XX / 8); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /*! \brief store unaligned XX-bit integer (little-endian encoding) */ |
| 96 | static inline void osmo_storeXXle(uintXX_t x, void *p) |
| 97 | { |
Sylvain Munaut | 5469ef8 | 2014-06-16 16:39:08 +0200 | [diff] [blame] | 98 | osmo_storeXXle_ext(x, p, XX / 8); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /*! \brief store unaligned XX-bit integer (big-endian encoding) */ |
| 102 | static inline void osmo_storeXXbe(uintXX_t x, void *p) |
| 103 | { |
Sylvain Munaut | 5469ef8 | 2014-06-16 16:39:08 +0200 | [diff] [blame] | 104 | osmo_storeXXbe_ext(x, p, XX / 8); |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 105 | } |