Nebula
Toggle main menu visibility
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
//------------------------------------------------------------------------------
30
namespace
System
31
{
32
class
ByteOrder
33
{
34
public
:
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__
44
Host
=
LittleEndian
,
45
#else
46
Host
=
BigEndian
,
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
71
private
:
72
Type
from
;
73
Type
to
;
74
75
union
PunFloatUL
76
{
77
float
f
;
78
ulong
u
;
79
};
80
union
PunDoubleULL
81
{
82
double
d
;
83
unsigned
long
long
u
;
84
};
85
};
86
87
//------------------------------------------------------------------------------
90
__forceinline
91
ByteOrder::ByteOrder
() :
92
from
(
Host
),
93
to
(
Host
)
94
{
95
// empty
96
}
97
98
//------------------------------------------------------------------------------
101
__forceinline
102
ByteOrder::ByteOrder
(
ByteOrder::Type
fromByteOrder,
ByteOrder::Type
toByteOrder) :
103
from
(fromByteOrder),
104
to
(toByteOrder)
105
{
106
// empty
107
}
108
109
//------------------------------------------------------------------------------
112
__forceinline
void
113
ByteOrder::SetFromByteOrder
(
Type
fromByteOrder)
114
{
115
this->
from
= fromByteOrder;
116
}
117
118
//------------------------------------------------------------------------------
121
__forceinline
ByteOrder::Type
122
ByteOrder::GetFromByteOrder
()
const
123
{
124
return
this->
from
;
125
}
126
127
//------------------------------------------------------------------------------
130
__forceinline
void
131
ByteOrder::SetToByteOrder
(
Type
toByteOrder)
132
{
133
this->
to
= toByteOrder;
134
}
135
136
//------------------------------------------------------------------------------
139
__forceinline
ByteOrder::Type
140
ByteOrder::GetToByteOrder
()
const
141
{
142
return
this->
to
;
143
}
144
145
//------------------------------------------------------------------------------
148
template
<> __forceinline
void
149
ByteOrder::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
//------------------------------------------------------------------------------
161
template
<> __forceinline
void
162
ByteOrder::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
//------------------------------------------------------------------------------
174
template
<> __forceinline
short
175
ByteOrder::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
//------------------------------------------------------------------------------
190
template
<> __forceinline
short
191
ByteOrder::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
//------------------------------------------------------------------------------
206
template
<> __forceinline
void
207
ByteOrder::ConvertInPlace<ushort>
(
Type
fromByteOrder,
Type
toByteOrder,
ushort
& val)
208
{
209
if
(fromByteOrder != toByteOrder)
210
{
211
val = _byteswap_ushort(val);
212
}
213
}
214
215
//------------------------------------------------------------------------------
218
template
<> __forceinline
void
219
ByteOrder::ConvertInPlace<ushort>
(
ushort
& val)
const
220
{
221
if
(this->
from
!= this->
to
)
222
{
223
val = _byteswap_ushort(val);
224
}
225
}
226
227
//------------------------------------------------------------------------------
230
template
<> __forceinline
ushort
231
ByteOrder::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
//------------------------------------------------------------------------------
246
template
<> __forceinline
ushort
247
ByteOrder::Convert<ushort>
(
ushort
val)
const
248
{
249
if
(this->
from
!= this->
to
)
250
{
251
return
_byteswap_ushort(val);
252
}
253
else
254
{
255
return
val;
256
}
257
}
258
259
//------------------------------------------------------------------------------
262
template
<> __forceinline
void
263
ByteOrder::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
//------------------------------------------------------------------------------
275
template
<> __forceinline
void
276
ByteOrder::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
//------------------------------------------------------------------------------
288
template
<> __forceinline
int
289
ByteOrder::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
//------------------------------------------------------------------------------
304
template
<> __forceinline
int
305
ByteOrder::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
//------------------------------------------------------------------------------
320
template
<> __forceinline
void
321
ByteOrder::ConvertInPlace<uint>
(
Type
fromByteOrder,
Type
toByteOrder,
uint
& val)
322
{
323
if
(fromByteOrder != toByteOrder)
324
{
325
val = _byteswap_ulong(val);
326
}
327
}
328
329
//------------------------------------------------------------------------------
332
template
<> __forceinline
void
333
ByteOrder::ConvertInPlace<uint>
(
uint
& val)
const
334
{
335
if
(this->
from
!= this->
to
)
336
{
337
val = _byteswap_ulong(val);
338
}
339
}
340
341
//------------------------------------------------------------------------------
344
template
<> __forceinline
uint
345
ByteOrder::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
//------------------------------------------------------------------------------
360
template
<> __forceinline
uint
361
ByteOrder::Convert<uint>
(
uint
val)
const
362
{
363
if
(this->
from
!= this->
to
)
364
{
365
return
_byteswap_ulong(val);
366
}
367
else
368
{
369
return
val;
370
}
371
}
372
373
//------------------------------------------------------------------------------
376
template
<> __forceinline
void
377
ByteOrder::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
//------------------------------------------------------------------------------
391
template
<> __forceinline
void
392
ByteOrder::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
//------------------------------------------------------------------------------
406
template
<> __forceinline
float
407
ByteOrder::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
//------------------------------------------------------------------------------
425
template
<> __forceinline
float
426
ByteOrder::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
//------------------------------------------------------------------------------
444
template
<> __forceinline
void
445
ByteOrder::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
//------------------------------------------------------------------------------
459
template
<> __forceinline
void
460
ByteOrder::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
//------------------------------------------------------------------------------
474
template
<> __forceinline
double
475
ByteOrder::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
//------------------------------------------------------------------------------
493
template
<> __forceinline uint64_t
494
ByteOrder::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
//------------------------------------------------------------------------------
509
template
<> __forceinline int64_t
510
ByteOrder::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
//------------------------------------------------------------------------------
525
template
<> __forceinline int64_t
526
ByteOrder::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
//------------------------------------------------------------------------------
541
template
<> __forceinline uint64_t
542
ByteOrder::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
//------------------------------------------------------------------------------
557
template
<> __forceinline
double
558
ByteOrder::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
//------------------------------------------------------------------------------
577
template
<> __forceinline
void
578
ByteOrder::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
//------------------------------------------------------------------------------
592
template
<> __forceinline
void
593
ByteOrder::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
//------------------------------------------------------------------------------
607
template
<> __forceinline
void
608
ByteOrder::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
//------------------------------------------------------------------------------
630
template
<> __forceinline
void
631
ByteOrder::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
;
639
ConvertInPlace<Math::vec4>
(row0);
640
ConvertInPlace<Math::vec4>
(row1);
641
ConvertInPlace<Math::vec4>
(row2);
642
ConvertInPlace<Math::vec4>
(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
System::ByteOrder::SetFromByteOrder
void SetFromByteOrder(Type fromByteOrder)
set from-byte-order
Definition
byteorder.h:113
System::ByteOrder::from
Type from
Definition
byteorder.h:72
System::ByteOrder::ConvertInPlace
static void ConvertInPlace(Type fromByteOrder, Type toByteOrder, TYPE &val)
endian-convert in place
System::ByteOrder::GetToByteOrder
Type GetToByteOrder() const
get to-byte-order
Definition
byteorder.h:140
System::ByteOrder::ConvertInPlace
void ConvertInPlace(TYPE &val) const
endian-convert in place
System::ByteOrder::Convert
static TYPE Convert(Type fromByteOrder, Type toByteOrder, TYPE val)
endian-convert by copy
System::ByteOrder::to
Type to
Definition
byteorder.h:73
System::ByteOrder::ByteOrder
ByteOrder()
default constructor
Definition
byteorder.h:91
System::ByteOrder::Type
Type
Definition
byteorder.h:37
System::ByteOrder::Network
@ Network
Definition
byteorder.h:40
System::ByteOrder::Host
@ Host
Definition
byteorder.h:46
System::ByteOrder::BigEndian
@ BigEndian
Definition
byteorder.h:39
System::ByteOrder::LittleEndian
@ LittleEndian
Definition
byteorder.h:38
System::ByteOrder::GetFromByteOrder
Type GetFromByteOrder() const
get from-byte-order
Definition
byteorder.h:122
System::ByteOrder::SetToByteOrder
void SetToByteOrder(Type toByteOrder)
set to-byte-order
Definition
byteorder.h:131
System::ByteOrder::Convert
TYPE Convert(TYPE val) const
endian-convert by copy
mat4.h
System
Definition
osxsysfunc.h:15
System::ConvertInPlace< Math::vec4 >
__forceinline void ByteOrder::ConvertInPlace< Math::vec4 >(Type fromByteOrder, Type toByteOrder, Math::vec4 &val)
Definition
byteorder.h:578
Math::mat4
A 4x4 single point precision float matrix.
Definition
mat4.h:49
Math::mat4::row3
vec4 row3
Definition
mat4.h:143
Math::mat4::row1
vec4 row1
Definition
mat4.h:141
Math::mat4::row0
vec4 row0
Definition
mat4.h:140
Math::mat4::row2
vec4 row2
Definition
mat4.h:142
Math::vec4
A 4D vector.
Definition
vec4.h:24
Math::vec4::y
float y
Definition
vec4.h:95
Math::vec4::z
float z
Definition
vec4.h:95
Math::vec4::w
float w
Definition
vec4.h:95
Math::vec4::x
float x
Definition
vec4.h:95
types.h
ulong
unsigned long ulong
Definition
types.h:32
uint
unsigned int uint
Definition
types.h:33
ushort
unsigned short ushort
Definition
types.h:34
System::ByteOrder::PunDoubleULL
Definition
byteorder.h:81
System::ByteOrder::PunDoubleULL::u
unsigned long long u
Definition
byteorder.h:83
System::ByteOrder::PunDoubleULL::d
double d
Definition
byteorder.h:82
System::ByteOrder::PunFloatUL
Definition
byteorder.h:76
System::ByteOrder::PunFloatUL::f
float f
Definition
byteorder.h:77
System::ByteOrder::PunFloatUL::u
ulong u
Definition
byteorder.h:78
vec4.h
code
foundation
system
byteorder.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.