2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-09-03 09:37:38 +02:00
|
|
|
|
2019-01-09 22:46:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-09-03 09:37:38 +02:00
|
|
|
/* only include from header */
|
|
|
|
#ifndef __BLI_ENDIAN_SWITCH_H__
|
2021-03-17 23:35:12 +01:00
|
|
|
# error "this file isn't to be directly included"
|
2012-09-03 09:37:38 +02:00
|
|
|
#endif
|
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2012-09-03 09:37:38 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-03 15:08:40 +02:00
|
|
|
/* NOTE: using a temp char to switch endian is a lot slower,
|
2012-10-07 06:21:37 +02:00
|
|
|
* use bit shifting instead. */
|
2012-09-03 09:37:38 +02:00
|
|
|
|
2012-10-07 06:21:37 +02:00
|
|
|
/* *** 16 *** */
|
2021-12-20 09:01:14 +01:00
|
|
|
|
2012-09-03 09:37:38 +02:00
|
|
|
BLI_INLINE void BLI_endian_switch_int16(short *val)
|
|
|
|
{
|
2012-11-20 11:37:11 +01:00
|
|
|
BLI_endian_switch_uint16((unsigned short *)val);
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val)
|
|
|
|
{
|
2022-09-26 04:26:48 +02:00
|
|
|
#ifdef __GNUC__
|
2015-07-11 21:33:04 +02:00
|
|
|
*val = __builtin_bswap16(*val);
|
|
|
|
#else
|
2012-11-20 11:37:11 +01:00
|
|
|
unsigned short tval = *val;
|
|
|
|
*val = (tval >> 8) | (tval << 8);
|
2015-07-11 21:33:04 +02:00
|
|
|
#endif
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-07 06:21:37 +02:00
|
|
|
/* *** 32 *** */
|
2021-12-20 09:01:14 +01:00
|
|
|
|
2012-09-03 09:37:38 +02:00
|
|
|
BLI_INLINE void BLI_endian_switch_int32(int *val)
|
|
|
|
{
|
2012-11-20 11:37:11 +01:00
|
|
|
BLI_endian_switch_uint32((unsigned int *)val);
|
|
|
|
}
|
|
|
|
BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val)
|
|
|
|
{
|
2015-07-11 21:33:04 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
*val = __builtin_bswap32(*val);
|
|
|
|
#else
|
2012-11-20 11:37:11 +01:00
|
|
|
unsigned int tval = *val;
|
2012-10-07 06:21:37 +02:00
|
|
|
*val = ((tval >> 24)) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | ((tval << 24));
|
2015-07-11 21:33:04 +02:00
|
|
|
#endif
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
BLI_INLINE void BLI_endian_switch_float(float *val)
|
|
|
|
{
|
2012-11-20 11:37:11 +01:00
|
|
|
BLI_endian_switch_uint32((unsigned int *)val);
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-07 06:21:37 +02:00
|
|
|
/* *** 64 *** */
|
2021-12-20 09:01:14 +01:00
|
|
|
|
2012-09-03 09:37:38 +02:00
|
|
|
BLI_INLINE void BLI_endian_switch_int64(int64_t *val)
|
|
|
|
{
|
2012-11-20 11:37:11 +01:00
|
|
|
BLI_endian_switch_uint64((uint64_t *)val);
|
|
|
|
}
|
|
|
|
BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val)
|
|
|
|
{
|
2015-07-11 21:33:04 +02:00
|
|
|
#ifdef __GNUC__
|
2015-07-11 21:35:44 +02:00
|
|
|
*val = __builtin_bswap64(*val);
|
2015-07-11 21:33:04 +02:00
|
|
|
#else
|
2012-11-20 11:37:11 +01:00
|
|
|
uint64_t tval = *val;
|
2012-10-14 06:11:34 +02:00
|
|
|
*val = ((tval >> 56)) | ((tval << 40) & 0x00ff000000000000ll) |
|
|
|
|
((tval << 24) & 0x0000ff0000000000ll) | ((tval << 8) & 0x000000ff00000000ll) |
|
|
|
|
((tval >> 8) & 0x00000000ff000000ll) | ((tval >> 24) & 0x0000000000ff0000ll) |
|
|
|
|
((tval >> 40) & 0x000000000000ff00ll) | ((tval << 56));
|
2015-07-11 21:33:04 +02:00
|
|
|
#endif
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
BLI_INLINE void BLI_endian_switch_double(double *val)
|
|
|
|
{
|
2012-11-20 11:37:11 +01:00
|
|
|
BLI_endian_switch_uint64((uint64_t *)val);
|
2012-09-03 09:37:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|