~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to Common/Swap.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2012- PPSSPP Project / Dolphin Project.
 
2
 
 
3
// This program is free software: you can redistribute it and/or modify
 
4
// it under the terms of the GNU General Public License as published by
 
5
// the Free Software Foundation, version 2.0 or later versions.
 
6
 
 
7
// This program is distributed in the hope that it will be useful,
 
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
// GNU General Public License 2.0 for more details.
 
11
 
 
12
// A copy of the GPL 2.0 should have been included with the program.
 
13
// If not, see http://www.gnu.org/licenses/
 
14
 
 
15
// Official git repository and contact information can be found at
 
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
 
17
 
 
18
#pragma once
 
19
 
 
20
// Android
 
21
#if defined(ANDROID)
 
22
#include <sys/endian.h>
 
23
 
 
24
#if _BYTE_ORDER == _LITTLE_ENDIAN && !defined(COMMON_LITTLE_ENDIAN)
 
25
#define COMMON_LITTLE_ENDIAN 1
 
26
#elif _BYTE_ORDER == _BIG_ENDIAN && !defined(COMMON_BIG_ENDIAN)
 
27
#define COMMON_BIG_ENDIAN 1
 
28
#endif
 
29
 
 
30
// GCC 4.6+
 
31
#elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
 
32
 
 
33
#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) && !defined(COMMON_LITTLE_ENDIAN)
 
34
#define COMMON_LITTLE_ENDIAN 1
 
35
#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && !defined(COMMON_BIG_ENDIAN)
 
36
#define COMMON_BIG_ENDIAN 1
 
37
#endif
 
38
 
 
39
// LLVM/clang
 
40
#elif __clang__
 
41
 
 
42
#if __LITTLE_ENDIAN__ && !defined(COMMON_LITTLE_ENDIAN)
 
43
#define COMMON_LITTLE_ENDIAN 1
 
44
#elif __BIG_ENDIAN__ && !defined(COMMON_BIG_ENDIAN)
 
45
#define COMMON_BIG_ENDIAN 1
 
46
#endif
 
47
 
 
48
// MSVC
 
49
#elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN)
 
50
 
 
51
#ifdef _XBOX
 
52
#define COMMON_BIG_ENDIAN 1
 
53
#else
 
54
#define COMMON_LITTLE_ENDIAN 1
 
55
#endif
 
56
 
 
57
#endif
 
58
 
 
59
// Worst case, default to little endian.
 
60
#if !COMMON_BIG_ENDIAN && !COMMON_LITTLE_ENDIAN
 
61
#define COMMON_LITTLE_ENDIAN 1
 
62
#endif
 
63
 
 
64
#ifdef _MSC_VER
 
65
#ifndef _XBOX
 
66
inline unsigned long long bswap64(unsigned long long x) { return _byteswap_uint64(x); }
 
67
inline unsigned int bswap32(unsigned int x) { return _byteswap_ulong(x); }
 
68
inline unsigned short bswap16(unsigned short x) { return _byteswap_ushort(x); }
 
69
#else
 
70
inline unsigned long long bswap64(unsigned long long x) { return __loaddoublewordbytereverse(0, &x); }
 
71
inline unsigned int bswap32(unsigned int x) { return __loadwordbytereverse(0, &x); }
 
72
inline unsigned short bswap16(unsigned short x) { return __loadshortbytereverse(0, &x); }
 
73
#endif
 
74
#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
 
75
      defined(__NetBSD__) || defined(__OpenBSD__)
 
76
#include <sys/endian.h>
 
77
# ifdef __OpenBSD__
 
78
#define bswap16 swap16
 
79
#define bswap32 swap32
 
80
#define bswap64 swap64
 
81
#define
 
82
# endif
 
83
#else
 
84
// TODO: speedup
 
85
inline unsigned short bswap16(unsigned short x) { return (x << 8) | (x >> 8); }
 
86
inline unsigned int bswap32(unsigned int x) { return (x >> 24) | ((x & 0xFF0000) >> 8) | ((x & 0xFF00) << 8) | (x << 24); }
 
87
inline unsigned long long bswap64(unsigned long long x) { return ((unsigned long long)bswap32(x) << 32) | bswap32(x >> 32); }
 
88
#endif
 
89
 
 
90
inline float bswapf(float f) {
 
91
        union {
 
92
                float f;
 
93
                unsigned int u32;
 
94
        } dat1, dat2;
 
95
 
 
96
        dat1.f = f;
 
97
        dat2.u32 = bswap32(dat1.u32);
 
98
 
 
99
        return dat2.f;
 
100
}
 
