1/// @ref core
2/// @file glm/detail/func_packing.inl
3
4#include "../common.hpp"
5#include "type_half.hpp"
6
7namespace glm
8{
9	GLM_FUNC_QUALIFIER uint packUnorm2x16(vec2 const& v)
10	{
11		union
12		{
13			unsigned short in[2];
14			uint out;
15		} u;
16
17		vec<2, unsigned short, defaultp> result(round(clamp(v, 0.0f, 1.0f) * 65535.0f));
18
19		u.in[0] = result[0];
20		u.in[1] = result[1];
21
22		return u.out;
23	}
24
25	GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint p)
26	{
27		union
28		{
29			uint in;
30			unsigned short out[2];
31		} u;
32
33		u.in = p;
34
35		return vec2(u.out[0], u.out[1]) * 1.5259021896696421759365224689097e-5f;
36	}
37
38	GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const& v)
39	{
40		union
41		{
42			signed short in[2];
43			uint out;
44		} u;
45
46		vec<2, short, defaultp> result(round(clamp(v, -1.0f, 1.0f) * 32767.0f));
47
48		u.in[0] = result[0];
49		u.in[1] = result[1];
50
51		return u.out;
52	}
53
54	GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint p)
55	{
56		union
57		{
58			uint in;
59			signed short out[2];
60		} u;
61
62		u.in = p;
63
64		return clamp(vec2(u.out[0], u.out[1]) * 3.0518509475997192297128208258309e-5f, -1.0f, 1.0f);
65	}
66
67	GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const& v)
68	{
69		union
70		{
71			unsigned char in[4];
72			uint out;
73		} u;
74
75		vec<4, unsigned char, defaultp> result(round(clamp(v, 0.0f, 1.0f) * 255.0f));
76
77		u.in[0] = result[0];
78		u.in[1] = result[1];
79		u.in[2] = result[2];
80		u.in[3] = result[3];
81
82		return u.out;
83	}
84
85	GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint p)
86	{
87		union
88		{
89			uint in;
90			unsigned char out[4];
91		} u;
92
93		u.in = p;
94
95		return vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0039215686274509803921568627451f;
96	}
97
98	GLM_FUNC_QUALIFIER uint packSnorm4x8(vec4 const& v)
99	{
100		union
101		{
102			signed char in[4];
103			uint out;
104		} u;
105
106		vec<4, signed char, defaultp> result(round(clamp(v, -1.0f, 1.0f) * 127.0f));
107
108		u.in[0] = result[0];
109		u.in[1] = result[1];
110		u.in[2] = result[2];
111		u.in[3] = result[3];
112
113		return u.out;
114	}
115
116	GLM_FUNC_QUALIFIER glm::vec4 unpackSnorm4x8(uint p)
117	{
118		union
119		{
120			uint in;
121			signed char out[4];
122		} u;
123
124		u.in = p;
125
126		return clamp(vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0078740157480315f, -1.0f, 1.0f);
127	}
128
129	GLM_FUNC_QUALIFIER double packDouble2x32(uvec2 const& v)
130	{
131		union
132		{
133			uint   in[2];
134			double out;
135		} u;
136
137		u.in[0] = v[0];
138		u.in[1] = v[1];
139
140		return u.out;
141	}
142
143	GLM_FUNC_QUALIFIER uvec2 unpackDouble2x32(double v)
144	{
145		union
146		{
147			double in;
148			uint   out[2];
149		} u;
150
151		u.in = v;
152
153		return uvec2(u.out[0], u.out[1]);
154	}
155
156	GLM_FUNC_QUALIFIER uint packHalf2x16(vec2 const& v)
157	{
158		union
159		{
160			signed short in[2];
161			uint out;
162		} u;
163
164		u.in[0] = detail::toFloat16(v.x);
165		u.in[1] = detail::toFloat16(v.y);
166
167		return u.out;
168	}
169
170	GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint v)
171	{
172		union
173		{
174			uint in;
175			signed short out[2];
176		} u;
177
178		u.in = v;
179
180		return vec2(
181			detail::toFloat32(u.out[0]),
182			detail::toFloat32(u.out[1]));
183	}
184}//namespace glm
185
186#if GLM_CONFIG_SIMD == GLM_ENABLE
187#	include "func_packing_simd.inl"
188#endif
189
190