gnulliver.h 3.75 KB
Newer Older
Zhang Xin's avatar
Zhang Xin committed
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
//   gnulliver.h - header information for the gnulliver library.

// Copyright (C) 1994-2004 Paul Hardy
// Copyright (C) 2011 Alan W. Irwin
//
// This file is part of the timeephem software project.
//
// timeephem is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// timeephem is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with timeephem; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

#ifndef __GNULLIVER_H__
#define __GNULLIVER_H__

//
// Travels the middle of the road to swiftly gnullify
// the Big-endian / Little-endian controversy.
// Only works on machines with IEEE floating point.
//

// Swaps values between host order and network order without calls
// to htonl(), etc.  This is a symmetric conversion, so the same
// function works in both directions.  For example, to convert an
// integer between host byte ordering and network byte ordering (in
// either direction), use
//
//         int i, j;
//         j = gnulliver32(i);
//
// then use j in an equation (if you just read i from the net), or
// write j to the net (if i is an integer on your machine).
//
// The gnulliver routines work by setting a double to a certain value,
// then examining a unioned byte array.
//
// The functions are:
//
//      char gnulliver() - determine (or recalculate) endianness of host:
//         GNULLIVER_BIG, GNULLIVER_LITTLE, GNULLIVER_DEC
//      unsigned short       gnulliver16(unsigned short input)
//      unsigned             gnulliver32(unsigned input)
//      unsigned long        gnulliver64(unsigned long input)
//      unsigned long long   gnulliver128(unsigned long long input)
//      float                gnulliver32f(float input)
//      double               gnulliver64f(double input)
//      long double          gnulliver128f(long double input)
//

// Set up for dealing with function visibility issues.
#include "ephcomdll.h"

#define GNULLIVER_VERSION    "1.0"

#define GNULLIVER_SWAP16     4  // Swap adjacent bytes in 2-byte string
#define GNULLIVER_SWAP32     8  // Swap 2-byte pairs in 4-byte string
#define GNULLIVER_SWAP64     16 // Swap 4-byte pairs in 8-byte string
#define GNULLIVER_SWAP128    32 // Swap 8-byte pairs in 16-byte string
//
//   What we have to do to convert between network (Big-Endian) order and
//   host machine order.
//
#define GNULLIVER_NATIVE    1  // No swapping regardless.
#define GNULLIVER_BIG       2  // No swapping if this machine is Big-endian
#define GNULLIVER_LITTLE    ( GNULLIVER_SWAP16 | GNULLIVER_SWAP32 | GNULLIVER_SWAP64 | GNULLIVER_SWAP128 )
// This should work on a PDP-11 or VAX, but I have no way to test it.
#define GNULLIVER_DEC       ( GNULLIVER_SWAP32 | GNULLIVER_SWAP64 | GNULLIVER_SWAP128 )

// Public function declarations for the gnulliver library.

GNULLIVERDLLIMPEXP unsigned char
gnulliver();

GNULLIVERDLLIMPEXP unsigned short
gnulliver16( unsigned short input );

GNULLIVERDLLIMPEXP unsigned
gnulliver32( unsigned input );

GNULLIVERDLLIMPEXP unsigned long
gnulliver64( unsigned long input );

GNULLIVERDLLIMPEXP unsigned char *
gnulliver64c( unsigned char *ch );

GNULLIVERDLLIMPEXP unsigned long long
gnulliver128( unsigned long long input );

GNULLIVERDLLIMPEXP float
gnulliver32f( float input );

GNULLIVERDLLIMPEXP double
gnulliver64f( double input );

GNULLIVERDLLIMPEXP long double
gnulliver128f( long double input );

#endif  // __GNULLIVER_H__