~ubuntu-branches/ubuntu/saucy/3depict/saucy

« back to all changes in this revision

Viewing changes to src/endianTest.h

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2013-05-17 00:52:39 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130517005239-7bl4mnhkvrhc2ba6
Tags: 0.0.13-1
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *      endianttest.h - Platform endian testing
3
 
 *      Copyright (C) 2011, D Haley 
4
 
 
5
 
 *      This program is free software: you can redistribute it and/or modify
6
 
 *      it under the terms of the GNU General Public License as published by
7
 
 *      the Free Software Foundation, either version 3 of the License, or
8
 
 *      (at your option) any later version.
9
 
 
10
 
 *      This program is distributed in the hope that it will be useful,
11
 
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *      GNU General Public License for more details.
14
 
 
15
 
 *      You should have received a copy of the GNU General Public License
16
 
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
*/
18
 
 
19
 
#ifndef _ENDIAN_TEST_H_
20
 
#define _ENDIAN_TEST_H_
21
 
#if defined (_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
22
 
#define __LITTLE_ENDIAN__
23
 
#else
24
 
#ifdef __linux__
25
 
#include <endian.h>
26
 
#endif
27
 
#endif
28
 
 
29
 
#ifdef __BYTE_ORDER
30
 
//if both are not defined it is TRUE!
31
 
#if __BYTE_ORDER == __BIG_ENDIAN
32
 
#ifndef __BIG_ENDIAN__
33
 
#define __BIG_ENDIAN__
34
 
#endif
35
 
#elif __BYTE_ORDER == __LITTLE_ENDIAN
36
 
#ifndef __LITTLE_ENDIAN__
37
 
#define __LITTLE_ENDIAN__
38
 
#endif
39
 
#elif __BYTE_ORDER == __PDP_ENDIAN
40
 
#ifndef __ARM_ENDIAN__
41
 
#define __ARM_ENDIAN__
42
 
#endif
43
 
#else
44
 
#error "Endian determination failed"
45
 
#endif
46
 
#endif
47
 
 
48
 
const int ENDIAN_TEST=1;
49
 
//Run-time detection
50
 
inline int is_bigendian() { return (*(char*)&ENDIAN_TEST) == 0 ;}
51
 
 
52
 
inline int is_littleendian() { return (*(char*)&ENDIAN_TEST) == 1 ;}
53
 
 
54
 
 
55
 
inline void floatSwapBytes(float *inFloat)
56
 
{
57
 
        //Use a union to avoid strict-aliasing error
58
 
        union FloatSwapUnion{
59
 
           float f;
60
 
           char c[4];
61
 
        } ;
62
 
        FloatSwapUnion fa,fb;
63
 
        fa.f = *inFloat;
64
 
 
65
 
        fb.c[0] = fa.c[3];
66
 
        fb.c[1] = fa.c[2];
67
 
        fb.c[2] = fa.c[1];
68
 
        fb.c[3] = fa.c[0];
69
 
 
70
 
        *inFloat=fb.f;
71
 
}
72
 
 
73
 
#endif