Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_bytes / size_report / byte_builder_size_report.cc
1 // Copyright 2020 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14
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
18
19 #include <array>
20 #include <bit>
21 #include <cstdint>
22 #include <cstdio>
23
24 #include "pw_bytes/byte_builder.h"
25
26 #if !defined(USE_BYTE_BUILDER)
27 #error "USE_BYTE_BUILDER must be defined"
28 #endif  // !defined(USE_BYTE_BUILDER)
29
30 namespace pw::bytes {
31
32 #if USE_BYTE_BUILDER
33
34 ByteBuffer<8> bb;
35
36 void PutBytes() {
37   bb.PutUint32(0x482B3D9E);
38   bb.PutInt32(0x482B3D9E, std::endian::big);
39 }
40
41 void ReadBytes() {
42   auto it = bb.begin();
43
44   std::printf("%u\n", static_cast<unsigned>(it.ReadUint32()));
45   std::printf("%d\n", static_cast<int>(it.ReadInt32(std::endian::big)));
46 }
47
48 #else  // !USE_BYTE_BUILDER
49
50 std::byte b_array[8];
51
52 void PutBytes() {
53   uint32_t kVal1 = 0x482B3D9E;
54   int32_t kVal2 = 0x482B3D9E;
55
56   if (std::endian::native == std::endian::little) {
57     std::memcpy(b_array, &kVal1, sizeof(kVal1));
58
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));
64   } else {
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));
70
71     std::memcpy(b_array + 4, &kVal2, sizeof(kVal2));
72   }
73 }
74
75 void ReadBytes() {
76   uint32_t kVal1;
77   int32_t kVal2;
78
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));
86   } else {
87     std::memcpy(&kVal1, b_array, sizeof(kVal1));
88     std::memcpy(&kVal2, b_array + 4, sizeof(kVal2));
89
90     kVal1 = uint32_t(((kVal1 & 0x000000FF) << 3 * 8) |  //
91                      ((kVal1 & 0x0000FF00) << 1 * 8) |  //
92                      ((kVal1 & 0x00FF0000) >> 1 * 8) |  //
93                      ((kVal1 & 0xFF000000) >> 3 * 8));
94   }
95
96   std::printf("%u\n", static_cast<unsigned>(kVal1));
97   std::printf("%d\n", static_cast<int>(kVal2));
98 }
99
100 #endif  // USE_BYTE_BUILDER
101
102 }  // namespace pw::bytes
103
104 int main() {
105   pw::bytes::PutBytes();
106   pw::bytes::ReadBytes();
107   return 0;
108 }