101
 
 
102
inline double bswapd(double f) {
 
103
        union {
 
104
                double f;
 
105
                unsigned long long u64;
 
106
        } dat1, dat2;
 
107
 
 
108
        dat1.f = f;
 
109
        dat2.u64 = bswap64(dat1.u64);
 
110
 
 
111
        return dat2.f;
 
112
}
 
113
 
 
114
template <typename T, typename F>
 
115
struct swap_struct_t {
 
116
        typedef swap_struct_t<T, F> swapped_t;
 
117
 
 
118
protected:
 
119
        T value;
 
120
 
 
121
        static T swap(T v) {
 
122
                return F::swap(v);
 
123
        }
 
124
public:
 
125
        T const swap() const {
 
126
                return swap(value);
 
127
        }
 
128
        swap_struct_t() : value((T)0) {}
 
129
        swap_struct_t(const T &v): value(swap(v)) {}
 
130
 
 
131
        template <typename S>
 
132
        swapped_t& operator=(const S &source) {
 
133
                value = swap((T)source);
 
134
                return *this;
 
135
        }
 
136
 
 
137
        operator unsigned long() const { return (unsigned long)swap(); }
 
138
        operator long() const { return (long)swap(); }  
 
139
        operator s8() const { return (s8)swap(); }
 
140
        operator u8() const { return (u8)swap(); }
 
141
        operator s16() const { return (s16)swap(); }
 
142
        operator u16() const { return (u16)swap(); }
 
143
        operator s32() const { return (s32)swap(); }
 
144
        operator u32() const { return (u32)swap(); }
 
145
        operator s64() const { return (s64)swap(); }
 
146
        operator u64() const { return (u64)swap(); }
 
147
        operator float() const { return (float)swap(); }
 
148
        operator double() const { return (double)swap(); }
 
149
 
 
150
        // +v
 
151
        swapped_t operator +() const {
 
152
                return +swap();
 
153
        }
 
154
        // -v
 
155
        swapped_t operator -() const {
 
156
                return -swap();
 
157
        }
 
158
 
 
159
        // v / 5
 
160
        swapped_t operator/(const swapped_t &i) const {
 
161
                return swap() / i.swap();
 
162
        }
 
163
        template <typename S>
 
164
        swapped_t operator/(const S &i) const {
 
165
                return swap() / i;
 
166
        }
 
167
 
 
168
        // v * 5
 
169
        swapped_t operator*(const swapped_t &i) const {
 
170
                return swap() * i.swap();
 
171
        }
 
172
        template <typename S>
 
173
        swapped_t operator*(const S &i) const {
 
174
                return swap() * i;
 
175
        }
 
176
 
 
177
        // v + 5
 
178
        swapped_t operator+(const swapped_t &i) const {
 
179
                return swap() + i.swap();
 
180
        }
 
181
        template <typename S>
 
182
        swapped_t operator+(const S &i) const {
 
183
                return swap() + (T)i;
 
184
        }
 
185
        // v - 5
 
186
        swapped_t operator-(const swapped_t &i) const {
 
187
                return swap() - i.swap();
 
188
        }
 
189
        template <typename S>
 
190
        swapped_t operator-(const S &i) const {
 
191
                return swap() - (T)i;
 
192
        }
 
193
 
 
194
        // v += 5
 
195
        swapped_t& operator+=(const swapped_t &i) {
 
196
                value = swap(swap() + i.swap());
 
197
                return *this;
 
198
        }
 
199
        template <typename S>
 
200
        swapped_t& operator+=(const S &i) {
 
201
                value = swap(swap() + (T)i);
 
202
                return *this;
 
203
        }
 
204
        // v -= 5
 
205
        swapped_t& operator-=(const swapped_t &i) {
 
206
                value = swap(swap() - i.swap());
 
207
                return *this;
 
208
        }
 
209
        template <typename S>
 
210
        swapped_t& operator-=(const S &i) {
 
211
                value = swap(swap() - (T)i);
 
212
                return *this;
 
213
        }
 
214
 
 
215
        // ++v
 
216
        swapped_t& operator++() {
 
217
                value = swap(swap()+1);
 
218
                return *this;
 
219
        }
 
220
        // --v
 
221
        swapped_t& operator--()  {
 
222
                value = swap(swap()-1);
 
223
                return *this;
 
224
        }
 
225
 
 
226
        // v++
 
227
        swapped_t operator++(int) {
 
228
                swapped_t old = *this;
 
229
                value = swap(swap()+1);
 
230
                return old;
 
231
        }
 
232
        // v--
 
233
        swapped_t operator--(int) {
 
234
                swapped_t old = *this;
 
235
                value = swap(swap()-1);
 
236
                return old;
 
237
        }
 
238
        // Comparaison
 
239
        // v == i
 
240
        bool operator==(const swapped_t &i) const {
 
241
                return swap() == i.swap();
 
242
        }
 
243
        template <typename S>
 
244
        bool operator==(const S &i) const {
 
245
                return swap() == i;
 
246
        }
 
247
 
 
248
        // v != i
 
249
        bool operator!=(const swapped_t &i) const {
 
250
                return swap() != i.swap();
 
251
        }
 
252
        template <typename S>
 
253
        bool operator!=(const S &i) const {
 
254
                return swap() != i;
 
255
        }
 
256
 
 
257
        // v > i
 
258
        bool operator>(const swapped_t &i) const {
 
259
                return swap() > i.swap();
 
260
        }
 
261
        template <typename S>
 
262
        bool operator>(const S &i) const {
 
263
                return swap() > i;
 
264
        }
 
265
 
 
266
        // v < i
 
267
        bool operator<(const swapped_t &i) const {
 
268
                return swap() < i.swap();
 
269
        }
 
270
        template <typename S>
 
271
        bool operator<(const S &i) const {
 
272
                return swap() < i;
 
273
        }
 
274
 
 
275
        // v >= i
 
276
        bool operator>=(const swapped_t &i) const {
 
277
                return swap() >= i.swap();
 
278
        }
 
279
        template <typename S>
 
280
        bool operator>=(const S &i) const {
 
281
                return swap() >= i;
 
282
        }
 
283
 
 
284
        // v <= i
 
285
        bool operator<=(const swapped_t &i) const {
 
286
                return swap() <= i.swap();
 
287
        }
 
288
        template <typename S>
 
289
        bool operator<=(const S &i) const {
 
290
                return swap() <= i;
 
291
        }
 
292
 
 
293
        // logical
 
294
        swapped_t operator !() const {
 
295
                return !swap();
 
296
        }
 
297
        
 
298
        bool operator ||(const swapped_t  & b) const {
 
299
                return swap() || b.swap();
 
300
        }
 
301
        template <typename S>
 
302
        bool operator ||(const S & b) const {
 
303
                return swap() || b;
 
304
        }
 
305
 
 
306
        // bitmath
 
307
        swapped_t operator ~() const {
 
308
                return ~swap();
 
309
        }
 
310
 
 
311
        swapped_t operator &(const swapped_t &b) const {
 
312
                return swap() & b.swap();
 
313
        }
 
314
        template <typename S>
 
315
        swapped_t operator &(const S &b) const {
 
316
                return swap() & b;
 
317
        }
 
318
        swapped_t& operator &=(const swapped_t &b) {
 
319
                value = swap(swap() & b.swap());
 
320
                return *this;
 
321
        }
 
322
        template <typename S>
 
323
        swapped_t& operator &=(const S b) {
 
324
                value = swap(swap() & b);
 
325
                return *this;
 
326
        }
 
327
 
 
328
        swapped_t operator |(const swapped_t &b) const {
 
329
                return swap() | b.swap();
 
330
        }
 
331
        template <typename S>
 
332
        swapped_t operator |(const S &b) const {
 
333
                return swap() | b;
 
334
        }
 
335
        swapped_t& operator |=(const swapped_t &b) {
 
336
                value = swap(swap() | b.swap());
 
337
                return *this;
 
338
        }
 
339
        template <typename S>
 
340
        swapped_t& operator |=(const S &b) {
 
341
                value = swap(swap() | b);
 
342
                return *this;
 
343
        }
 
344
 
 
345
        swapped_t operator ^(const swapped_t &b) const {
 
346
                return swap() ^ b.swap();
 
347
        }
 
348
        template <typename S>
 
349
        swapped_t operator ^(const S &b) const {
 
350
                return swap() ^ b;
 
351
        }
 
352
        swapped_t& operator ^=(const swapped_t &b) {
 
353
                value = swap(swap() ^ b.swap());
 
354
                return *this;
 
355
        }
 
356
        template <typename S>
 
357
        swapped_t& operator ^=(const S &b) {
 
358
                value = swap(swap() ^ b);
 
359
                return *this;
 
360
        }
 
361
 
 
362
        template <typename S>
 
363
        swapped_t operator <<(const S &b) const {
 
364
                return swap() << b;
 
365
        }
 
366
        template <typename S>
 
367
        swapped_t& operator <<=(const S &b) const {
 
368
                value = swap(swap() << b);
 
369
                return *this;
 
370
        }
 
371
 
 
372
        template <typename S>
 
373
        swapped_t operator >>(const S &b) const {
 
374
                return swap() >> b;
 
375
        }
 
376
        template <typename S>
 
377
        swapped_t& operator >>=(const S &b) const {
 
378
                value = swap(swap() >> b);
 
379
                return *this;
 
380
        }
 
381
 
 
382
        // Member
 
383
        /** todo **/
 
384
 
 
385
 
 
386
        // Arithmetics
 
387
        template <typename S, typename T2, typename F2>
 
388
        friend S operator+(const S &p, const swapped_t& v);
 
389
 
 
390
        template <typename S, typename T2, typename F2>
 
391
        friend S operator-(const S &p, const swapped_t& v);
 
392
 
 
393
        template <typename S, typename T2, typename F2>
 
394
        friend S operator/(const S &p, const swapped_t& v);
 
395
 
 
396
        template <typename S, typename T2, typename F2>
 
397
        friend S operator*(const S &p, const swapped_t& v);
 
398
 
 
399
        template <typename S, typename T2, typename F2>
 
400
        friend S operator%(const S &p, const swapped_t& v);
 
401
 
 
402
        // Arithmetics + assignements
 
403
        template <typename S, typename T2, typename F2>
 
404
        friend S operator+=(const S &p, const swapped_t& v);
 
405
 
 
406
        template <typename S, typename T2, typename F2>
 
407
        friend S operator-=(const S &p, const swapped_t& v);
 
408
 
 
409
        // Bitmath
 
410
        template <typename S, typename T2, typename F2>
 
411
        friend S operator&(const S &p, const swapped_t& v);
 
412
 
 
413
        // Comparison
 
414
        template <typename S, typename T2, typename F2>
 
415
        friend bool operator<(const S &p, const swapped_t& v);
 
416
 
 
417
        template <typename S, typename T2, typename F2>
 
418
        friend bool operator>(const S &p, const swapped_t& v);
 
419
 
 
420
        template <typename S, typename T2, typename F2>
 
421
        friend bool operator<=(const S &p, const swapped_t& v);
 
422
 
 
423
        template <typename S, typename T2, typename F2>
 
424
        friend bool operator>=(const S &p, const swapped_t& v);
 
425
 
 
426
        template <typename S, typename T2, typename F2>
 
427
        friend bool operator!=(const S &p, const swapped_t& v);
 
428
 
 
429
        template <typename S, typename T2, typename F2>
 
430
        friend bool operator==(const S &p, const swapped_t& v);
 
431
};
 
