aboutsummaryrefslogtreecommitdiff
path: root/sibs/endian.hpp
blob: 13e6a3121b770f6868bcc2f1f528af5616241894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once

/*
Endian macro copied from https://github.com/xxtea/xxtea-c/blob/master/xxtea.c
License for the macro definition:
The MIT License (MIT)
Copyright (c) 2008-2015 Ma Bingyao mabingyao@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/types.h> /* This will likely define BYTE_ORDER */
#ifndef BYTE_ORDER
#if (BSD >= 199103)
# include <machine/endian.h>
#else
#if defined(linux) || defined(__linux__)
# include <endian.h>
#else
#define LITTLE_ENDIAN   1234    /* least-significant byte first (vax, pc) */
#define BIG_ENDIAN  4321    /* most-significant byte first (IBM, net) */
#define PDP_ENDIAN  3412    /* LSB first in word, MSW first in long (pdp)*/
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
defined(vax) || defined(ns32000) || defined(sun386) || \
defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
defined(__alpha__) || defined(__alpha)
#define BYTE_ORDER    LITTLE_ENDIAN
#endif
#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
    defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
    defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
    defined(apollo) || defined(__convex__) || defined(_CRAY) || \
    defined(__hppa) || defined(__hp9000) || \
    defined(__hp9000s300) || defined(__hp9000s700) || \
    defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
#define BYTE_ORDER  BIG_ENDIAN
#endif
#endif /* linux */
#endif /* BSD */
#endif /* BYTE_ORDER */
#if defined(WIN32) || defined(_WIN32)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#ifndef BYTE_ORDER
#ifdef __BYTE_ORDER
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
#endif
#endif
    
#include "types.hpp"
#include <cstdlib>

namespace sibs
{
    #if defined(_MSC_VER)
        static u16 byteswap(u16 value)
        {
            return _byteswap_ushort(value);
        }
        static u32 byteswap(u32 value)
        {
            return _byteswap_ulong(value);
        }
        static u64 byteswap(u64 value)
        {
            return _byteswap_uint64(value);
        }
    #elif defined(__GCC__)
        static u16 byteswap(u16 value)
        {
            return __builtin_bswap16(value);
        }
        static u32 byteswap(u32 value)
        {
            return __builtin_bswap32(value);
        }
        static u64 byteswap(u64 value)
        {
            return __builtin_bswap64(value);
        }
    #else
        static u16 byteswap(u16 value)
        {
            u16 result = 0;
            result |= (value & 0x00FF) << 8;
            result |= (value & 0xFF00) >> 8;
            return result;
        }
        static u32 byteswap(u32 value)
        {
            u32 result = 0;
            result |= (value & 0x000000FF) << 24;
            result |= (value & 0x0000FF00) << 8;
            result |= (value & 0x00FF0000) >> 8;
            result |= (value & 0xFF000000) >> 24;
            return result;
        }
        static u64 byteswap(u64 value)
        {
            u64 result = 0;
            result |= (value & 0x00000000000000FF) << 56;
            result |= (value & 0x000000000000FF00) << 40;
            result |= (value & 0x0000000000FF0000) << 24;
            result |= (value & 0x00000000FF000000) << 8;
            result |= (value & 0x000000FF00000000) >> 8;
            result |= (value & 0x0000FF0000000000) >> 24;
            result |= (value & 0x00FF000000000000) >> 40;
            result |= (value & 0xFF00000000000000) >> 56;
            return result;
        }
    #endif
    
    static u16 byteswap16(u16 value)
    {
        return byteswap(value);
    }
    
    static u32 byteswap32(u32 value)
    {
        return byteswap(value);
    }
    
    static u64 byteswap64(u64 value)
    {
        return byteswap(value);
    }

    enum class Endianness
    {
        LITTLE,
        BIG,
        MIDDLE, // TODO: Implement conversion from/to middle endian
        UNKNOWN
    };

    static Endianness getEndianness()
    {
        u32 endian = 0x12345678;
        u8 *endianByte = (u8*)&endian;
        if (endianByte[0] == (u8)0x78 && endianByte[1] == (u8)0x56)
            return Endianness::LITTLE;
        else if (endianByte[0] == (u8)0x12 && endianByte[1] == (u8)0x34)
            return Endianness::BIG;
        else
            return Endianness::UNKNOWN;
    }
}