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