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 isnt to be directly included"
26 #endif
27
28 /** \file
29 * \ingroup bli
30 */
31
32 /* note: using a temp char to switch endian is a lot slower,
33 * use bit shifting instead. */
34
35 /* *** 16 *** */
BLI_endian_switch_int16(short * val)36 BLI_INLINE void BLI_endian_switch_int16(short *val)
37 {
38 BLI_endian_switch_uint16((unsigned short *)val);
39 }
BLI_endian_switch_uint16(unsigned short * val)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 *** */
BLI_endian_switch_int32(int * val)51 BLI_INLINE void BLI_endian_switch_int32(int *val)
52 {
53 BLI_endian_switch_uint32((unsigned int *)val);
54 }
BLI_endian_switch_uint32(unsigned int * val)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 }
BLI_endian_switch_float(float * val)64 BLI_INLINE void BLI_endian_switch_float(float *val)
65 {
66 BLI_endian_switch_uint32((unsigned int *)val);
67 }
68
69 /* *** 64 *** */
BLI_endian_switch_int64(int64_t * val)70 BLI_INLINE void BLI_endian_switch_int64(int64_t *val)
71 {
72 BLI_endian_switch_uint64((uint64_t *)val);
73 }
BLI_endian_switch_uint64(uint64_t * val)74 BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
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 }
BLI_endian_switch_double(double * val)86 BLI_INLINE void BLI_endian_switch_double(double *val)
87 {
88 BLI_endian_switch_uint64((uint64_t *)val);
89 }
90
91 #ifdef __cplusplus
92 }
93 #endif
94