Tanoda
BitConverterNonAlloc.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Runtime.InteropServices;
11
12namespace Leap.Unity {
13
14 public static class BitConverterNonAlloc {
15
16#if !ENABLE_IL2CPP
17 [ThreadStatic]
18 private static ConversionStruct _c;
19#endif
20
26 public static UInt16 ToUInt16(byte[] bytes, int offset = 0) {
27#if ENABLE_IL2CPP
28 unsafe {
29 fixed (void* ptr = &bytes[offset]) {
30 return *(UInt16*)ptr;
31 }
32 }
33#else
34 _c.Byte0 = bytes[offset++];
35 _c.Byte1 = bytes[offset++];
36 return _c.UInt16;
37#endif
38 }
44 public static Int16 ToInt16(byte[] bytes, int offset = 0) {
45#if ENABLE_IL2CPP
46 unsafe {
47 fixed (void* ptr = &bytes[offset]) {
48 return *(Int16*)ptr;
49 }
50 }
51#else
52 _c.Byte0 = bytes[offset++];
53 _c.Byte1 = bytes[offset++];
54 return _c.Int16;
55#endif
56 }
62 public static UInt32 ToUInt32(byte[] bytes, int offset = 0) {
63#if ENABLE_IL2CPP
64 unsafe {
65 fixed (void* ptr = &bytes[offset]) {
66 return *(UInt32*)ptr;
67 }
68 }
69#else
70 _c.Byte0 = bytes[offset++];
71 _c.Byte1 = bytes[offset++];
72 _c.Byte2 = bytes[offset++];
73 _c.Byte3 = bytes[offset++];
74 return _c.UInt32;
75#endif
76 }
82 public static Int32 ToInt32(byte[] bytes, int offset = 0) {
83#if ENABLE_IL2CPP
84 unsafe {
85 fixed (void* ptr = &bytes[offset]) {
86 return *(Int32*)ptr;
87 }
88 }
89#else
90 _c.Byte0 = bytes[offset++];
91 _c.Byte1 = bytes[offset++];
92 _c.Byte2 = bytes[offset++];
93 _c.Byte3 = bytes[offset++];
94 return _c.Int32;
95#endif
96 }
102 public static UInt64 ToUInt64(byte[] bytes, int offset = 0) {
103#if ENABLE_IL2CPP
104 unsafe {
105 fixed (void* ptr = &bytes[offset]) {
106 return *(UInt64*)ptr;
107 }
108 }
109#else
110 _c.Byte0 = bytes[offset++];
111 _c.Byte1 = bytes[offset++];
112 _c.Byte2 = bytes[offset++];
113 _c.Byte3 = bytes[offset++];
114 _c.Byte4 = bytes[offset++];
115 _c.Byte5 = bytes[offset++];
116 _c.Byte6 = bytes[offset++];
117 _c.Byte7 = bytes[offset++];
118 return _c.UInt64;
119#endif
120 }
126 public static Int64 ToInt64(byte[] bytes, int offset = 0) {
127#if ENABLE_IL2CPP
128 unsafe {
129 fixed (void* ptr = &bytes[offset]) {
130 return *(Int64*)ptr;
131 }
132 }
133#else
134 _c.Byte0 = bytes[offset++];
135 _c.Byte1 = bytes[offset++];
136 _c.Byte2 = bytes[offset++];
137 _c.Byte3 = bytes[offset++];
138 _c.Byte4 = bytes[offset++];
139 _c.Byte5 = bytes[offset++];
140 _c.Byte6 = bytes[offset++];
141 _c.Byte7 = bytes[offset++];
142 return _c.Int64;
143#endif
144 }
150 public static Single ToSingle(byte[] bytes, int offset = 0) {
151#if ENABLE_IL2CPP
152 unsafe {
153 fixed (void* ptr = &bytes[offset]) {
154 return *(Single*)ptr;
155 }
156 }
157#else
158 _c.Byte0 = bytes[offset++];
159 _c.Byte1 = bytes[offset++];
160 _c.Byte2 = bytes[offset++];
161 _c.Byte3 = bytes[offset++];
162 return _c.Single;
163#endif
164 }
170 public static Double ToDouble(byte[] bytes, int offset = 0) {
171#if ENABLE_IL2CPP
172 unsafe {
173 fixed (void* ptr = &bytes[offset]) {
174 return *(Double*)ptr;
175 }
176 }
177#else
178 _c.Byte0 = bytes[offset++];
179 _c.Byte1 = bytes[offset++];
180 _c.Byte2 = bytes[offset++];
181 _c.Byte3 = bytes[offset++];
182 _c.Byte4 = bytes[offset++];
183 _c.Byte5 = bytes[offset++];
184 _c.Byte6 = bytes[offset++];
185 _c.Byte7 = bytes[offset++];
186 return _c.Double;
187#endif
188 }
189
198 public static UInt16 ToUInt16(byte[] bytes, ref int offset) {
199#if ENABLE_IL2CPP
200 unsafe {
201 fixed (void* ptr = &bytes[offset]) {
202 offset += sizeof(UInt16);
203 return *(UInt16*)ptr;
204 }
205 }
206#else
207 _c.Byte0 = bytes[offset++];
208 _c.Byte1 = bytes[offset++];
209 return _c.UInt16;
210#endif
211 }
212
221 public static Int16 ToInt16(byte[] bytes, ref int offset) {
222#if ENABLE_IL2CPP
223 unsafe {
224 fixed (void* ptr = &bytes[offset]) {
225 offset += sizeof(Int16);
226 return *(Int16*)ptr;
227 }
228 }
229#else
230 _c.Byte0 = bytes[offset++];
231 _c.Byte1 = bytes[offset++];
232 return _c.Int16;
233#endif
234 }
235
244 public static UInt32 ToUInt32(byte[] bytes, ref int offset) {
245#if ENABLE_IL2CPP
246 unsafe {
247 fixed (void* ptr = &bytes[offset]) {
248 offset += sizeof(UInt32);
249 return *(UInt32*)ptr;
250 }
251 }
252#else
253 _c.Byte0 = bytes[offset++];
254 _c.Byte1 = bytes[offset++];
255 _c.Byte2 = bytes[offset++];
256 _c.Byte3 = bytes[offset++];
257 return _c.UInt32;
258#endif
259 }
260
269 public static Int32 ToInt32(byte[] bytes, ref int offset) {
270#if ENABLE_IL2CPP
271 unsafe {
272 fixed (void* ptr = &bytes[offset]) {
273 offset += sizeof(Int32);
274 return *(Int32*)ptr;
275 }
276 }
277#else
278 _c.Byte0 = bytes[offset++];
279 _c.Byte1 = bytes[offset++];
280 _c.Byte2 = bytes[offset++];
281 _c.Byte3 = bytes[offset++];
282 return _c.Int32;
283#endif
284 }
285
294 public static UInt64 ToUInt64(byte[] bytes, ref int offset) {
295#if ENABLE_IL2CPP
296 unsafe {
297 fixed (void* ptr = &bytes[offset]) {
298 offset += sizeof(UInt64);
299 return *(UInt64*)ptr;
300 }
301 }
302#else
303 _c.Byte0 = bytes[offset++];
304 _c.Byte1 = bytes[offset++];
305 _c.Byte2 = bytes[offset++];
306 _c.Byte3 = bytes[offset++];
307 _c.Byte4 = bytes[offset++];
308 _c.Byte5 = bytes[offset++];
309 _c.Byte6 = bytes[offset++];
310 _c.Byte7 = bytes[offset++];
311 return _c.UInt64;
312#endif
313 }
314
323 public static Int64 ToInt64(byte[] bytes, ref int offset) {
324#if ENABLE_IL2CPP
325 unsafe {
326 fixed (void* ptr = &bytes[offset]) {
327 offset += sizeof(Int64);
328 return *(Int64*)ptr;
329 }
330 }
331#else
332 _c.Byte0 = bytes[offset++];
333 _c.Byte1 = bytes[offset++];
334 _c.Byte2 = bytes[offset++];
335 _c.Byte3 = bytes[offset++];
336 _c.Byte4 = bytes[offset++];
337 _c.Byte5 = bytes[offset++];
338 _c.Byte6 = bytes[offset++];
339 _c.Byte7 = bytes[offset++];
340 return _c.Int64;
341#endif
342 }
343
352 public static Single ToSingle(byte[] bytes, ref int offset) {
353#if ENABLE_IL2CPP
354 unsafe {
355 fixed (void* ptr = &bytes[offset]) {
356 offset += sizeof(Single);
357 return *(Single*)ptr;
358 }
359 }
360#else
361 _c.Byte0 = bytes[offset++];
362 _c.Byte1 = bytes[offset++];
363 _c.Byte2 = bytes[offset++];
364 _c.Byte3 = bytes[offset++];
365 return _c.Single;
366#endif
367 }
368
377 public static Double ToDouble(byte[] bytes, ref int offset) {
378#if ENABLE_IL2CPP
379 unsafe {
380 fixed (void* ptr = &bytes[offset]) {
381 offset += sizeof(Double);
382 return *(Double*)ptr;
383 }
384 }
385#else
386 _c.Byte0 = bytes[offset++];
387 _c.Byte1 = bytes[offset++];
388 _c.Byte2 = bytes[offset++];
389 _c.Byte3 = bytes[offset++];
390 _c.Byte4 = bytes[offset++];
391 _c.Byte5 = bytes[offset++];
392 _c.Byte6 = bytes[offset++];
393 _c.Byte7 = bytes[offset++];
394 return _c.Double;
395#endif
396 }
397
403 public static void GetBytes(UInt16 value, byte[] bytes, int offset = 0) {
404#if ENABLE_IL2CPP
405 unsafe {
406 fixed (void* ptr = &bytes[offset]) {
407 *(UInt16*)ptr = value;
408 }
409 }
410#else
411 _c.UInt16 = value;
412 bytes[offset++] = _c.Byte0;
413 bytes[offset++] = _c.Byte1;
414#endif
415 }
416
422 public static void GetBytes(Int16 value, byte[] bytes, int offset = 0) {
423#if ENABLE_IL2CPP
424 unsafe {
425 fixed (void* ptr = &bytes[offset]) {
426 *(Int16*)ptr = value;
427 }
428 }
429#else
430 _c.Int16 = value;
431 bytes[offset++] = _c.Byte0;
432 bytes[offset++] = _c.Byte1;
433#endif
434 }
435
441 public static void GetBytes(UInt32 value, byte[] bytes, int offset = 0) {
442#if ENABLE_IL2CPP
443 unsafe {
444 fixed (void* ptr = &bytes[offset]) {
445 *(UInt32*)ptr = value;
446 }
447 }
448#else
449 _c.UInt32 = value;
450 bytes[offset++] = _c.Byte0;
451 bytes[offset++] = _c.Byte1;
452 bytes[offset++] = _c.Byte2;
453 bytes[offset++] = _c.Byte3;
454#endif
455 }
456
462 public static void GetBytes(Int32 value, byte[] bytes, int offset = 0) {
463#if ENABLE_IL2CPP
464 unsafe {
465 fixed (void* ptr = &bytes[offset]) {
466 *(Int32*)ptr = value;
467 }
468 }
469#else
470 _c.Int32 = value;
471 bytes[offset++] = _c.Byte0;
472 bytes[offset++] = _c.Byte1;
473 bytes[offset++] = _c.Byte2;
474 bytes[offset++] = _c.Byte3;
475#endif
476 }
477
483 public static void GetBytes(UInt64 value, byte[] bytes, int offset = 0) {
484#if ENABLE_IL2CPP
485 unsafe {
486 fixed (void* ptr = &bytes[offset]) {
487 *(UInt64*)ptr = value;
488 }
489 }
490#else
491 _c.UInt64 = value;
492 bytes[offset++] = _c.Byte0;
493 bytes[offset++] = _c.Byte1;
494 bytes[offset++] = _c.Byte2;
495 bytes[offset++] = _c.Byte3;
496 bytes[offset++] = _c.Byte4;
497 bytes[offset++] = _c.Byte5;
498 bytes[offset++] = _c.Byte6;
499 bytes[offset++] = _c.Byte7;
500#endif
501 }
502
508 public static void GetBytes(Int64 value, byte[] bytes, int offset = 0) {
509#if ENABLE_IL2CPP
510 unsafe {
511 fixed (void* ptr = &bytes[offset]) {
512 *(Int64*)ptr = value;
513 }
514 }
515#else
516 _c.Int64 = value;
517 bytes[offset++] = _c.Byte0;
518 bytes[offset++] = _c.Byte1;
519 bytes[offset++] = _c.Byte2;
520 bytes[offset++] = _c.Byte3;
521 bytes[offset++] = _c.Byte4;
522 bytes[offset++] = _c.Byte5;
523 bytes[offset++] = _c.Byte6;
524 bytes[offset++] = _c.Byte7;
525#endif
526 }
527
533 public static void GetBytes(Single value, byte[] bytes, int offset = 0) {
534#if ENABLE_IL2CPP
535 unsafe {
536 fixed (void* ptr = &bytes[offset]) {
537 *(Single*)ptr = value;
538 }
539 }
540#else
541 _c.Single = value;
542 bytes[offset++] = _c.Byte0;
543 bytes[offset++] = _c.Byte1;
544 bytes[offset++] = _c.Byte2;
545 bytes[offset++] = _c.Byte3;
546#endif
547 }
548
554 public static void GetBytes(Double value, byte[] bytes, int offset = 0) {
555#if ENABLE_IL2CPP
556 unsafe {
557 fixed (void* ptr = &bytes[offset]) {
558 *(Double*)ptr = value;
559 }
560 }
561#else
562 _c.Double = value;
563 bytes[offset++] = _c.Byte0;
564 bytes[offset++] = _c.Byte1;
565 bytes[offset++] = _c.Byte2;
566 bytes[offset++] = _c.Byte3;
567 bytes[offset++] = _c.Byte4;
568 bytes[offset++] = _c.Byte5;
569 bytes[offset++] = _c.Byte6;
570 bytes[offset++] = _c.Byte7;
571#endif
572 }
573
582 public static void GetBytes(UInt16 value, byte[] bytes, ref int offset) {
583#if ENABLE_IL2CPP
584 unsafe {
585 fixed (void* ptr = &bytes[offset]) {
586 offset += sizeof(UInt16);
587 *(UInt16*)ptr = value;
588 }
589 }
590#else
591 _c.UInt16 = value;
592 bytes[offset++] = _c.Byte0;
593 bytes[offset++] = _c.Byte1;
594#endif
595 }
596
605 public static void GetBytes(Int16 value, byte[] bytes, ref int offset) {
606#if ENABLE_IL2CPP
607 unsafe {
608 fixed (void* ptr = &bytes[offset]) {
609 offset += sizeof(Int16);
610 *(Int16*)ptr = value;
611 }
612 }
613#else
614 _c.Int16 = value;
615 bytes[offset++] = _c.Byte0;
616 bytes[offset++] = _c.Byte1;
617#endif
618 }
619
628 public static void GetBytes(UInt32 value, byte[] bytes, ref int offset) {
629#if ENABLE_IL2CPP
630 unsafe {
631 fixed (void* ptr = &bytes[offset]) {
632 offset += sizeof(UInt32);
633 *(UInt32*)ptr = value;
634 }
635 }
636#else
637 _c.UInt32 = value;
638 bytes[offset++] = _c.Byte0;
639 bytes[offset++] = _c.Byte1;
640 bytes[offset++] = _c.Byte2;
641 bytes[offset++] = _c.Byte3;
642#endif
643 }
644
653 public static void GetBytes(Int32 value, byte[] bytes, ref int offset) {
654#if ENABLE_IL2CPP
655 unsafe {
656 fixed (void* ptr = &bytes[offset]) {
657 offset += sizeof(Int32);
658 *(Int32*)ptr = value;
659 }
660 }
661#else
662 _c.Int32 = value;
663 bytes[offset++] = _c.Byte0;
664 bytes[offset++] = _c.Byte1;
665 bytes[offset++] = _c.Byte2;
666 bytes[offset++] = _c.Byte3;
667#endif
668 }
669
678 public static void GetBytes(UInt64 value, byte[] bytes, ref int offset) {
679#if ENABLE_IL2CPP
680 unsafe {
681 fixed (void* ptr = &bytes[offset]) {
682 offset += sizeof(UInt64);
683 *(UInt64*)ptr = value;
684 }
685 }
686#else
687 _c.UInt64 = value;
688 bytes[offset++] = _c.Byte0;
689 bytes[offset++] = _c.Byte1;
690 bytes[offset++] = _c.Byte2;
691 bytes[offset++] = _c.Byte3;
692 bytes[offset++] = _c.Byte4;
693 bytes[offset++] = _c.Byte5;
694 bytes[offset++] = _c.Byte6;
695 bytes[offset++] = _c.Byte7;
696#endif
697 }
698
707 public static void GetBytes(Int64 value, byte[] bytes, ref int offset) {
708#if ENABLE_IL2CPP
709 unsafe {
710 fixed (void* ptr = &bytes[offset]) {
711 offset += sizeof(Int64);
712 *(Int64*)ptr = value;
713 }
714 }
715#else
716 _c.Int64 = value;
717 bytes[offset++] = _c.Byte0;
718 bytes[offset++] = _c.Byte1;
719 bytes[offset++] = _c.Byte2;
720 bytes[offset++] = _c.Byte3;
721 bytes[offset++] = _c.Byte4;
722 bytes[offset++] = _c.Byte5;
723 bytes[offset++] = _c.Byte6;
724 bytes[offset++] = _c.Byte7;
725#endif
726 }
727
736 public static void GetBytes(Single value, byte[] bytes, ref int offset) {
737#if ENABLE_IL2CPP
738 unsafe {
739 fixed (void* ptr = &bytes[offset]) {
740 offset += sizeof(Single);
741 *(Single*)ptr = value;
742 }
743 }
744#else
745 _c.Single = value;
746 bytes[offset++] = _c.Byte0;
747 bytes[offset++] = _c.Byte1;
748 bytes[offset++] = _c.Byte2;
749 bytes[offset++] = _c.Byte3;
750#endif
751 }
752
761 public static void GetBytes(Double value, byte[] bytes, ref int offset) {
762#if ENABLE_IL2CPP
763 unsafe {
764 fixed (void* ptr = &bytes[offset]) {
765 offset += sizeof(Double);
766 *(Double*)ptr = value;
767 }
768 }
769#else
770 _c.Double = value;
771 bytes[offset++] = _c.Byte0;
772 bytes[offset++] = _c.Byte1;
773 bytes[offset++] = _c.Byte2;
774 bytes[offset++] = _c.Byte3;
775 bytes[offset++] = _c.Byte4;
776 bytes[offset++] = _c.Byte5;
777 bytes[offset++] = _c.Byte6;
778 bytes[offset++] = _c.Byte7;
779#endif
780 }
781
782#if !ENABLE_IL2CPP
783 [StructLayout(LayoutKind.Explicit)]
784 private struct ConversionStruct {
785 [FieldOffset(0)]
786 public byte Byte0;
787 [FieldOffset(1)]
788 public byte Byte1;
789 [FieldOffset(2)]
790 public byte Byte2;
791 [FieldOffset(3)]
792 public byte Byte3;
793 [FieldOffset(4)]
794 public byte Byte4;
795 [FieldOffset(5)]
796 public byte Byte5;
797 [FieldOffset(6)]
798 public byte Byte6;
799 [FieldOffset(7)]
800 public byte Byte7;
801
802 [FieldOffset(0)]
803 public UInt16 UInt16;
804 [FieldOffset(0)]
805 public Int16 Int16;
806 [FieldOffset(0)]
807 public UInt32 UInt32;
808 [FieldOffset(0)]
809 public Int32 Int32;
810 [FieldOffset(0)]
811 public UInt64 UInt64;
812 [FieldOffset(0)]
813 public Int64 Int64;
814 [FieldOffset(0)]
815 public Single Single;
816 [FieldOffset(0)]
817 public Double Double;
818 }
819#endif
820 }
821}