Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 The Abseil Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |
| 18 | #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |
| 19 | |
| 20 | #include <string.h> |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 21 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 22 | #include <cstdint> |
| 23 | |
| 24 | #include "absl/base/attributes.h" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 25 | #include "absl/base/config.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 26 | |
| 27 | // unaligned APIs |
| 28 | |
| 29 | // Portable handling of unaligned loads, stores, and copies. |
| 30 | |
| 31 | // The unaligned API is C++ only. The declarations use C++ features |
| 32 | // (namespaces, inline) which are absent or incompatible in C. |
| 33 | #if defined(__cplusplus) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 34 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 35 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 36 | namespace base_internal { |
| 37 | |
| 38 | inline uint16_t UnalignedLoad16(const void *p) { |
| 39 | uint16_t t; |
| 40 | memcpy(&t, p, sizeof t); |
| 41 | return t; |
| 42 | } |
| 43 | |
| 44 | inline uint32_t UnalignedLoad32(const void *p) { |
| 45 | uint32_t t; |
| 46 | memcpy(&t, p, sizeof t); |
| 47 | return t; |
| 48 | } |
| 49 | |
| 50 | inline uint64_t UnalignedLoad64(const void *p) { |
| 51 | uint64_t t; |
| 52 | memcpy(&t, p, sizeof t); |
| 53 | return t; |
| 54 | } |
| 55 | |
| 56 | inline void UnalignedStore16(void *p, uint16_t v) { memcpy(p, &v, sizeof v); } |
| 57 | |
| 58 | inline void UnalignedStore32(void *p, uint32_t v) { memcpy(p, &v, sizeof v); } |
| 59 | |
| 60 | inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); } |
| 61 | |
| 62 | } // namespace base_internal |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 63 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 64 | } // namespace absl |
| 65 | |
| 66 | #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \ |
| 67 | (absl::base_internal::UnalignedLoad16(_p)) |
| 68 | #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \ |
| 69 | (absl::base_internal::UnalignedLoad32(_p)) |
| 70 | #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \ |
| 71 | (absl::base_internal::UnalignedLoad64(_p)) |
| 72 | |
| 73 | #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \ |
| 74 | (absl::base_internal::UnalignedStore16(_p, _val)) |
| 75 | #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \ |
| 76 | (absl::base_internal::UnalignedStore32(_p, _val)) |
| 77 | #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \ |
| 78 | (absl::base_internal::UnalignedStore64(_p, _val)) |
| 79 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 80 | #endif // defined(__cplusplus), end of unaligned API |
| 81 | |
| 82 | #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |