Blender  V2.93
BLI_endian_switch_inline.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* only include from header */
24 #ifndef __BLI_ENDIAN_SWITCH_H__
25 # error "this file isn't to be directly included"
26 #endif
27 
32 /* note: using a temp char to switch endian is a lot slower,
33  * use bit shifting instead. */
34 
35 /* *** 16 *** */
37 {
38  BLI_endian_switch_uint16((unsigned short *)val);
39 }
40 BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
41 {
42 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 408)) /* gcc4.8+ only */
43  *val = __builtin_bswap16(*val);
44 #else
45  unsigned short tval = *val;
46  *val = (tval >> 8) | (tval << 8);
47 #endif
48 }
49 
50 /* *** 32 *** */
52 {
53  BLI_endian_switch_uint32((unsigned int *)val);
54 }
55 BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
56 {
57 #ifdef __GNUC__
58  *val = __builtin_bswap32(*val);
59 #else
60  unsigned int tval = *val;
61  *val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24));
62 #endif
63 }
65 {
66  BLI_endian_switch_uint32((unsigned int *)val);
67 }
68 
69 /* *** 64 *** */
71 {
73 }
75 {
76 #ifdef __GNUC__
77  *val = __builtin_bswap64(*val);
78 #else
79  uint64_t tval = *val;
80  *val = ((tval >> 56)) | ((tval << 40) & 0x00ff000000000000ll) |
81  ((tval << 24) & 0x0000ff0000000000ll) | ((tval << 8) & 0x000000ff00000000ll) |
82  ((tval >> 8) & 0x00000000ff000000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
83  ((tval >> 40) & 0x000000000000ff00ll) | ((tval << 56));
84 #endif
85 }
87 {
89 }
90 
91 #ifdef __cplusplus
92 }
93 #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:92
unsigned __int64 uint64_t
Definition: stdint.h:93