432
 
 
433
 
 
434
// Arithmetics
 
435
template <typename S, typename T, typename F>
 
436
S operator+(const S &i, const swap_struct_t<T, F>& v) {
 
437
        return i + v.swap();
 
438
}
 
439
 
 
440
template <typename S, typename T, typename F>
 
441
S operator-(const S &i, const swap_struct_t<T, F>& v) {
 
442
        return i - v.swap();
 
443
}
 
444
 
 
445
template <typename S, typename T, typename F>
 
446
S operator/(const S &i, const swap_struct_t<T, F>& v) {
 
447
        return i / v.swap();
 
448
}
 
449
 
 
450
template <typename S, typename T, typename F>
 
451
S operator*(const S &i, const swap_struct_t<T, F>& v) {
 
452
        return i * v.swap();
 
453
}
 
454
 
 
455
template <typename S, typename T, typename F>
 
456
S operator%(const S &i, const swap_struct_t<T, F>& v) {
 
457
        return i % v.swap();
 
458
}
 
459
 
 
460
// Arithmetics + assignements
 
461
template <typename S, typename T, typename F>
 
462
S &operator+=(S &i, const swap_struct_t<T, F>& v) {
 
463
        i += v.swap();
 
464
        return i;
 
465
}
 
466
 
 
467
template <typename S, typename T, typename F>
 
