Blender  V3.3
BLI_endian_switch_inline.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 /* only include from header */
10 #ifndef __BLI_ENDIAN_SWITCH_H__
11 # error "this file isn't to be directly included"
12 #endif
13 
18 /* NOTE: using a temp char to switch endian is a lot slower,
19  * use bit shifting instead. */
20 
21 /* *** 16 *** */
22 
24 {
25  BLI_endian_switch_uint16((unsigned short *)val);
26 }
27 BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
28 {
29 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 408)) /* gcc4.8+ only */
30  *val = __builtin_bswap16(*val);
31 #else
32  unsigned short tval = *val;
33  *val = (tval >> 8) | (tval << 8);
34 #endif
35 }
36 
37 /* *** 32 *** */
38 
40 {
41  BLI_endian_switch_uint32((unsigned int *)val);
42 }
43 BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
44 {
45 #ifdef __GNUC__
46  *val = __builtin_bswap32(*val);
47 #else
48  unsigned int tval = *val;
49  *val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24));
50 #endif
51 }
53 {
54  BLI_endian_switch_uint32((unsigned int *)val);
55 }
56 
57 /* *** 64 *** */
58 
60 {
62 }
64 {
65 #ifdef __GNUC__
66  *val = __builtin_bswap64(*val);
67 #else
68  uint64_t tval = *val;
69  *val = ((tval >> 56)) | ((tval << 40) & 0x00ff000000000000ll) |
70  ((tval << 24) & 0x0000ff0000000000ll) | ((tval << 8) & 0x000000ff00000000ll) |
71  ((tval >> 8) & 0x00000000ff000000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
72  ((tval >> 40) & 0x000000000000ff00ll) | ((tval << 56));
73 #endif
74 }
76 {
78 }
79 
80 #ifdef __cplusplus
81 }
82 #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