v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
byte_conversions.h
Go to the documentation of this file.
1// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Slightly adapted for inclusion in V8.
6// Copyright 2025 the V8 project authors. All rights reserved.
7
8#ifndef V8_BASE_NUMERICS_BYTE_CONVERSIONS_H_
9#define V8_BASE_NUMERICS_BYTE_CONVERSIONS_H_
10
11#include <array>
12#include <bit>
13#include <cstdint>
14#include <cstring>
15#include <span>
16#include <type_traits>
17
18#include "build/build_config.h"
20
21// Chromium only builds and runs on Little Endian machines.
22static_assert(ARCH_CPU_LITTLE_ENDIAN);
23
24namespace v8::base {
25
26// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
27// TODO(pkasting): Once C++23 is available, replace with std::byteswap.
28template <class T>
29 requires(std::is_integral_v<T>)
30inline constexpr T ByteSwap(T value) {
31 return internal::SwapBytes(value);
32}
33
34// Returns a uint8_t with the value in `bytes` interpreted as the native endian
35// encoding of the integer for the machine.
36//
37// This is suitable for decoding integers that were always kept in native
38// encoding, such as when stored in shared-memory (or through IPC) as a byte
39// buffer. Prefer an explicit little endian when storing and reading data from
40// storage, and explicit big endian for network order.
41//
42// Note that since a single byte can have only one ordering, this just copies
43// the byte out of the span. This provides a consistent function for the
44// operation nonetheless.
45inline constexpr uint8_t U8FromNativeEndian(
46 std::span<const uint8_t, 1u> bytes) {
47 return bytes[0];
48}
49// Returns a uint16_t with the value in `bytes` interpreted as the native endian
50// encoding of the integer for the machine.
51//
52// This is suitable for decoding integers that were always kept in native
53// encoding, such as when stored in shared-memory (or through IPC) as a byte
54// buffer. Prefer an explicit little endian when storing and reading data from
55// storage, and explicit big endian for network order.
56inline constexpr uint16_t U16FromNativeEndian(
57 std::span<const uint8_t, 2u> bytes) {
59}
60// Returns a uint32_t with the value in `bytes` interpreted as the native endian
61// encoding of the integer for the machine.
62//
63// This is suitable for decoding integers that were always kept in native
64// encoding, such as when stored in shared-memory (or through IPC) as a byte
65// buffer. Prefer an explicit little endian when storing and reading data from
66// storage, and explicit big endian for network order.
67inline constexpr uint32_t U32FromNativeEndian(
68 std::span<const uint8_t, 4u> bytes) {
70}
71// Returns a uint64_t with the value in `bytes` interpreted as the native endian
72// encoding of the integer for the machine.
73//
74// This is suitable for decoding integers that were always kept in native
75// encoding, such as when stored in shared-memory (or through IPC) as a byte
76// buffer. Prefer an explicit little endian when storing and reading data from
77// storage, and explicit big endian for network order.
78inline constexpr uint64_t U64FromNativeEndian(
79 std::span<const uint8_t, 8u> bytes) {
81}
82// Returns a int8_t with the value in `bytes` interpreted as the native endian
83// encoding of the integer for the machine.
84//
85// This is suitable for decoding integers that were always kept in native
86// encoding, such as when stored in shared-memory (or through IPC) as a byte
87// buffer. Prefer an explicit little endian when storing and reading data from
88// storage, and explicit big endian for network order.
89//
90// Note that since a single byte can have only one ordering, this just copies
91// the byte out of the span. This provides a consistent function for the
92// operation nonetheless.
93inline constexpr int8_t I8FromNativeEndian(std::span<const uint8_t, 1u> bytes) {
94 return static_cast<int8_t>(bytes[0]);
95}
96// Returns a int16_t with the value in `bytes` interpreted as the native endian
97// encoding of the integer for the machine.
98//
99// This is suitable for decoding integers that were always kept in native
100// encoding, such as when stored in shared-memory (or through IPC) as a byte
101// buffer. Prefer an explicit little endian when storing and reading data from
102// storage, and explicit big endian for network order.
103inline constexpr int16_t I16FromNativeEndian(
104 std::span<const uint8_t, 2u> bytes) {
106}
107// Returns a int32_t with the value in `bytes` interpreted as the native endian
108// encoding of the integer for the machine.
109//
110// This is suitable for decoding integers that were always kept in native
111// encoding, such as when stored in shared-memory (or through IPC) as a byte
112// buffer. Prefer an explicit little endian when storing and reading data from
113// storage, and explicit big endian for network order.
114inline constexpr int32_t I32FromNativeEndian(
115 std::span<const uint8_t, 4u> bytes) {
117}
118// Returns a int64_t with the value in `bytes` interpreted as the native endian
119// encoding of the integer for the machine.
120//
121// This is suitable for decoding integers that were always kept in native
122// encoding, such as when stored in shared-memory (or through IPC) as a byte
123// buffer. Prefer an explicit little endian when storing and reading data from
124// storage, and explicit big endian for network order.
125inline constexpr int64_t I64FromNativeEndian(
126 std::span<const uint8_t, 8u> bytes) {
128}
129
130// Returns a float with the value in `bytes` interpreted as the native endian
131// encoding of the number for the machine.
132//
133// This is suitable for decoding numbers that were always kept in native
134// encoding, such as when stored in shared-memory (or through IPC) as a byte
135// buffer. Prefer an explicit little endian when storing and reading data from
136// storage, and explicit big endian for network order.
137inline constexpr float FloatFromNativeEndian(
138 std::span<const uint8_t, 4u> bytes) {
139 return std::bit_cast<float>(U32FromNativeEndian(bytes));
140}
141// Returns a double with the value in `bytes` interpreted as the native endian
142// encoding of the number for the machine.
143//
144// This is suitable for decoding numbers that were always kept in native
145// encoding, such as when stored in shared-memory (or through IPC) as a byte
146// buffer. Prefer an explicit little endian when storing and reading data from
147// storage, and explicit big endian for network order.
148inline constexpr double DoubleFromNativeEndian(
149 std::span<const uint8_t, 8u> bytes) {
150 return std::bit_cast<double>(U64FromNativeEndian(bytes));
151}
152
153// Returns a uint8_t with the value in `bytes` interpreted as a little-endian
154// encoding of the integer.
155//
156// This is suitable for decoding integers encoded explicitly in little endian,
157// which is a good practice with storing and reading data from storage. Use
158// the native-endian versions when working with values that were always in
159// memory, such as when stored in shared-memory (or through IPC) as a byte
160// buffer.
161//
162// Note that since a single byte can have only one ordering, this just copies
163// the byte out of the span. This provides a consistent function for the
164// operation nonetheless.
165inline constexpr uint8_t U8FromLittleEndian(
166 std::span<const uint8_t, 1u> bytes) {
167 return bytes[0];
168}
169// Returns a uint16_t with the value in `bytes` interpreted as a little-endian
170// encoding of the integer.
171//
172// This is suitable for decoding integers encoded explicitly in little endian,
173// which is a good practice with storing and reading data from storage. Use
174// the native-endian versions when working with values that were always in
175// memory, such as when stored in shared-memory (or through IPC) as a byte
176// buffer.
177inline constexpr uint16_t U16FromLittleEndian(
178 std::span<const uint8_t, 2u> bytes) {
180}
181// Returns a uint32_t with the value in `bytes` interpreted as a little-endian
182// encoding of the integer.
183//
184// This is suitable for decoding integers encoded explicitly in little endian,
185// which is a good practice with storing and reading data from storage. Use
186// the native-endian versions when working with values that were always in
187// memory, such as when stored in shared-memory (or through IPC) as a byte
188// buffer.
189inline constexpr uint32_t U32FromLittleEndian(
190 std::span<const uint8_t, 4u> bytes) {
192}
193// Returns a uint64_t with the value in `bytes` interpreted as a little-endian
194// encoding of the integer.
195//
196// This is suitable for decoding integers encoded explicitly in little endian,
197// which is a good practice with storing and reading data from storage. Use
198// the native-endian versions when working with values that were always in
199// memory, such as when stored in shared-memory (or through IPC) as a byte
200// buffer.
201inline constexpr uint64_t U64FromLittleEndian(
202 std::span<const uint8_t, 8u> bytes) {
204}
205// Returns a int8_t with the value in `bytes` interpreted as a little-endian
206// encoding of the integer.
207//
208// This is suitable for decoding integers encoded explicitly in little endian,
209// which is a good practice with storing and reading data from storage. Use
210// the native-endian versions when working with values that were always in
211// memory, such as when stored in shared-memory (or through IPC) as a byte
212// buffer.
213//
214// Note that since a single byte can have only one ordering, this just copies
215// the byte out of the span. This provides a consistent function for the
216// operation nonetheless.
217inline constexpr int8_t I8FromLittleEndian(std::span<const uint8_t, 1u> bytes) {
218 return static_cast<int8_t>(bytes[0]);
219}
220// Returns a int16_t with the value in `bytes` interpreted as a little-endian
221// encoding of the integer.
222//
223// This is suitable for decoding integers encoded explicitly in little endian,
224// which is a good practice with storing and reading data from storage. Use
225// the native-endian versions when working with values that were always in
226// memory, such as when stored in shared-memory (or through IPC) as a byte
227// buffer.
228inline constexpr int16_t I16FromLittleEndian(
229 std::span<const uint8_t, 2u> bytes) {
231}
232// Returns a int32_t with the value in `bytes` interpreted as a little-endian
233// encoding of the integer.
234//
235// This is suitable for decoding integers encoded explicitly in little endian,
236// which is a good practice with storing and reading data from storage. Use
237// the native-endian versions when working with values that were always in
238// memory, such as when stored in shared-memory (or through IPC) as a byte
239// buffer.
240inline constexpr int32_t I32FromLittleEndian(
241 std::span<const uint8_t, 4u> bytes) {
243}
244// Returns a int64_t with the value in `bytes` interpreted as a little-endian
245// encoding of the integer.
246//
247// This is suitable for decoding integers encoded explicitly in little endian,
248// which is a good practice with storing and reading data from storage. Use
249// the native-endian versions when working with values that were always in
250// memory, such as when stored in shared-memory (or through IPC) as a byte
251// buffer.
252inline constexpr int64_t I64FromLittleEndian(
253 std::span<const uint8_t, 8u> bytes) {
255}
256// Returns a float with the value in `bytes` interpreted as a little-endian
257// encoding of the integer.
258//
259// This is suitable for decoding numbers encoded explicitly in little endian,
260// which is a good practice with storing and reading data from storage. Use
261// the native-endian versions when working with values that were always in
262// memory, such as when stored in shared-memory (or through IPC) as a byte
263// buffer.
264inline constexpr float FloatFromLittleEndian(
265 std::span<const uint8_t, 4u> bytes) {
266 return std::bit_cast<float>(U32FromLittleEndian(bytes));
267}
268// Returns a double with the value in `bytes` interpreted as a little-endian
269// encoding of the integer.
270//
271// This is suitable for decoding numbers encoded explicitly in little endian,
272// which is a good practice with storing and reading data from storage. Use
273// the native-endian versions when working with values that were always in
274// memory, such as when stored in shared-memory (or through IPC) as a byte
275// buffer.
276inline constexpr double DoubleFromLittleEndian(
277 std::span<const uint8_t, 8u> bytes) {
278 return std::bit_cast<double>(U64FromLittleEndian(bytes));
279}
280
281// Returns a uint8_t with the value in `bytes` interpreted as a big-endian
282// encoding of the integer.
283//
284// This is suitable for decoding integers encoded explicitly in big endian, such
285// as for network order. Use the native-endian versions when working with values
286// that were always in memory, such as when stored in shared-memory (or through
287// IPC) as a byte buffer.
288//
289// Note that since a single byte can have only one ordering, this just copies
290// the byte out of the span. This provides a consistent function for the
291// operation nonetheless.
292inline constexpr uint8_t U8FromBigEndian(std::span<const uint8_t, 1u> bytes) {
293 return bytes[0];
294}
295// Returns a uint16_t with the value in `bytes` interpreted as a big-endian
296// encoding of the integer.
297//
298// This is suitable for decoding integers encoded explicitly in big endian, such
299// as for network order. Use the native-endian versions when working with values
300// that were always in memory, such as when stored in shared-memory (or through
301// IPC) as a byte buffer.
302inline constexpr uint16_t U16FromBigEndian(std::span<const uint8_t, 2u> bytes) {
304}
305// Returns a uint32_t with the value in `bytes` interpreted as a big-endian
306// encoding of the integer.
307//
308// This is suitable for decoding integers encoded explicitly in big endian, such
309// as for network order. Use the native-endian versions when working with values
310// that were always in memory, such as when stored in shared-memory (or through
311// IPC) as a byte buffer.
312inline constexpr uint32_t U32FromBigEndian(std::span<const uint8_t, 4u> bytes) {
314}
315// Returns a uint64_t with the value in `bytes` interpreted as a big-endian
316// encoding of the integer.
317//
318// This is suitable for decoding integers encoded explicitly in big endian, such
319// as for network order. Use the native-endian versions when working with values
320// that were always in memory, such as when stored in shared-memory (or through
321// IPC) as a byte buffer.
322inline constexpr uint64_t U64FromBigEndian(std::span<const uint8_t, 8u> bytes) {
324}
325// Returns a int8_t with the value in `bytes` interpreted as a big-endian
326// encoding of the integer.
327//
328// This is suitable for decoding integers encoded explicitly in big endian, such
329// as for network order. Use the native-endian versions when working with values
330// that were always in memory, such as when stored in shared-memory (or through
331// IPC) as a byte buffer.
332//
333// Note that since a single byte can have only one ordering, this just copies
334// the byte out of the span. This provides a consistent function for the
335// operation nonetheless.
336inline constexpr int8_t I8FromBigEndian(std::span<const uint8_t, 1u> bytes) {
337 return static_cast<int8_t>(bytes[0]);
338}
339// Returns a int16_t with the value in `bytes` interpreted as a big-endian
340// encoding of the integer.
341//
342// This is suitable for decoding integers encoded explicitly in big endian, such
343// as for network order. Use the native-endian versions when working with values
344// that were always in memory, such as when stored in shared-memory (or through
345// IPC) as a byte buffer.
346inline constexpr int16_t I16FromBigEndian(std::span<const uint8_t, 2u> bytes) {
348}
349// Returns a int32_t with the value in `bytes` interpreted as a big-endian
350// encoding of the integer.
351//
352// This is suitable for decoding integers encoded explicitly in big endian, such
353// as for network order. Use the native-endian versions when working with values
354// that were always in memory, such as when stored in shared-memory (or through
355// IPC) as a byte buffer.
356inline constexpr int32_t I32FromBigEndian(std::span<const uint8_t, 4u> bytes) {
358}
359// Returns a int64_t with the value in `bytes` interpreted as a big-endian
360// encoding of the integer.
361//
362// This is suitable for decoding integers encoded explicitly in big endian, such
363// as for network order. Use the native-endian versions when working with values
364// that were always in memory, such as when stored in shared-memory (or through
365// IPC) as a byte buffer.
366inline constexpr int64_t I64FromBigEndian(std::span<const uint8_t, 8u> bytes) {
368}
369// Returns a float with the value in `bytes` interpreted as a big-endian
370// encoding of the integer.
371//
372// This is suitable for decoding numbers encoded explicitly in big endian, such
373// as for network order. Use the native-endian versions when working with values
374// that were always in memory, such as when stored in shared-memory (or through
375// IPC) as a byte buffer.
376inline constexpr float FloatFromBigEndian(std::span<const uint8_t, 4u> bytes) {
377 return std::bit_cast<float>(U32FromBigEndian(bytes));
378}
379// Returns a double with the value in `bytes` interpreted as a big-endian
380// encoding of the integer.
381//
382// This is suitable for decoding numbers encoded explicitly in big endian, such
383// as for network order. Use the native-endian versions when working with values
384// that were always in memory, such as when stored in shared-memory (or through
385// IPC) as a byte buffer.
386inline constexpr double DoubleFromBigEndian(
387 std::span<const uint8_t, 8u> bytes) {
388 return std::bit_cast<double>(U64FromBigEndian(bytes));
389}
390
391// Returns a byte array holding the value of a uint8_t encoded as the native
392// endian encoding of the integer for the machine.
393//
394// This is suitable for encoding integers that will always be kept in native
395// encoding, such as for storing in shared-memory (or sending through IPC) as a
396// byte buffer. Prefer an explicit little endian when storing data into external
397// storage, and explicit big endian for network order.
398inline constexpr std::array<uint8_t, 1u> U8ToNativeEndian(uint8_t val) {
399 return {val};
400}
401// Returns a byte array holding the value of a uint16_t encoded as the native
402// endian encoding of the integer for the machine.
403//
404// This is suitable for encoding integers that will always be kept in native
405// encoding, such as for storing in shared-memory (or sending through IPC) as a
406// byte buffer. Prefer an explicit little endian when storing data into external
407// storage, and explicit big endian for network order.
408inline constexpr std::array<uint8_t, 2u> U16ToNativeEndian(uint16_t val) {
409 return internal::ToLittleEndian(val);
410}
411// Returns a byte array holding the value of a uint32_t encoded as the native
412// endian encoding of the integer for the machine.
413//
414// This is suitable for encoding integers that will always be kept in native
415// encoding, such as for storing in shared-memory (or sending through IPC) as a
416// byte buffer. Prefer an explicit little endian when storing data into external
417// storage, and explicit big endian for network order.
418inline constexpr std::array<uint8_t, 4u> U32ToNativeEndian(uint32_t val) {
419 return internal::ToLittleEndian(val);
420}
421// Returns a byte array holding the value of a uint64_t encoded as the native
422// endian encoding of the integer for the machine.
423//
424// This is suitable for encoding integers that will always be kept in native
425// encoding, such as for storing in shared-memory (or sending through IPC) as a
426// byte buffer. Prefer an explicit little endian when storing data into external
427// storage, and explicit big endian for network order.
428inline constexpr std::array<uint8_t, 8u> U64ToNativeEndian(uint64_t val) {
429 return internal::ToLittleEndian(val);
430}
431// Returns a byte array holding the value of a int8_t encoded as the native
432// endian encoding of the integer for the machine.
433//
434// This is suitable for encoding integers that will always be kept in native
435// encoding, such as for storing in shared-memory (or sending through IPC) as a
436// byte buffer. Prefer an explicit little endian when storing data into external
437// storage, and explicit big endian for network order.
438inline constexpr std::array<uint8_t, 1u> I8ToNativeEndian(int8_t val) {
439 return {static_cast<uint8_t>(val)};
440}
441// Returns a byte array holding the value of a int16_t encoded as the native
442// endian encoding of the integer for the machine.
443//
444// This is suitable for encoding integers that will always be kept in native
445// encoding, such as for storing in shared-memory (or sending through IPC) as a
446// byte buffer. Prefer an explicit little endian when storing data into external
447// storage, and explicit big endian for network order.
448inline constexpr std::array<uint8_t, 2u> I16ToNativeEndian(int16_t val) {
449 return internal::ToLittleEndian(val);
450}
451// Returns a byte array holding the value of a int32_t encoded as the native
452// endian encoding of the integer for the machine.
453//
454// This is suitable for encoding integers that will always be kept in native
455// encoding, such as for storing in shared-memory (or sending through IPC) as a
456// byte buffer. Prefer an explicit little endian when storing data into external
457// storage, and explicit big endian for network order.
458inline constexpr std::array<uint8_t, 4u> I32ToNativeEndian(int32_t val) {
459 return internal::ToLittleEndian(val);
460}
461// Returns a byte array holding the value of a int64_t encoded as the native
462// endian encoding of the integer for the machine.
463//
464// This is suitable for encoding integers that will always be kept in native
465// encoding, such as for storing in shared-memory (or sending through IPC) as a
466// byte buffer. Prefer an explicit little endian when storing data into external
467// storage, and explicit big endian for network order.
468inline constexpr std::array<uint8_t, 8u> I64ToNativeEndian(int64_t val) {
469 return internal::ToLittleEndian(val);
470}
471// Returns a byte array holding the value of a float encoded as the native
472// endian encoding of the number for the machine.
473//
474// This is suitable for encoding numbers that will always be kept in native
475// encoding, such as for storing in shared-memory (or sending through IPC) as a
476// byte buffer. Prefer an explicit little endian when storing data into external
477// storage, and explicit big endian for network order.
478inline constexpr std::array<uint8_t, 4u> FloatToNativeEndian(float val) {
479 return U32ToNativeEndian(std::bit_cast<uint32_t>(val));
480}
481// Returns a byte array holding the value of a double encoded as the native
482// endian encoding of the number for the machine.
483//
484// This is suitable for encoding numbers that will always be kept in native
485// encoding, such as for storing in shared-memory (or sending through IPC) as a
486// byte buffer. Prefer an explicit little endian when storing data into external
487// storage, and explicit big endian for network order.
488inline constexpr std::array<uint8_t, 8u> DoubleToNativeEndian(double val) {
489 return U64ToNativeEndian(std::bit_cast<uint64_t>(val));
490}
491
492// Returns a byte array holding the value of a uint8_t encoded as the
493// little-endian encoding of the integer.
494//
495// This is suitable for encoding integers explicitly in little endian, which is
496// a good practice with storing and reading data from storage. Use the
497// native-endian versions when working with values that will always be in
498// memory, such as when stored in shared-memory (or passed through IPC) as a
499// byte buffer.
500inline constexpr std::array<uint8_t, 1u> U8ToLittleEndian(uint8_t val) {
501 return {val};
502}
503// Returns a byte array holding the value of a uint16_t encoded as the
504// little-endian encoding of the integer.
505//
506// This is suitable for encoding integers explicitly in little endian, which is
507// a good practice with storing and reading data from storage. Use the
508// native-endian versions when working with values that will always be in
509// memory, such as when stored in shared-memory (or passed through IPC) as a
510// byte buffer.
511inline constexpr std::array<uint8_t, 2u> U16ToLittleEndian(uint16_t val) {
512 return internal::ToLittleEndian(val);
513}
514// Returns a byte array holding the value of a uint32_t encoded as the
515// little-endian encoding of the integer.
516//
517// This is suitable for encoding integers explicitly in little endian, which is
518// a good practice with storing and reading data from storage. Use the
519// native-endian versions when working with values that will always be in
520// memory, such as when stored in shared-memory (or passed through IPC) as a
521// byte buffer.
522inline constexpr std::array<uint8_t, 4u> U32ToLittleEndian(uint32_t val) {
523 return internal::ToLittleEndian(val);
524}
525// Returns a byte array holding the value of a uint64_t encoded as the
526// little-endian encoding of the integer.
527//
528// This is suitable for encoding integers explicitly in little endian, which is
529// a good practice with storing and reading data from storage. Use the
530// native-endian versions when working with values that will always be in
531// memory, such as when stored in shared-memory (or passed through IPC) as a
532// byte buffer.
533inline constexpr std::array<uint8_t, 8u> U64ToLittleEndian(uint64_t val) {
534 return internal::ToLittleEndian(val);
535}
536// Returns a byte array holding the value of a int8_t encoded as the
537// little-endian encoding of the integer.
538//
539// This is suitable for encoding integers explicitly in little endian, which is
540// a good practice with storing and reading data from storage. Use the
541// native-endian versions when working with values that will always be in
542// memory, such as when stored in shared-memory (or passed through IPC) as a
543// byte buffer.
544inline constexpr std::array<uint8_t, 1u> I8ToLittleEndian(int8_t val) {
545 return {static_cast<uint8_t>(val)};
546}
547// Returns a byte array holding the value of a int16_t encoded as the
548// little-endian encoding of the integer.
549//
550// This is suitable for encoding integers explicitly in little endian, which is
551// a good practice with storing and reading data from storage. Use the
552// native-endian versions when working with values that will always be in
553// memory, such as when stored in shared-memory (or passed through IPC) as a
554// byte buffer.
555inline constexpr std::array<uint8_t, 2u> I16ToLittleEndian(int16_t val) {
556 return internal::ToLittleEndian(val);
557}
558// Returns a byte array holding the value of a int32_t encoded as the
559// little-endian encoding of the integer.
560//
561// This is suitable for encoding integers explicitly in little endian, which is
562// a good practice with storing and reading data from storage. Use the
563// native-endian versions when working with values that will always be in
564// memory, such as when stored in shared-memory (or passed through IPC) as a
565// byte buffer.
566inline constexpr std::array<uint8_t, 4u> I32ToLittleEndian(int32_t val) {
567 return internal::ToLittleEndian(val);
568}
569// Returns a byte array holding the value of a int64_t encoded as the
570// little-endian encoding of the integer.
571//
572// This is suitable for encoding integers explicitly in little endian, which is
573// a good practice with storing and reading data from storage. Use the
574// native-endian versions when working with values that will always be in
575// memory, such as when stored in shared-memory (or passed through IPC) as a
576// byte buffer.
577inline constexpr std::array<uint8_t, 8u> I64ToLittleEndian(int64_t val) {
578 return internal::ToLittleEndian(val);
579}
580// Returns a byte array holding the value of a float encoded as the
581// little-endian encoding of the number.
582//
583// This is suitable for encoding numbers explicitly in little endian, which is
584// a good practice with storing and reading data from storage. Use the
585// native-endian versions when working with values that will always be in
586// memory, such as when stored in shared-memory (or passed through IPC) as a
587// byte buffer.
588inline constexpr std::array<uint8_t, 4u> FloatToLittleEndian(float val) {
589 return internal::ToLittleEndian(std::bit_cast<uint32_t>(val));
590}
591// Returns a byte array holding the value of a double encoded as the
592// little-endian encoding of the number.
593//
594// This is suitable for encoding numbers explicitly in little endian, which is
595// a good practice with storing and reading data from storage. Use the
596// native-endian versions when working with values that will always be in
597// memory, such as when stored in shared-memory (or passed through IPC) as a
598// byte buffer.
599inline constexpr std::array<uint8_t, 8u> DoubleToLittleEndian(double val) {
600 return internal::ToLittleEndian(std::bit_cast<uint64_t>(val));
601}
602
603// Returns a byte array holding the value of a uint8_t encoded as the big-endian
604// encoding of the integer.
605//
606// This is suitable for encoding integers explicitly in big endian, such as for
607// network order. Use the native-endian versions when working with values that
608// are always in memory, such as when stored in shared-memory (or passed through
609// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
610// from storage.
611inline constexpr std::array<uint8_t, 1u> U8ToBigEndian(uint8_t val) {
612 return {val};
613}
614// Returns a byte array holding the value of a uint16_t encoded as the
615// big-endian encoding of the integer.
616//
617// This is suitable for encoding integers explicitly in big endian, such as for
618// network order. Use the native-endian versions when working with values that
619// are always in memory, such as when stored in shared-memory (or passed through
620// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
621// from storage.
622inline constexpr std::array<uint8_t, 2u> U16ToBigEndian(uint16_t val) {
624}
625// Returns a byte array holding the value of a uint32_t encoded as the
626// big-endian encoding of the integer.
627//
628// This is suitable for encoding integers explicitly in big endian, such as for
629// network order. Use the native-endian versions when working with values that
630// are always in memory, such as when stored in shared-memory (or passed through
631// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
632// from storage.
633inline constexpr std::array<uint8_t, 4u> U32ToBigEndian(uint32_t val) {
635}
636// Returns a byte array holding the value of a uint64_t encoded as the
637// big-endian encoding of the integer.
638//
639// This is suitable for encoding integers explicitly in big endian, such as for
640// network order. Use the native-endian versions when working with values that
641// are always in memory, such as when stored in shared-memory (or passed through
642// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
643// from storage.
644inline constexpr std::array<uint8_t, 8u> U64ToBigEndian(uint64_t val) {
646}
647// Returns a byte array holding the value of a int8_t encoded as the big-endian
648// encoding of the integer.
649//
650// This is suitable for encoding integers explicitly in big endian, such as for
651// network order. Use the native-endian versions when working with values that
652// are always in memory, such as when stored in shared-memory (or passed through
653// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
654// from storage.
655inline constexpr std::array<uint8_t, 1u> I8ToBigEndian(int8_t val) {
656 return {static_cast<uint8_t>(val)};
657}
658// Returns a byte array holding the value of a int16_t encoded as the
659// big-endian encoding of the integer.
660//
661// This is suitable for encoding integers explicitly in big endian, such as for
662// network order. Use the native-endian versions when working with values that
663// are always in memory, such as when stored in shared-memory (or passed through
664// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
665// from storage.
666inline constexpr std::array<uint8_t, 2u> I16ToBigEndian(int16_t val) {
668}
669// Returns a byte array holding the value of a int32_t encoded as the
670// big-endian encoding of the integer.
671//
672// This is suitable for encoding integers explicitly in big endian, such as for
673// network order. Use the native-endian versions when working with values that
674// are always in memory, such as when stored in shared-memory (or passed through
675// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
676// from storage.
677inline constexpr std::array<uint8_t, 4u> I32ToBigEndian(int32_t val) {
679}
680// Returns a byte array holding the value of a int64_t encoded as the
681// big-endian encoding of the integer.
682//
683// This is suitable for encoding integers explicitly in big endian, such as for
684// network order. Use the native-endian versions when working with values that
685// are always in memory, such as when stored in shared-memory (or passed through
686// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
687// from storage.
688inline constexpr std::array<uint8_t, 8u> I64ToBigEndian(int64_t val) {
690}
691// Returns a byte array holding the value of a float encoded as the big-endian
692// encoding of the number.
693//
694// This is suitable for encoding numbers explicitly in big endian, such as for
695// network order. Use the native-endian versions when working with values that
696// are always in memory, such as when stored in shared-memory (or passed through
697// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
698// from storage.
699inline constexpr std::array<uint8_t, 4u> FloatToBigEndian(float val) {
700 return internal::ToLittleEndian(ByteSwap(std::bit_cast<uint32_t>(val)));
701}
702// Returns a byte array holding the value of a double encoded as the big-endian
703// encoding of the number.
704//
705// This is suitable for encoding numbers explicitly in big endian, such as for
706// network order. Use the native-endian versions when working with values that
707// are always in memory, such as when stored in shared-memory (or passed through
708// IPC) as a byte buffer. Use the little-endian encoding for storing and reading
709// from storage.
710inline constexpr std::array<uint8_t, 8u> DoubleToBigEndian(double val) {
711 return internal::ToLittleEndian(ByteSwap(std::bit_cast<uint64_t>(val)));
712}
713
714} // namespace v8::base
715
716#endif // V8_BASE_NUMERICS_BYTE_CONVERSIONS_H_
constexpr T SwapBytes(T value)
constexpr T FromLittleEndian(std::span< const uint8_t, sizeof(T)> bytes)
constexpr std::array< uint8_t, sizeof(T)> ToLittleEndian(T val)
constexpr uint8_t U8FromLittleEndian(std::span< const uint8_t, 1u > bytes)
constexpr float FloatFromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr uint8_t U8FromNativeEndian(std::span< const uint8_t, 1u > bytes)
constexpr uint32_t U32FromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr double DoubleFromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 4u > FloatToLittleEndian(float val)
constexpr uint16_t U16FromLittleEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > FloatToBigEndian(float val)
constexpr int32_t I32FromNativeEndian(std::span< const uint8_t, 4u > bytes)
constexpr std::array< uint8_t, 4u > U32ToBigEndian(uint32_t val)
constexpr uint64_t U64FromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > DoubleToBigEndian(double val)
constexpr std::array< uint8_t, 2u > U16ToNativeEndian(uint16_t val)
constexpr std::array< uint8_t, 4u > U32ToNativeEndian(uint32_t val)
constexpr int32_t I32FromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr uint64_t U64FromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr int64_t I64FromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > U64ToNativeEndian(uint64_t val)
constexpr std::array< uint8_t, 1u > I8ToBigEndian(int8_t val)
constexpr uint32_t U32FromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr int16_t I16FromLittleEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 1u > U8ToNativeEndian(uint8_t val)
constexpr std::array< uint8_t, 2u > I16ToBigEndian(int16_t val)
constexpr std::array< uint8_t, 1u > I8ToNativeEndian(int8_t val)
constexpr T ByteSwap(T value)
constexpr int64_t I64FromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 1u > I8ToLittleEndian(int8_t val)
constexpr std::array< uint8_t, 8u > U64ToBigEndian(uint64_t val)
constexpr std::array< uint8_t, 4u > U32ToLittleEndian(uint32_t val)
constexpr std::array< uint8_t, 1u > U8ToBigEndian(uint8_t val)
constexpr int16_t I16FromBigEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > I32ToBigEndian(int32_t val)
constexpr std::array< uint8_t, 4u > FloatToNativeEndian(float val)
constexpr std::array< uint8_t, 2u > I16ToNativeEndian(int16_t val)
constexpr std::array< uint8_t, 8u > DoubleToNativeEndian(double val)
constexpr std::array< uint8_t, 1u > U8ToLittleEndian(uint8_t val)
constexpr int8_t I8FromLittleEndian(std::span< const uint8_t, 1u > bytes)
constexpr std::array< uint8_t, 8u > I64ToBigEndian(int64_t val)
constexpr float FloatFromNativeEndian(std::span< const uint8_t, 4u > bytes)
constexpr int16_t I16FromNativeEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 8u > I64ToNativeEndian(int64_t val)
constexpr int8_t I8FromNativeEndian(std::span< const uint8_t, 1u > bytes)
constexpr uint64_t U64FromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 8u > U64ToLittleEndian(uint64_t val)
constexpr double DoubleFromBigEndian(std::span< const uint8_t, 8u > bytes)
constexpr uint8_t U8FromBigEndian(std::span< const uint8_t, 1u > bytes)
constexpr float FloatFromBigEndian(std::span< const uint8_t, 4u > bytes)
constexpr int64_t I64FromNativeEndian(std::span< const uint8_t, 8u > bytes)
constexpr int8_t I8FromBigEndian(std::span< const uint8_t, 1u > bytes)
constexpr double DoubleFromLittleEndian(std::span< const uint8_t, 8u > bytes)
constexpr std::array< uint8_t, 4u > I32ToLittleEndian(int32_t val)
constexpr int32_t I32FromLittleEndian(std::span< const uint8_t, 4u > bytes)
constexpr std::array< uint8_t, 8u > DoubleToLittleEndian(double val)
constexpr std::array< uint8_t, 8u > I64ToLittleEndian(int64_t val)
constexpr uint16_t U16FromNativeEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 4u > I32ToNativeEndian(int32_t val)
constexpr std::array< uint8_t, 2u > U16ToLittleEndian(uint16_t val)
constexpr std::array< uint8_t, 2u > I16ToLittleEndian(int16_t val)
constexpr uint16_t U16FromBigEndian(std::span< const uint8_t, 2u > bytes)
constexpr std::array< uint8_t, 2u > U16ToBigEndian(uint16_t val)
constexpr uint32_t U32FromNativeEndian(std::span< const uint8_t, 4u > bytes)