Blender V5.0
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
8#include "BLI_sys_types.h"
9
10/* only include from header */
11#ifndef __BLI_ENDIAN_SWITCH_H__
12# error "this file isn't to be directly included"
13#endif
14
18
19/* NOTE: using a temp char to switch endian is a lot slower,
20 * use bit shifting instead. */
21
22/* *** 16 *** */
23
25{
26 BLI_endian_switch_uint16((unsigned short *)val);
27}
28BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
29{
30#ifdef __GNUC__
31 *val = __builtin_bswap16(*val);
32#else
33 unsigned short tval = *val;
34 *val = (tval >> 8) | (tval << 8);
35#endif
36}
37
38/* *** 32 *** */
39
41{
42 BLI_endian_switch_uint32((unsigned int *)val);
43}
44BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
45{
46#ifdef __GNUC__
47 *val = __builtin_bswap32(*val);
48#else
49 unsigned int tval = *val;
50 *val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24));
51#endif
52}
54{
55 BLI_endian_switch_uint32((unsigned int *)val);
56}
57
58/* *** 64 *** */
59
65{
66#ifdef __GNUC__
67 *val = __builtin_bswap64(*val);
68#else
69 uint64_t tval = *val;
70 *val = ((tval >> 56)) | ((tval << 40) & 0x00ff000000000000ll) |
71 ((tval << 24) & 0x0000ff0000000000ll) | ((tval << 8) & 0x000000ff00000000ll) |
72 ((tval >> 8) & 0x00000000ff000000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
73 ((tval >> 40) & 0x000000000000ff00ll) | ((tval << 56));
74#endif
75}
#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)
long long int int64_t
unsigned long long int uint64_t