468
S &operator-=(S &i, const swap_struct_t<T, F>& v) {
 
469
        i -= v.swap();
 
470
        return i;
 
471
}
 
472
 
 
473
// Logical
 
474
template <typename S, typename T, typename F>
 
475
S operator&(const S &i, const swap_struct_t<T, F>& v) {
 
476
        return i & v.swap();
 
477
}
 
478
 
 
479
template <typename S, typename T, typename F>
 
480
S operator&(const swap_struct_t<T, F>& v, const S &i) {
 
481
        return (S)(v.swap() & i);
 
482
}
 
483
 
 
484
 
 
485
// Comparaison
 
486
template <typename S, typename T, typename F>
 
487
bool operator<(const S &p, const swap_struct_t<T, F>& v) {
 
488
        return p < v.swap();
 
489
}
 
490
template <typename S, typename T, typename F>
 
491
bool operator>(const S &p, const swap_struct_t<T, F>& v) {
 
492
        return p > v.swap();
 
493
}
 
494
template <typename S, typename T, typename F>
 
495
bool operator<=(const S &p, const swap_struct_t<T, F>& v) {
 
496
        return p <= v.swap();
 
497
}
 
498
template <typename S, typename T, typename F>
 
499
bool operator>=(const S &p, const swap_struct_t<T, F>& v) {
 
500
        return p >= v.swap();
 
501
}
 
