Nebula
Loading...
Searching...
No Matches
darwinbyteorder.h
Go to the documentation of this file.
1#pragma once
2#ifndef DARWIN_DARWINBYTEORDER_H
3#define DARWIN_DARWINBYTEORDER_H
4//------------------------------------------------------------------------------
11#include "core/types.h"
12#include <libkern/OSByteOrder.h>
13
14//------------------------------------------------------------------------------
15namespace System
16{
17class ByteOrder
18{
19public:
20 // byte orders
21 enum Type
22 {
23 LittleEndian = 0, // e.g. x86
24 BigEndian, // e.g. PowerPC
25 Network = BigEndian, // network byte order
26
27 #if __WIN32__
29 #else
31 #endif
32 };
33
37 ByteOrder(Type fromByteOrder, Type toByteOrder);
39 void SetFromByteOrder(Type fromByteOrder);
43 void SetToByteOrder(Type toByteOrder);
47 template<class TYPE> void Convert(TYPE& val) const;
48
49private:
50 Type from;
51 Type to;
52};
53
54//------------------------------------------------------------------------------
57__forceinline
59 from(Host),
60 to(Host)
61{
62 // empty
63}
64
65//------------------------------------------------------------------------------
68__forceinline
69ByteOrder::ByteOrder(ByteOrder::Type fromByteOrder, ByteOrder::Type toByteOrder) :
70 from(fromByteOrder),
71 to(toByteOrder)
72{
73 // empty
74}
75
76//------------------------------------------------------------------------------
79__forceinline void
80ByteOrder::SetFromByteOrder(Type fromByteOrder)
81{
82 this->from = fromByteOrder;
83}
84
85//------------------------------------------------------------------------------
88__forceinline ByteOrder::Type
90{
91 return this->from;
92}
93
94//------------------------------------------------------------------------------
97__forceinline void
98ByteOrder::SetToByteOrder(Type toByteOrder)
99{
100 this->to = toByteOrder;
101}
102
103//------------------------------------------------------------------------------
106__forceinline ByteOrder::Type
108{
109 return this->to;
110}
111
112//------------------------------------------------------------------------------
115template<> __forceinline void
116ByteOrder::Convert<short>(short& val) const
117{
118 if (this->from != this->to)
119 {
120 ushort res = OSSwapInt16(*(ushort*)&val);
121 val = *(short*)&res;
122 }
123}
124
125//------------------------------------------------------------------------------
128template<> __forceinline void
129ByteOrder::Convert<ushort>(ushort& val) const
130{
131 if (this->from != this->to)
132 {
133 val = OSSwapInt16(val);
134 }
135}
136
137//------------------------------------------------------------------------------
140template<> __forceinline void
141ByteOrder::Convert<int>(int& val) const
142{
143 if (this->from != this->to)
144 {
145 uint res = OSSwapInt32(*(uint*)&val);
146 val = *(int*)&res;
147 }
148}
149
150//------------------------------------------------------------------------------
153template<> __forceinline void
154ByteOrder::Convert<uint>(uint& val) const
155{
156 if (this->from != this->to)
157 {
158 val = OSSwapInt32(val);
159 }
160}
161
162//------------------------------------------------------------------------------
165template<> __forceinline void
166ByteOrder::Convert<float>(float& val) const
167{
168 if (this->from != this->to)
169 {
170 uint res = OSSwapInt32(*(uint*)&val);
171 val = *(float*)&res;
172 }
173}
174
175//------------------------------------------------------------------------------
178template<> __forceinline void
179ByteOrder::Convert<double>(double& val) const
180{
181 if (this->from != this->to)
182 {
183 unsigned long long res = OSSwapInt64(*(unsigned long long*)&val);
184 val = *(double*)&res;
185 }
186}
187
188//------------------------------------------------------------------------------
191template<> __forceinline void
192ByteOrder::Convert<Math::float4>(Math::float4& val) const
193{
194 if (this->from != this->to)
195 {
196 Convert<float>(val.x());
197 Convert<float>(val.y());
198 Convert<float>(val.z());
199 Convert<float>(val.w());
200 }
201}
202
203//------------------------------------------------------------------------------
206template<> __forceinline void
207ByteOrder::Convert<Math::matrix44>(Math::matrix44& val) const
208{
209 if (this->from != this->to)
210 {
211 Math::float4 row0 = val.getrow0();
212 Math::float4 row1 = val.getrow1();
213 Math::float4 row2 = val.getrow2();
214 Math::float4 row3 = val.getrow3();
219 val.setrow0(row0);
220 val.setrow1(row1);
221 val.setrow2(row2);
222 val.setrow3(row3);
223 }
224}
225
226} // namespace System
227//------------------------------------------------------------------------------
228#endif
(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
ByteOrder()
default constructor
Type from
Definition byteorder.h:72
void Convert(TYPE &val) const
endian-convert in place
Type GetToByteOrder() const
get to-byte-order
Definition byteorder.h:140
void SetToByteOrder(Type toByteOrder)
set to-byte-order
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
ByteOrder(Type fromByteOrder, Type toByteOrder)
constructor: set byte order conversion rule
void SetFromByteOrder(Type fromByteOrder)
set from-byte-order
void SetToByteOrder(Type toByteOrder)
set to-byte-order
Definition byteorder.h:131
Type GetToByteOrder() const
get to-byte-order
Type GetFromByteOrder() const
get from-byte-order
Definition osxsysfunc.h:15
__forceinline void ByteOrder::Convert< Math::float4 >(Math::float4 &val) const
Definition darwinbyteorder.h:192
Definition scalar.h:76
scalar x
Definition scalar.h:79
scalar y
Definition scalar.h:79
scalar w
Definition scalar.h:79
scalar z
Definition scalar.h:79
unsigned int uint
Definition types.h:31
unsigned short ushort
Definition types.h:32