Nebula
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
23#include "core/types.h"
24#if !__OSX__
25#include "math/vec4.h"
26#include "math/mat4.h"
27#endif
28
29//------------------------------------------------------------------------------
30namespace System
31{
32class ByteOrder
33{
34public:
35 // byte orders
36 enum Type
37 {
38 LittleEndian = 0, // e.g. x86
39 BigEndian, // e.g. PowerPC
40 Network = BigEndian, // network byte order
41
42 //FIXME, this is wrong
43 #if __WIN32__ || __LINUX__
45 #else
47 #endif
48 };
49
51 ByteOrder();
53 ByteOrder(Type fromByteOrder, Type toByteOrder);
55 void SetFromByteOrder(Type fromByteOrder);
57 Type GetFromByteOrder() const;
59 void SetToByteOrder(Type toByteOrder);
61 Type GetToByteOrder() const;
63 template<class TYPE> void ConvertInPlace(TYPE& val) const;
65 template<class TYPE> TYPE Convert(TYPE val) const;
67 template<class TYPE> static void ConvertInPlace(Type fromByteOrder, Type toByteOrder, TYPE& val);
69 template<class TYPE> static TYPE Convert(Type fromByteOrder, Type toByteOrder, TYPE val);
70
71private:
74
76 {
77 float f;
79 };
81 {
82 double d;
83 unsigned long long u;
84 };
85};
86
87//------------------------------------------------------------------------------
90__forceinline
92 from(Host),
93 to(Host)
94{
95 // empty
96}
97
98//------------------------------------------------------------------------------
101__forceinline
103 from(fromByteOrder),
104 to(toByteOrder)
105{
106 // empty
107}
108
109//------------------------------------------------------------------------------
112__forceinline void
114{
115 this->from = fromByteOrder;
116}
117
118//------------------------------------------------------------------------------
121__forceinline ByteOrder::Type
123{
124 return this->from;
125}
126
127//------------------------------------------------------------------------------
130__forceinline void
132{
133 this->to = toByteOrder;
134}
135
136//------------------------------------------------------------------------------
139__forceinline ByteOrder::Type
141{
142 return this->to;
143}
144
145//------------------------------------------------------------------------------
148template<> __forceinline void
149ByteOrder::ConvertInPlace<short>(Type fromByteOrder, Type toByteOrder, short& val)
150{
151 if (fromByteOrder != toByteOrder)
152 {
153 ushort res = _byteswap_ushort((ushort)val);
154 val = (short)res;
155 }
156}
157
158//------------------------------------------------------------------------------
161template<> __forceinline void
162ByteOrder::ConvertInPlace<short>(short& val) const
163{
164 if (this->from != this->to)
165 {
166 ushort res = _byteswap_ushort((ushort)val);
167 val = (short)res;
168 }
169}
170
171//------------------------------------------------------------------------------
174template<> __forceinline short
175ByteOrder::Convert<short>(Type fromByteOrder, Type toByteOrder, short val)
176{
177 if (fromByteOrder != toByteOrder)
178 {
179 return (short)_byteswap_ushort((ushort)val);
180 }
181 else
182 {
183 return val;
184 }
185}
186
187//------------------------------------------------------------------------------
190template<> __forceinline short
191ByteOrder::Convert<short>(short val) const
192{
193 if (this->from != this->to)
194 {
195 return (short)_byteswap_ushort((ushort)val);
196 }
197 else
198 {
199 return val;
200 }
201}
202
203//------------------------------------------------------------------------------
206template<> __forceinline void
207ByteOrder::ConvertInPlace<ushort>(Type fromByteOrder, Type toByteOrder, ushort& val)
208{
209 if (fromByteOrder != toByteOrder)
210 {
211 val = _byteswap_ushort(val);
212 }
213}
214
215//------------------------------------------------------------------------------
218template<> __forceinline void
220{
221 if (this->from != this->to)
222 {
223 val = _byteswap_ushort(val);
224 }
225}
226
227//------------------------------------------------------------------------------
230template<> __forceinline ushort
231ByteOrder::Convert<ushort>(Type fromByteOrder, Type toByteOrder, ushort val)
232{
233 if (fromByteOrder != toByteOrder)
234 {
235 return _byteswap_ushort(val);
236 }
237 else
238 {
239 return val;
240 }
241}
242
243//------------------------------------------------------------------------------
246template<> __forceinline ushort
248{
249 if (this->from != this->to)
250 {
251 return _byteswap_ushort(val);
252 }
253 else
254 {
255 return val;
256 }
257}
258
259//------------------------------------------------------------------------------
262template<> __forceinline void
263ByteOrder::ConvertInPlace<int>(Type fromByteOrder, Type toByteOrder, int& val)
264{
265 if (fromByteOrder != toByteOrder)
266 {
267 uint res = _byteswap_ulong((uint)val);
268 val = (int)res;
269 }
270}
271
272//------------------------------------------------------------------------------
275template<> __forceinline void
276ByteOrder::ConvertInPlace<int>(int& val) const
277{
278 if (this->from != this->to)
279 {
280 uint res = _byteswap_ulong((uint)val);
281 val = (int)res;
282 }
283}
284
285//------------------------------------------------------------------------------
288template<> __forceinline int
289ByteOrder::Convert<int>(Type fromByteOrder, Type toByteOrder, int val)
290{
291 if (fromByteOrder != toByteOrder)
292 {
293 return (int) _byteswap_ulong((uint)val);
294 }
295 else
296 {
297 return val;
298 }
299}
300
301//------------------------------------------------------------------------------
304template<> __forceinline int
305ByteOrder::Convert<int>(int val) const
306{
307 if (this->from != this->to)
308 {
309 return (int) _byteswap_ulong((uint)val);
310 }
311 else
312 {
313 return val;
314 }
315}
316
317//------------------------------------------------------------------------------
320template<> __forceinline void
321ByteOrder::ConvertInPlace<uint>(Type fromByteOrder, Type toByteOrder, uint& val)
322{
323 if (fromByteOrder != toByteOrder)
324 {
325 val = _byteswap_ulong(val);
326 }
327}
328
329//------------------------------------------------------------------------------
332template<> __forceinline void
334{
335 if (this->from != this->to)
336 {
337 val = _byteswap_ulong(val);
338 }
339}
340
341//------------------------------------------------------------------------------
344template<> __forceinline uint
345ByteOrder::Convert<uint>(Type fromByteOrder, Type toByteOrder, uint val)
346{
347 if (fromByteOrder != toByteOrder)
348 {
349 return _byteswap_ulong(val);
350 }
351 else
352 {
353 return val;
354 }
355}
356
357//------------------------------------------------------------------------------
360template<> __forceinline uint
362{
363 if (this->from != this->to)
364 {
365 return _byteswap_ulong(val);
366 }
367 else
368 {
369 return val;
370 }
371}
372
373//------------------------------------------------------------------------------
376template<> __forceinline void
377ByteOrder::ConvertInPlace<float>(Type fromByteOrder, Type toByteOrder, float& val)
378{
379 if (fromByteOrder != toByteOrder)
380 {
381 PunFloatUL pun;
382 pun.f = val;
383 pun.u = _byteswap_ulong(pun.u);
384 val = pun.f;
385 }
386}
387
388//------------------------------------------------------------------------------
391template<> __forceinline void
392ByteOrder::ConvertInPlace<float>(float& val) const
393{
394 if (this->from != this->to)
395 {
396 PunFloatUL pun;
397 pun.f = val;
398 pun.u = _byteswap_ulong(pun.u);
399 val = pun.f;
400 }
401}
402
403//------------------------------------------------------------------------------
406template<> __forceinline float
407ByteOrder::Convert<float>(Type fromByteOrder, Type toByteOrder, float val)
408{
409 if (fromByteOrder != toByteOrder)
410 {
411 PunFloatUL pun;
412 pun.f = val;
413 pun.u = _byteswap_ulong(pun.u);
414 return pun.f;
415 }
416 else
417 {
418 return val;
419 }
420}
421
422//------------------------------------------------------------------------------
425template<> __forceinline float
426ByteOrder::Convert<float>(float val) const
427{
428 if (this->from != this->to)
429 {
430 PunFloatUL pun;
431 pun.f = val;
432 pun.u = _byteswap_ulong(pun.u);
433 return pun.f;
434 }
435 else
436 {
437 return val;
438 }
439}
440
441//------------------------------------------------------------------------------
444template<> __forceinline void
445ByteOrder::ConvertInPlace<double>(Type fromByteOrder, Type toByteOrder, double& val)
446{
447 if (fromByteOrder != toByteOrder)
448 {
449 PunDoubleULL pun;
450 pun.d = val;
451 pun.u = _byteswap_uint64(pun.u);
452 val = pun.d;
453 }
454}
455
456//------------------------------------------------------------------------------
459template<> __forceinline void
460ByteOrder::ConvertInPlace<double>(double& val) const
461{
462 if (this->from != this->to)
463 {
464 PunDoubleULL pun;
465 pun.d = val;
466 pun.u = _byteswap_uint64(pun.u);
467 val = pun.d;
468 }
469}
470
471//------------------------------------------------------------------------------
474template<> __forceinline double
475ByteOrder::Convert<double>(Type fromByteOrder, Type toByteOrder, double val)
476{
477 if (fromByteOrder != toByteOrder)
478 {
479 PunDoubleULL pun;
480 pun.d = val;
481 pun.u = _byteswap_uint64(pun.u);
482 return pun.d;
483 }
484 else
485 {
486 return val;
487 }
488}
489
490//------------------------------------------------------------------------------
493template<> __forceinline uint64_t
494ByteOrder::Convert<uint64_t>(Type fromByteOrder, Type toByteOrder, uint64_t val)
495{
496 if (fromByteOrder != toByteOrder)
497 {
498 return _byteswap_uint64(val);
499 }
500 else
501 {
502 return val;
503 }
504}
505
506//------------------------------------------------------------------------------
509template<> __forceinline int64_t
510ByteOrder::Convert<int64_t>(Type fromByteOrder, Type toByteOrder, int64_t val)
511{
512 if (fromByteOrder != toByteOrder)
513 {
514 return _byteswap_uint64(val);
515 }
516 else
517 {
518 return val;
519 }
520}
521
522//------------------------------------------------------------------------------
525template<> __forceinline int64_t
526ByteOrder::Convert<int64_t>(int64_t val) const
527{
528 if (this->from != this->to)
529 {
530 return _byteswap_uint64(val);
531 }
532 else
533 {
534 return val;
535 }
536}
537
538//------------------------------------------------------------------------------
541template<> __forceinline uint64_t
542ByteOrder::Convert<uint64_t>(uint64_t val) const
543{
544 if (this->from != this->to)
545 {
546 return _byteswap_uint64(val);
547 }
548 else
549 {
550 return val;
551 }
552}
553
554//------------------------------------------------------------------------------
557template<> __forceinline double
558ByteOrder::Convert<double>(double val) const
559{
560 if (this->from != this->to)
561 {
562 PunDoubleULL pun;
563 pun.d = val;
564 pun.u = _byteswap_uint64(pun.u);
565 return pun.d;
566 }
567 else
568 {
569 return val;
570 }
571}
572
573#if !__OSX__
574//------------------------------------------------------------------------------
577template<> __forceinline void
578ByteOrder::ConvertInPlace<Math::vec4>(Type fromByteOrder, Type toByteOrder, Math::vec4& val)
579{
580 if (fromByteOrder != toByteOrder)
581 {
582 ConvertInPlace<float>(fromByteOrder, toByteOrder, val.x);
583 ConvertInPlace<float>(fromByteOrder, toByteOrder, val.y);
584 ConvertInPlace<float>(fromByteOrder, toByteOrder, val.z);
585 ConvertInPlace<float>(fromByteOrder, toByteOrder, val.w);
586 }
587}
588
589//------------------------------------------------------------------------------
592template<> __forceinline void
593ByteOrder::ConvertInPlace<Math::vec4>(Math::vec4& val) const
594{
595 if (this->from != this->to)
596 {
597 ConvertInPlace<float>(val.x);
598 ConvertInPlace<float>(val.y);
599 ConvertInPlace<float>(val.z);
600 ConvertInPlace<float>(val.w);
601 }
602}
603
604//------------------------------------------------------------------------------
607template<> __forceinline void
608ByteOrder::ConvertInPlace<Math::mat4>(Type fromByteOrder, Type toByteOrder, Math::mat4& val)
609{
610 if (fromByteOrder != toByteOrder)
611 {
612 Math::vec4 row0 = val.row0;
613 Math::vec4 row1 = val.row1;
614 Math::vec4 row2 = val.row2;
615 Math::vec4 row3 = val.row3;
616 ConvertInPlace<Math::vec4>(fromByteOrder, toByteOrder, row0);
617 ConvertInPlace<Math::vec4>(fromByteOrder, toByteOrder, row1);
618 ConvertInPlace<Math::vec4>(fromByteOrder, toByteOrder, row2);
619 ConvertInPlace<Math::vec4>(fromByteOrder, toByteOrder, row3);
620 val.row0 = row0;
621 val.row1 = row1;
622 val.row2 = row2;
623 val.row3 = row3;
624 }
625}
626
627//------------------------------------------------------------------------------
630template<> __forceinline void
631ByteOrder::ConvertInPlace<Math::mat4>(Math::mat4& val) const
632{
633 if (this->from != this->to)
634 {
635 Math::vec4 row0 = val.row0;
636 Math::vec4 row1 = val.row1;
637 Math::vec4 row2 = val.row2;
638 Math::vec4 row3 = val.row3;
643 val.row0 = row0;
644 val.row1 = row1;
645 val.row2 = row2;
646 val.row3 = row3;
647 }
648}
649#endif // __OSX__
650
651} // namespace System
652//------------------------------------------------------------------------------
653
(C) 2007 Radon Labs GmbH (C) 2013-2018 Individual contributors, see AUTHORS file
void SetFromByteOrder(Type fromByteOrder)
set from-byte-order
Definition byteorder.h:113
Type from
Definition byteorder.h:72
static void ConvertInPlace(Type fromByteOrder, Type toByteOrder, TYPE &val)
endian-convert in place
Type GetToByteOrder() const
get to-byte-order
Definition byteorder.h:140
void ConvertInPlace(TYPE &val) const
endian-convert in place
static TYPE Convert(Type fromByteOrder, Type toByteOrder, TYPE val)
endian-convert by copy
Type to
Definition byteorder.h:73
ByteOrder()
default constructor
Definition byteorder.h:91
Type
Definition byteorder.h:37
@ Network
Definition byteorder.h:40
@ Host
Definition byteorder.h:46
@ BigEndian
Definition byteorder.h:39
@ LittleEndian
Definition byteorder.h:38
Type GetFromByteOrder() const
get from-byte-order
Definition byteorder.h:122
void SetToByteOrder(Type toByteOrder)
set to-byte-order
Definition byteorder.h:131
TYPE Convert(TYPE val) const
endian-convert by copy
Definition osxsysfunc.h:15
__forceinline void ByteOrder::ConvertInPlace< Math::vec4 >(Type fromByteOrder, Type toByteOrder, Math::vec4 &val)
Definition byteorder.h:578
A 4x4 single point precision float matrix.
Definition mat4.h:47
vec4 row3
Definition mat4.h:136
vec4 row1
Definition mat4.h:134
vec4 row0
Definition mat4.h:133
vec4 row2
Definition mat4.h:135
A 4D vector.
Definition vec4.h:24
float y
Definition vec4.h:93
float z
Definition vec4.h:93
float w
Definition vec4.h:93
float x
Definition vec4.h:93
unsigned long ulong
Definition types.h:30
unsigned int uint
Definition types.h:31
unsigned short ushort
Definition types.h:32
Definition byteorder.h:81
unsigned long long u
Definition byteorder.h:83
double d
Definition byteorder.h:82
Definition byteorder.h:76
float f
Definition byteorder.h:77
ulong u
Definition byteorder.h:78