Blender V4.3
BLI_endian_switch_inline.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11/* only include from header */
12#ifndef __BLI_ENDIAN_SWITCH_H__
13# error "this file isn't to be directly included"
14#endif
15
20/* NOTE: using a temp char to switch endian is a lot slower,
21 * use bit shifting instead. */
22
23/* *** 16 *** */
24
26{
27 BLI_endian_switch_uint16((unsigned short *)val);
28}
29BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
30{
31#ifdef __GNUC__
32 *val = __builtin_bswap16(*val);
33#else
34 unsigned short tval = *val;
35 *val = (tval >> 8) | (tval << 8);
36#endif
37}
38
39/* *** 32 *** */
40
42{
43 BLI_endian_switch_uint32((unsigned int *)val);
44}
45BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
46{
47#ifdef __GNUC__
48 *val = __builtin_bswap32(*val);
49#else
50 unsigned int tval = *val;
51 *val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24));
52#endif
53}
55{
56 BLI_endian_switch_uint32((unsigned int *)val);
57}
58
59/* *** 64 *** */
60
66{
67#ifdef __GNUC__
68 *val = __builtin_bswap64(*val);
69#else
70 uint64_t tval = *val;
71 *val = ((tval >> 56)) | ((tval << 40) & 0x00ff000000000000ll) |
72 ((tval << 24) & 0x0000ff0000000000ll) | ((tval << 8) & 0x000000ff00000000ll) |
73 ((tval >> 8) & 0x00000000ff000000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
74 ((tval >> 40) & 0x000000000000ff00ll) | ((tval << 56));
75#endif
76}
81
82#ifdef __cplusplus
83}
84#endif
#define BLI_INLINE
BLI_INLINE void BLI_endian_switch_float(float *val)
BLI_INLINE void BLI_endian_switch_int32(int *val)
BLI_INLINE void BLI_endian_switch_int64(int64_t *val)
BLI_INLINE void BLI_endian_switch_int16(short *val)
BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
BLI_INLINE void BLI_endian_switch_double(double *val)
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90