summaryrefslogtreecommitdiff
path: root/include/libHX/endian_float.h
blob: f36e1108732615cffd90f5e2aa27ab2220b5c3b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef _LIBHX_ENDIAN_FLOAT_H
#define _LIBHX_ENDIAN_FLOAT_H 1
#include <string.h>
#include <libHX/endian.h>

/*
 * While construction of integers from bytes was easy, it would be more work
 * for floats — and compilers probably won't be able to optimize it.
 *
 * So then, we make some shortcuts/assumptions here in endian_float.h:
 * - that the host platform uses the same byte order for integers as for floats
 * - that the host platform is using IEEE754/IEC559
 *
 * This holds for the typical Linux on {arm gnueabi LE, arm gnueabi
 * BE, aarch64 LE, aarch64 BE, i386, amd64, hppa, loongarch64, m68k,
 * mips, ppc64, ppc64le, sparc, sparc64, riscv64, s390x}.
 */

/*
 * Unlike cpu_to_be32, we will offer no float_cpu_to_be32. Values comprised of
 * inverted bytes should probably not be passed around in memory.
 */
LIBHX_DBG_INLINE float LIBHX_OPT_O2 float_be32p_to_cpu(const void *p)
{
	uint32_t v = be32p_to_cpu(p);
	float w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	return w;
}

LIBHX_DBG_INLINE double LIBHX_OPT_O2 float_le32p_to_cpu(const void *p)
{
	uint32_t v = le32p_to_cpu(p);
	float w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	return w;
}

LIBHX_DBG_INLINE float LIBHX_OPT_O2 float_be64p_to_cpu(const void *p)
{
	uint64_t v = be64p_to_cpu(p);
	double w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	return w;
}

LIBHX_DBG_INLINE double LIBHX_OPT_O2 float_le64p_to_cpu(const void *p)
{
	uint64_t v = le64p_to_cpu(p);
	double w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	return w;
}

LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_be32p(void *p, float v)
{
	uint32_t w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	cpu_to_be32p(p, w);
}

LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_le32p(void *p, float v)
{
	uint32_t w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	cpu_to_le32p(p, w);
}

LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_be64p(void *p, double v)
{
	uint64_t w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	cpu_to_be64p(p, w);
}

LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_le64p(void *p, double v)
{
	uint64_t w;
#ifdef __cplusplus
	static_assert(sizeof(v) == sizeof(w));
#endif
	memcpy(&w, &v, sizeof(w));
	cpu_to_le64p(p, w);
}

#endif /* _LIBHX_ENDIAN_FLOAT_H */