Open Inventor Reference
SoMachine.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * Further, this software is distributed without any warranty that it is
16  * free of the rightful claim of any third person regarding infringement
17  * or the like. Any license provided herein, whether implied or
18  * otherwise, applies only to this software file. Patent licenses, if
19  * any, provided herein do not apply to combinations of this program with
20  * other software, or any other product whatsoever.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27  * Mountain View, CA 94043, or:
28  *
29  * http://www.sgi.com
30  *
31  * For further information regarding this notice, see:
32  *
33  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34  *
35  */
36 
37 #ifndef __MACHINE_H__
38 #define __MACHINE_H__
39 
40 /*
41  * Copyright (C) 1990,91,92,93 Silicon Graphics, Inc.
42  *
43  _______________________________________________________________________
44  ______________ S I L I C O N G R A P H I C S I N C . ____________
45  |
46  | $Revision: 1.5 $
47  |
48  | Description:
49  | This include file contains the machine-dependent macros
50  | and defines for converting basic datatypes across machines.
51  | This is only used by the SoInput and SoOutput classes defined
52  | in the Inventor lib/database source.
53  | TO ADD A NEW MACHINE:
54  |
55  | 1. Add a machine independent setup section
56  | (define MACHINE_WORD_FORMAT and MACHINE_FLOAT_FORMAT)
57  |
58  | 2. Define DGL_HTON_*, DGL_NTOH_*, and SHORT/INT32/FLOAT/DOUBLE
59  |
60  | IMPORTANT NOTE: The only complete examples found in this
61  | file is the SGI implementation. Note that the other
62  | implementations are incomplete!
63  |
64  | Author(s) : Dave Immel
65  |
66  ______________ S I L I C O N G R A P H I C S I N C . ____________
67  _______________________________________________________________________
68  */
69 
70 /*
71  * Defines for the various data formats
72  */
73 
74 #define DGL_LITTLE_ENDIAN 1 /* integer formats */
75 #define DGL_BIG_ENDIAN 2
76 
77 #define DGL_BIG_IEEE 1 /* floating point formats */
78 #define DGL_NON_IEEE 3
79 
80 /*
81  * Win32 machine dependent setup
82  */
83 
84 #if defined(WIN32)
85 
86 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
87 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
88 
89 #endif /* WIN32 */
90 
91 
92 /*
93  * Linux i386/ia64 machine dependent setup
94  */
95 
96 #if defined(__linux__) && (__i386__ || __ia64__ || __x86_64__ || __arm__ || __arm64__ || __aarch64__)
97 
98 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
99 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
100 
101 #endif /* __linux__ && (__i386__ || __ia64__) */
102 
103 
104 /*
105  * Apple Darwin (Mac OS X) machine dependent setup
106  */
107 
108 #if defined(__APPLE__)
109 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
110 
111 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
112 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
113 
114 #else /* __APPLE__ and not (__i386__ or __x86_64__) */
115 
116 #define MACHINE_WORD_FORMAT DGL_BIG_ENDIAN
117 #define MACHINE_FLOAT_FORMAT DGL_BIG_IEEE
118 
119 #endif /* __i386__ || __x86_64__ */
120 #endif /* __APPLE__ */
121 
122 /*
123  * 32/64-bit architecture dependent statements
124  */
125 
126 #define M_SIZEOF(x) sizeof(x)
127 
128 
129 /*
130  * Data conversion (byte swapping) algorithms:
131  * HTON - client host to network (server)
132  * NTOH - network (server) to client host
133  */
134 
135 #if MACHINE_WORD_FORMAT == DGL_BIG_ENDIAN
136 #define DGL_HTON_SHORT(t,f) t = f
137 #define DGL_NTOH_SHORT DGL_HTON_SHORT
138 #define DGL_HTON_INT32(t,f) t = f
139 #define DGL_NTOH_INT32 DGL_HTON_INT32
140 #endif /* MACHINE_WORD_FORMAT */
141 
142 /*
143  * DGL_BIG_IEEE: no conversion necessary (FLOAT)
144  */
145 
146 #if MACHINE_FLOAT_FORMAT == DGL_BIG_IEEE
147 #define DGL_HTON_FLOAT(t,f) t = f
148 #define DGL_NTOH_FLOAT DGL_HTON_FLOAT
149 #define DGL_HTON_DOUBLE(t,f) t = f
150 #define DGL_NTOH_DOUBLE DGL_HTON_DOUBLE
151 #endif
152 
153 /*
154  * DGL_LITTLE_ENDIAN: conversion necessary (INTEGER)
155  * NOTE: non-floating point conversions are the same for both
156  * directions and thus one macro suffices
157  */
158 
159 #if MACHINE_WORD_FORMAT == DGL_LITTLE_ENDIAN
160 
161 /* like DGL_HTON_INT32, but more efficient if f is a constant */
162 #define DGL_HTON_SHORT(t,f) \
163  { \
164  short _from = f,_to; \
165  ((char *)&_to)[0] = ((char *)&_from)[1]; \
166  ((char *)&_to)[1] = ((char *)&_from)[0]; \
167  t = _to; \
168  }
169 #define DGL_NTOH_SHORT DGL_HTON_SHORT
170 #define DGL_HTON_INT32(t,f) \
171  { \
172  int32_t _from = f,_to; \
173  ((char *)&_to)[0] = ((char *)&_from)[3]; \
174  ((char *)&_to)[1] = ((char *)&_from)[2]; \
175  ((char *)&_to)[2] = ((char *)&_from)[1]; \
176  ((char *)&_to)[3] = ((char *)&_from)[0]; \
177  t = _to; \
178  }
179 #define DGL_NTOH_INT32 DGL_HTON_INT32
180 
181 #endif /* LITTLE_ENDIAN */
182 
183 
184 /*
185  * DGL_NON_IEEE: conversion necessary (FLOAT)
186  * conversion is done within procedure calls for simplicity
187  */
188 
189 #if MACHINE_FLOAT_FORMAT == DGL_NON_IEEE
190 #if __i386__ || __x86_64__ || _M_X64 || __ia64__ || __arm__ || __arm64__ || __aarch64__
191 void mem_hton_float(float *t, float *f);
192 void mem_ntoh_float(float *t, float *f);
193 void mem_hton_double(double *t, double *f);
194 void mem_ntoh_double(double *t, double *f);
195 #endif /* __i386__ || __x86_64__ || __ia64__ */
196 #define DGL_HTON_FLOAT(t,f) mem_hton_float(&t,&f)
197 #define DGL_NTOH_FLOAT(t,f) mem_ntoh_float(&t,&f)
198 #define DGL_HTON_DOUBLE(t,f) mem_hton_double(&t,&f)
199 #define DGL_NTOH_DOUBLE(t,f) mem_ntoh_double(&t,&f)
200 #endif
201 
202 
203 /*
204  * get/set a data item located at address p regardless what it really is
205  */
206 
207 #define INT32(p) (*(int32_t *)(p))
208 #define FLOAT(p) (*(float *)(p))
209 #define DOUBLE(p) (*(double *)(p))
210 #define SHORT(p) (*(short *)(p))
211 
212 #endif /* __MACHINE_H__ */