|
| 1 | +/** |
| 2 | + * @file mlib/intencode.h |
| 3 | + * @brief Integer encoding functions |
| 4 | + * @date 2025-01-31 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <mlib/config.h> |
| 23 | +#include <mlib/loop.h> |
| 24 | + |
| 25 | +#include <stdint.h> |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Decode an unsigned 32-bit little-endian integer from a memory buffer |
| 30 | + */ |
| 31 | +static inline uint32_t |
| 32 | +mlib_read_u32le (const void *buf) |
| 33 | +{ |
| 34 | + uint32_t ret = 0; |
| 35 | + if (mlib_is_little_endian ()) { |
| 36 | + // Optimize: The platform uses a LE encoding already |
| 37 | + memcpy (&ret, buf, sizeof ret); |
| 38 | + } else { |
| 39 | + // Portable decode of an LE integer |
| 40 | + const uint8_t *cptr = (const uint8_t *) buf; |
| 41 | + mlib_foreach_urange (i, sizeof ret) { |
| 42 | + ret <<= 8; |
| 43 | + ret |= cptr[i]; |
| 44 | + } |
| 45 | + } |
| 46 | + return ret; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Decode an signed 32-bit little-endian integer from a memory buffer |
| 51 | + */ |
| 52 | +static inline int32_t |
| 53 | +mlib_read_i32le (const void *buf) |
| 54 | +{ |
| 55 | + return (int32_t) mlib_read_u32le (buf); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Decode an unsigned 64-bit little-endian integer from a memory buffer |
| 60 | + */ |
| 61 | +static inline uint64_t |
| 62 | +mlib_read_u64le (const void *buf) |
| 63 | +{ |
| 64 | + uint64_t ret = 0; |
| 65 | + if (mlib_is_little_endian ()) { |
| 66 | + // Optimize: The platform uses a LE encoding already |
| 67 | + memcpy (&ret, buf, sizeof ret); |
| 68 | + } else { |
| 69 | + // Portable decode of an LE integer |
| 70 | + const uint8_t *cptr = (const uint8_t *) buf; |
| 71 | + mlib_foreach_urange (i, sizeof ret) { |
| 72 | + ret <<= 8; |
| 73 | + ret |= cptr[i]; |
| 74 | + } |
| 75 | + } |
| 76 | + return ret; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Decode an signed 64-bit little-endian integer from a memory buffer |
| 81 | + */ |
| 82 | +static inline int64_t |
| 83 | +mlib_read_i64le (const void *buf) |
| 84 | +{ |
| 85 | + return (int64_t) mlib_read_u64le (buf); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Write an unsigned 32-bit little-endian integer into a destination |
| 90 | + * |
| 91 | + * @return void* The address after the written value |
| 92 | + */ |
| 93 | +static inline void * |
| 94 | +mlib_write_u32le (void *out, const uint32_t value) |
| 95 | +{ |
| 96 | + uint8_t *o = (uint8_t *) out; |
| 97 | + if (mlib_is_little_endian ()) { |
| 98 | + memcpy (o, &value, sizeof value); |
| 99 | + return o + sizeof value; |
| 100 | + } |
| 101 | + mlib_foreach_urange (i, sizeof value) { |
| 102 | + *o++ = (value >> (8u * i)) & 0xffu; |
| 103 | + } |
| 104 | + return o; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Write a signed 32-bit little-endian integer into a destination |
| 109 | + * |
| 110 | + * @return void* The address after the written value |
| 111 | + */ |
| 112 | +static inline void * |
| 113 | +mlib_write_i32le (void *out, int32_t value) |
| 114 | +{ |
| 115 | + return mlib_write_u32le (out, (uint32_t) value); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief Write an unsigned 64-bit little-endian integer into a destination |
| 120 | + * |
| 121 | + * @return void* The address after the written value |
| 122 | + */ |
| 123 | +static inline void * |
| 124 | +mlib_write_u64le (void *out, const uint64_t value) |
| 125 | +{ |
| 126 | + uint8_t *o = (uint8_t *) out; |
| 127 | + if (mlib_is_little_endian ()) { |
| 128 | + memcpy (o, &value, sizeof value); |
| 129 | + return o + sizeof value; |
| 130 | + } |
| 131 | + mlib_foreach_urange (i, sizeof value) { |
| 132 | + *o++ = (value >> (8u * i)) & 0xffu; |
| 133 | + } |
| 134 | + return o; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @brief Write an signed 64-bit little-endian integer into a destination |
| 139 | + * |
| 140 | + * @return void* The address after the written value |
| 141 | + */ |
| 142 | +static inline void * |
| 143 | +mlib_write_i64le (void *out, int64_t value) |
| 144 | +{ |
| 145 | + return mlib_write_u64le (out, (uint64_t) value); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * @brief Write a little-endian 64-bit floating point (double) to the given |
| 150 | + * memory location |
| 151 | + * |
| 152 | + * @return void* The address after the written value. |
| 153 | + */ |
| 154 | +static inline void * |
| 155 | +mlib_write_f64le (void *out, double d) |
| 156 | +{ |
| 157 | + mlib_static_assert (sizeof (double) == sizeof (uint64_t)); |
| 158 | + uint64_t bits; |
| 159 | + memcpy (&bits, &d, sizeof d); |
| 160 | + return mlib_write_u64le (out, bits); |
| 161 | +} |
0 commit comments