1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 // This size report uses either ByteBuilder or manual bytes manipulation
16 // depending on whether "USE_BYTE_BUILDER" is set to true/false.
17 // Building the file: ninja -C out build_me
24 #include "pw_bytes/byte_builder.h"
26 #if !defined(USE_BYTE_BUILDER)
27 #error "USE_BYTE_BUILDER must be defined"
28 #endif // !defined(USE_BYTE_BUILDER)
37 bb.PutUint32(0x482B3D9E);
38 bb.PutInt32(0x482B3D9E, std::endian::big);
44 std::printf("%u\n", static_cast<unsigned>(it.ReadUint32()));
45 std::printf("%d\n", static_cast<int>(it.ReadInt32(std::endian::big)));
48 #else // !USE_BYTE_BUILDER
53 uint32_t kVal1 = 0x482B3D9E;
54 int32_t kVal2 = 0x482B3D9E;
56 if (std::endian::native == std::endian::little) {
57 std::memcpy(b_array, &kVal1, sizeof(kVal1));
59 kVal2 = int32_t(((kVal2 & 0x000000FF) << 3 * 8) | //
60 ((kVal2 & 0x0000FF00) << 1 * 8) | //
61 ((kVal2 & 0x00FF0000) >> 1 * 8) | //
62 ((kVal2 & 0xFF000000) >> 3 * 8));
63 std::memcpy(b_array + 4, &kVal2, sizeof(kVal2));
65 kVal1 = uint32_t(((kVal1 & 0x000000FF) << 3 * 8) | //
66 ((kVal1 & 0x0000FF00) << 1 * 8) | //
67 ((kVal1 & 0x00FF0000) >> 1 * 8) | //
68 ((kVal1 & 0xFF000000) >> 3 * 8));
69 std::memcpy(b_array, &kVal1, sizeof(kVal1));
71 std::memcpy(b_array + 4, &kVal2, sizeof(kVal2));
79 if (std::endian::native == std::endian::little) {
80 std::memcpy(&kVal1, b_array, sizeof(kVal1));
81 std::memcpy(&kVal2, b_array + 4, sizeof(kVal2));
82 kVal2 = int32_t(((kVal2 & 0x000000FF) << 3 * 8) | //
83 ((kVal2 & 0x0000FF00) << 1 * 8) | //
84 ((kVal2 & 0x00FF0000) >> 1 * 8) | //
85 ((kVal2 & 0xFF000000) >> 3 * 8));
87 std::memcpy(&kVal1, b_array, sizeof(kVal1));
88 std::memcpy(&kVal2, b_array + 4, sizeof(kVal2));
90 kVal1 = uint32_t(((kVal1 & 0x000000FF) << 3 * 8) | //
91 ((kVal1 & 0x0000FF00) << 1 * 8) | //
92 ((kVal1 & 0x00FF0000) >> 1 * 8) | //
93 ((kVal1 & 0xFF000000) >> 3 * 8));
96 std::printf("%u\n", static_cast<unsigned>(kVal1));
97 std::printf("%d\n", static_cast<int>(kVal2));
100 #endif // USE_BYTE_BUILDER
102 } // namespace pw::bytes
105 pw::bytes::PutBytes();
106 pw::bytes::ReadBytes();