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  * Win32 machine dependent setup
72  */
73 
74 #if defined(WIN32)
75 #if !defined(__i386__)
76 #define __i386__ 1
77 #endif /* not __i386__ */
78 
79 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
80 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
81 
82 #endif /* WIN32 */
83 
84 
85 /*
86  * Linux i386/ia64 machine dependent setup
87  */
88 
89 #if defined(__linux__) && (__i386__ || __ia64__ || __x86_64__ || __arm__ || __arm64__ || __aarch64__)
90 
91 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
92 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
93 
94 #endif /* __linux__ && (__i386__ || __ia64__) */
95 
96 
97 /*
98  * Apple Darwin (Mac OS X) machine dependent setup
99  */
100 
101 #if defined(__APPLE__)
102 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
103 
104 #define MACHINE_WORD_FORMAT DGL_LITTLE_ENDIAN
105 #define MACHINE_FLOAT_FORMAT DGL_NON_IEEE
106 
107 #else /* __APPLE__ and not (__i386__ or __x86_64__) */
108 
109 #define MACHINE_WORD_FORMAT DGL_BIG_ENDIAN
110 #define MACHINE_FLOAT_FORMAT DGL_BIG_IEEE
111 
112 #endif /* __i386__ || __x86_64__ */
113 #endif /* __APPLE__ */
114 
115 /*
116  * 32/64-bit architecture dependent statements
117  */
118 
119 #define M_SIZEOF(x) sizeof(x)
120 
121 /*
122  * Defines for the various data formats
123  */
124 
125 #define DGL_LITTLE_ENDIAN 1 /* integer formats */
126 #define DGL_BIG_ENDIAN 2
127 
128 #define DGL_BIG_IEEE 1 /* floating point formats */
129 #define DGL_NON_IEEE 3
130 
131 
132 /*
133  * Data conversion (byte swapping) algorithms:
134  * HTON - client host to network (server)
135  * NTOH - network (server) to client host
136  */
137 
138 #if MACHINE_WORD_FORMAT == DGL_BIG_ENDIAN
139 #define DGL_HTON_SHORT(t,f) t = f
140 #define DGL_NTOH_SHORT DGL_HTON_SHORT
141 #define DGL_HTON_INT32(t,f) t = f
142 #define DGL_NTOH_INT32 DGL_HTON_INT32
143 #endif /* MACHINE_WORD_FORMAT */
144 
145 /*
146  * DGL_BIG_IEEE: no conversion necessary (FLOAT)
147  */
148 
149 #if MACHINE_FLOAT_FORMAT == DGL_BIG_IEEE
150 #define DGL_HTON_FLOAT(t,f) t = f
151 #define DGL_NTOH_FLOAT DGL_HTON_FLOAT
152 #define DGL_HTON_DOUBLE(t,f) t = f
153 #define DGL_NTOH_DOUBLE DGL_HTON_DOUBLE
154 #endif
155 
156 /*
157  * DGL_LITTLE_ENDIAN: conversion necessary (INTEGER)
158  * NOTE: non-floating point conversions are the same for both
159  * directions and thus one macro suffices
160  */
161 
162 #if MACHINE_WORD_FORMAT == DGL_LITTLE_ENDIAN
163 
164 /* like DGL_HTON_INT32, but more efficient if f is a constant */
165 #define DGL_HTON_SHORT(t,f) \
166  { \
167  short _from = f,_to; \
168  ((char *)&_to)[0] = ((char *)&_from)[1]; \
169  ((char *)&_to)[1] = ((char *)&_from)[0]; \
170  t = _to; \
171  }
172 #define DGL_NTOH_SHORT DGL_HTON_SHORT
173 #define DGL_HTON_INT32(t,f) \
174  { \
175  int32_t _from = f,_to; \
176  ((char *)&_to)[0] = ((char *)&_from)[3]; \
177  ((char *)&_to)[1] = ((char *)&_from)[2]; \
178  ((char *)&_to)[2] = ((char *)&_from)[1]; \
179  ((char *)&_to)[3] = ((char *)&_from)[0]; \
180  t = _to; \
181  }
182 #define DGL_NTOH_INT32 DGL_HTON_INT32
183 
184 #endif /* LITTLE_ENDIAN */
185 
186 
187 /*
188  * DGL_NON_IEEE: conversion necessary (FLOAT)
189  * conversion is done within procedure calls for simplicity
190  */
191 
192 #if MACHINE_FLOAT_FORMAT == DGL_NON_IEEE
193 #if __i386__ || __x86_64__ || __ia64__ || __arm__ || __arm64__ || __arch64__
194 void mem_hton_float(float *t, float *f);
195 void mem_ntoh_float(float *t, float *f);
196 void mem_hton_double(double *t, double *f);
197 void mem_ntoh_double(double *t, double *f);
198 #endif /* __i386__ || __x86_64__ || __ia64__ */
199 #define DGL_HTON_FLOAT(t,f) mem_hton_float(&t,&f)
200 #define DGL_NTOH_FLOAT(t,f) mem_ntoh_float(&t,&f)
201 #define DGL_HTON_DOUBLE(t,f) mem_hton_double(&t,&f)
202 #define DGL_NTOH_DOUBLE(t,f) mem_ntoh_double(&t,&f)
203 #endif
204 
205 
206 /*
207  * get/set a data item located at address p regardless what it really is
208  */
209 
210 #define INT32(p) (*(int32_t *)(p))
211 #define FLOAT(p) (*(float *)(p))
212 #define DOUBLE(p) (*(double *)(p))
213 #define SHORT(p) (*(short *)(p))
214 
215 #endif /* __MACHINE_H__ */
void mem_hton_double(double *t, double *f)
void mem_ntoh_double(double *t, double *f)
void mem_ntoh_float(float *t, float *f)
void mem_hton_float(float *t, float *f)