502
template <typename S, typename T, typename F>
 
503
bool operator!=(const S &p, const swap_struct_t<T, F>& v) {
 
504
        return p != v.swap();
 
505
}
 
506
template <typename S, typename T, typename F>
 
507
bool operator==(const S &p, const swap_struct_t<T, F>& v) {
 
508
        return p == v.swap();
 
509
}
 
510
 
 
511
template <typename T>
 
512
struct swap_64_t {
 
513
        static T swap(T x) {
 
514
                return (T)bswap64(*(u64 *)&x);
 
515
        }
 
516
};
 
517
 
 
518
template <typename T>
 
519
struct swap_32_t {
 
520
        static T swap(T x) {
 
521
                return (T)bswap32(*(u32 *)&x);
 
522
        }
 
523
};
 
524
 
 
525
template <typename T>
 
526
struct swap_16_t {
 
527
        static T swap(T x) {
 
528
                return (T)bswap16(*(u16 *)&x);
 
529
        }
 
530
};
 
531
 
 
532
template <typename T>
 
533
struct swap_float_t {
 
534
        static T swap(T x) {
 
535
                return (T)bswapf(*(float *)&x);
 
536
        }
 
537
};
 
538
 
 
539
template <typename T>
 
540
struct swap_double_t {
 
541
        static T swap(T x) {
 
542
                return (T)bswapd(*(double *)&x);
 
543
        }
 
544
};
 
545
 
 
546
#if COMMON_LITTLE_ENDIAN
 
547
typedef u32 u32_le;
 
548
typedef u16 u16_le;
 
549
typedef u64 u64_le;
 
550
 
 
551
typedef s32 s32_le;
 
552
typedef s16 s16_le;
 
553
typedef s64 s64_le;
 
554
 
 
555
typedef float float_le;
 
556
typedef double double_le;
 
557
 
 
558
typedef swap_struct_t<u64, swap_64_t<u64>> u64_be;
 
559
typedef swap_struct_t<s64, swap_64_t<s64>> s64_be;
 
560
 
 
561
typedef swap_struct_t<u32, swap_32_t<u32>> u32_be;
 
562
typedef swap_struct_t<s32, swap_32_t<s32>> s32_be;
 
563
 
 
564
typedef swap_struct_t<u16, swap_16_t<u16>> u16_be;
 
565
typedef swap_struct_t<s16, swap_16_t<s16>> s16_be;
 
566
 
 
567
typedef swap_struct_t<float, swap_float_t<float> > float_be;
 
568
typedef swap_struct_t<double, swap_double_t<double> > double_be;
 
569
#else
 
570
 
 
571
typedef swap_struct_t<u64, swap_64_t<u64>> u64_le;
 
572
typedef swap_struct_t<s64, swap_64_t<s64>> s64_le;
 
573
 
 
574
typedef swap_struct_t<u32, swap_32_t<u32>> u32_le;
 
575
typedef swap_struct_t<s32, swap_32_t<s32>> s32_le;
 
576
 
 
577
typedef swap_struct_t<u16, swap_16_t<u16>> u16_le;
 
578
typedef swap_struct_t<s16, swap_16_t<s16>> s16_le;
 
579
 
 
580
typedef swap_struct_t<float, swap_float_t<float> > float_le;
 
581
typedef swap_struct_t<double, swap_double_t<double> > double_le;
 
582
 
 
583
typedef u32 u32_be;
 
584
typedef u16 u16_be;
 
585
typedef u64 u64_be;
 
586
 
 
587
typedef s32 s32_be;
 
588
typedef s16 s16_be;
 
589
typedef s64 s64_be;
 
590
 
 
591
typedef float float_be;
 
592
typedef double double_be;
 
593
#endif