~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/io/linux/TypeIOLinux.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2001-February-3              Jason Rohrer
 
5
 * Created.
 
6
 * Fixed a bug in the big-endian code.
 
7
 * Fixed parameter names to match convention.
 
8
 *
 
9
 * 2001-February-26             Jason Rohrer
 
10
 * Fixed a bug in the little-endian implementation.
 
11
 *
 
12
 * 2001-August-28               Jason Rohrer
 
13
 * Changed to be FreeBSD compatible. 
 
14
 *
 
15
 * 2002-March-13   Jason Rohrer
 
16
 * Started to change to work with solaris.
 
17
 * Finished changing to work with solaris.
 
18
 *
 
19
 * 2002-April-11   Jason Rohrer
 
20
 * Added a default BSD case to work with OSX.
 
21
 *
 
22
 * 2002-May-25   Jason Rohrer
 
23
 * Changed to use minorGems endian.h
 
24
 */
 
25
 
 
26
 
 
27
#include "minorGems/io/TypeIO.h"
 
28
#include "minorGems/system/endian.h"
 
29
 
 
30
 
 
31
 
 
32
 
 
33
/*
 
34
 * Linux-specific type input and output.
 
35
 * Note that all types are output in the order that
 
36
 * a big-endian linux machine outputs them with no conversion.
 
37
 */ 
 
38
 
 
39
 
 
40
 
 
41
#if __BYTE_ORDER == __LITTLE_ENDIAN
 
42
 
 
43
 
 
44
void TypeIO::doubleToBytes( double inDouble, unsigned char *outBytes ) {
 
45
        
 
46
        unsigned char *doubleBuffer = (unsigned char*)( &inDouble );
 
47
        
 
48
        // output second word first
 
49
        outBytes[0] = doubleBuffer[7];
 
50
        outBytes[1] = doubleBuffer[6];
 
51
        outBytes[2] = doubleBuffer[5];
 
52
        outBytes[3] = doubleBuffer[4];
 
53
        
 
54
        outBytes[4] = doubleBuffer[3];
 
55
        outBytes[5] = doubleBuffer[2];
 
56
        outBytes[6] = doubleBuffer[1];
 
57
        outBytes[7] = doubleBuffer[0];
 
58
        }
 
59
 
 
60
 
 
61
 
 
62
double TypeIO::bytesToDouble( unsigned char *inBytes ) {
 
63
        
 
64
        double returnValue;
 
65
        
 
66
        unsigned char *doubleBuffer = (unsigned char*)( &returnValue );
 
67
        
 
68
        // put first word at the end of this double
 
69
        doubleBuffer[7] = inBytes[0];
 
70
        doubleBuffer[6] = inBytes[1];
 
71
        doubleBuffer[5] = inBytes[2];
 
72
        doubleBuffer[4] = inBytes[3];
 
73
        
 
74
        doubleBuffer[3] = inBytes[4];
 
75
        doubleBuffer[2] = inBytes[5];
 
76
        doubleBuffer[1] = inBytes[6];
 
77
        doubleBuffer[0] = inBytes[7];
 
78
        
 
79
        return returnValue;
 
80
        }
 
81
 
 
82
 
 
83
 
 
84
#endif
 
85
 
 
86
 
 
87
 
 
88
#if __BYTE_ORDER == __BIG_ENDIAN
 
89
 
 
90
 
 
91
 
 
92
void TypeIO::doubleToBytes( double inDouble, unsigned char *outBytes ) {
 
93
        
 
94
        unsigned char *doubleBuffer = (unsigned char*)( &inDouble );
 
95
        
 
96
        // output in stored order
 
97
        outBytes[0] = doubleBuffer[0];
 
98
        outBytes[1] = doubleBuffer[1];
 
99
        outBytes[2] = doubleBuffer[2];
 
100
        outBytes[3] = doubleBuffer[3];
 
101
        
 
102
        outBytes[4] = doubleBuffer[4];
 
103
        outBytes[5] = doubleBuffer[5];
 
104
        outBytes[6] = doubleBuffer[6];
 
105
        outBytes[7] = doubleBuffer[7];
 
106
        }
 
107
 
 
108
 
 
109
 
 
110
double TypeIO::bytesToDouble( unsigned char *inBytes ) {
 
111
        
 
112
        double returnValue;
 
113
        
 
114
        unsigned char *doubleBuffer = (unsigned char*)( &returnValue );
 
115
        
 
116
        // store in input order
 
117
        doubleBuffer[0] = inBytes[0];
 
118
        doubleBuffer[1] = inBytes[1];
 
119
        doubleBuffer[2] = inBytes[2];
 
120
        doubleBuffer[3] = inBytes[3];
 
121
        
 
122
        doubleBuffer[4] = inBytes[4];
 
123
        doubleBuffer[5] = inBytes[5];
 
124
        doubleBuffer[6] = inBytes[6];
 
125
        doubleBuffer[7] = inBytes[7];
 
126
        
 
127
        return returnValue;
 
128
        }
 
129
        
 
130
        
 
131
        
 
132
#endif