1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright by The HDF Group. *
3 * Copyright by the Board of Trustees of the University of Illinois. *
4 * All rights reserved. *
5 * *
6 * This file is part of HDF. The full HDF copyright notice, including *
7 * terms governing use, modification, and redistribution, is contained in *
8 * the COPYING file, which can be found at the root of the source code *
9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14 /* $Id$ */
15
16 /*------------------------------------------------------------------
17 File: dfknat.c
18
19 Purpose:
20 Routines to support "native mode" conversion to and from HDF format
21
22 Invokes:
23
24 PRIVATE conversion functions:
25 DFKnb1b - Native mode for 8 bit integers
26 DFKnb2b - Native mode for 16 bit integers
27 DFKnb4b - Native mode for 32 bit integers and floats
28 DFKnb8b - Native mode for 64 bit floats
29
30 Remarks:
31 These files used to be in dfconv.c, but it got a little too huge,
32 so they were broken into a separate file.
33
34 *------------------------------------------------------------------*/
35
36 /*****************************************************************************/
37 /* */
38 /* All the routines in this file marked as PRIVATE have been marked so */
39 /* for a reason. *ANY* of these routines may or may nor be supported in */
40 /* the next version of HDF (4.00). Furthurmore, the names, paramters, or */
41 /* functionality is *NOT* guaranteed to remain the same. */
42 /* The *ONLY* guarantee possible is that DFKnumin(), and DFKnumout() */
43 /* will not change. They are *NOT* guaranteed to be implemented in the */
44 /* next version of HDF as function pointers. They are guaranteed to take */
45 /* the same arguments and produce the same results. */
46 /* If your programs call any routines in this file except for */
47 /* DFKnumin(), DFKnumout, and/or DFKsetntype(), your code may not work */
48 /* with future versions of HDF and your code will *NOT* be portable. */
49 /* */
50 /*****************************************************************************/
51
52 #include "hdf.h"
53 #include "hconv.h"
54
55 /*****************************************************************************/
56 /* NATIVE MODE NUMBER "CONVERSION" ROUTINES */
57 /*****************************************************************************/
58
59 /************************************************************/
60 /* DFKnb1b() */
61 /* Native mode for 1 byte data items */
62 /************************************************************/
63 int
DFKnb1b(VOIDP s,VOIDP d,uint32 num_elm,uint32 source_stride,uint32 dest_stride)64 DFKnb1b(VOIDP s, VOIDP d, uint32 num_elm, uint32 source_stride,
65 uint32 dest_stride)
66 {
67 int fast_processing = 0;
68 int in_place = 0;
69 uint32 i;
70 uint8 *source = (uint8 *) s;
71 uint8 *dest = (uint8 *) d;
72 CONSTR(FUNC, "DFKnb1b");
73
74 HEclear();
75
76 if (num_elm == 0)
77 {
78 HERROR(DFE_BADCONV);
79 return FAIL;
80 }
81
82 /* Determine if faster array processing is appropriate */
83 if ((source_stride == 0 && dest_stride == 0) ||
84 (source_stride == 1 && dest_stride == 1))
85 fast_processing = 1;
86
87 /* Determine if the conversion should be inplace */
88 if (source == dest)
89 in_place = 1;
90
91 if (fast_processing)
92 {
93 if (!in_place)
94 {
95 HDmemcpy(dest, source, num_elm);
96 return 0;
97 }
98 else
99 return 0; /* Nothing to do */
100 }
101 else
102 {
103 *dest = *source;
104 for (i = 1; i < num_elm; i++)
105 {
106 dest += dest_stride;
107 source += source_stride;
108 *dest = *source;
109 }
110 }
111
112 return 0;
113 }
114
115 /************************************************************/
116 /* DFKnb2b() */
117 /* -->Native mode for 2 byte data items */
118 /************************************************************/
119 int
DFKnb2b(VOIDP s,VOIDP d,uint32 num_elm,uint32 source_stride,uint32 dest_stride)120 DFKnb2b(VOIDP s, VOIDP d, uint32 num_elm, uint32 source_stride,
121 uint32 dest_stride)
122 {
123 int fast_processing = 0; /* Default is not fast processing */
124 int in_place = 0; /* Inplace must be detected */
125 uint32 i;
126 uint8 buf[2]; /* Inplace processing buffer */
127 uint8 *source = (uint8 *) s;
128 uint8 *dest = (uint8 *) d;
129 CONSTR(FUNC, "DFKnb2b");
130
131 HEclear();
132
133 if (num_elm == 0)
134 {
135 HERROR(DFE_BADCONV);
136 return FAIL;
137 }
138
139 /* Determine if faster array processing is appropriate */
140 if ((source_stride == 0 && dest_stride == 0) ||
141 (source_stride == 2 && dest_stride == 2))
142 fast_processing = 1;
143
144 /* Determine if the conversion should be inplace */
145 if (source == dest)
146 in_place = 1;
147
148 if (fast_processing) {
149 if (!in_place)
150 {
151 HDmemcpy(dest, source, num_elm * 2);
152 return 0;
153 }
154 else
155 { /* Nothing to do */
156 return 0;
157 }
158 }
159
160 /* Generic stride processing */
161 if (!in_place)
162 for (i = 0; i < num_elm; i++)
163 {
164 dest[0] = source[0];
165 dest[1] = source[1];
166 dest += dest_stride;
167 source += source_stride;
168 }
169 else
170 for (i = 0; i < num_elm; i++)
171 {
172 buf[0] = source[0];
173 buf[1] = source[1];
174 dest[0] = buf[0];
175 dest[1] = buf[1];
176 dest += dest_stride;
177 source += source_stride;
178 }
179
180 return 0;
181 }
182
183 /************************************************************/
184 /* DFKnb4b() */
185 /* -->Native mode for 4 byte items */
186 /************************************************************/
187 int
DFKnb4b(VOIDP s,VOIDP d,uint32 num_elm,uint32 source_stride,uint32 dest_stride)188 DFKnb4b(VOIDP s, VOIDP d, uint32 num_elm,
189 uint32 source_stride, uint32 dest_stride)
190 {
191 int fast_processing = 0; /* Default is not fast processing */
192 int in_place = 0; /* Inplace must be detected */
193 uint32 i;
194 uint8 buf[4]; /* Inplace processing buffer */
195 uint8 *source = (uint8 *) s;
196 uint8 *dest = (uint8 *) d;
197 CONSTR(FUNC, "DFKnb4b");
198
199 HEclear();
200
201 if (num_elm == 0)
202 {
203 HERROR(DFE_BADCONV);
204 return FAIL;
205 }
206
207 /* Determine if faster array processing is appropriate */
208 if ((source_stride == 0 && dest_stride == 0) ||
209 (source_stride == 4 && dest_stride == 4))
210 fast_processing = 1;
211
212 /* Determine if the conversion should be inplace */
213 if (source == dest)
214 in_place = 1;
215
216 if (fast_processing) {
217 if (!in_place)
218 {
219 HDmemcpy(dest, source, num_elm * 4);
220 return 0;
221 }
222 else
223 { /* Nothing to do */
224 return 0;
225 }
226 }
227
228 /* Generic stride processing */
229 if (!in_place)
230 for (i = 0; i < num_elm; i++)
231 {
232 dest[0] = source[0];
233 dest[1] = source[1];
234 dest[2] = source[2];
235 dest[3] = source[3];
236 dest += dest_stride;
237 source += source_stride;
238 }
239 else
240 for (i = 0; i < num_elm; i++)
241 {
242 buf[0] = source[0];
243 buf[1] = source[1];
244 buf[2] = source[2];
245 buf[3] = source[3];
246 dest[0] = buf[0];
247 dest[1] = buf[1];
248 dest[2] = buf[2];
249 dest[3] = buf[3];
250 dest += dest_stride;
251 source += source_stride;
252 }
253
254 return 0;
255 }
256
257 /************************************************************/
258 /* DFKnb8b() */
259 /* -->Native mode for 8 byte items */
260 /************************************************************/
261 int
DFKnb8b(VOIDP s,VOIDP d,uint32 num_elm,uint32 source_stride,uint32 dest_stride)262 DFKnb8b(VOIDP s, VOIDP d, uint32 num_elm,
263 uint32 source_stride, uint32 dest_stride)
264 {
265 int fast_processing = 0; /* Default is not fast processing */
266 int in_place = 0; /* Inplace must be detected */
267 uint32 i;
268 uint8 buf[8]; /* Inplace processing buffer */
269 uint8 *source = (uint8 *) s;
270 uint8 *dest = (uint8 *) d;
271
272 CONSTR(FUNC, "DFKnb8b");
273
274 HEclear();
275
276 if (num_elm == 0)
277 {
278 HERROR(DFE_BADCONV);
279 return FAIL;
280 }
281
282 /* Determine if faster array processing is appropriate */
283 if ((source_stride == 0 && dest_stride == 0) ||
284 (source_stride == 8 && dest_stride == 8))
285 fast_processing = 1;
286
287 /* Determine if the conversion should be inplace */
288 if (source == dest)
289 in_place = 1;
290
291 if (fast_processing) {
292 if (!in_place)
293 {
294 HDmemcpy(dest, source, num_elm * 8);
295 return 0;
296 }
297 else
298 {
299 return 0; /* No work to do ! */
300 }
301 }
302
303 /* Generic stride processing */
304 if (!in_place)
305 for (i = 0; i < num_elm; i++)
306 {
307 HDmemcpy(dest, source, 8);
308 dest += dest_stride;
309 source += source_stride;
310 }
311 else
312 for (i = 0; i < num_elm; i++)
313 {
314 HDmemcpy(buf, source, 8);
315 HDmemcpy(dest, buf, 8);
316 dest += dest_stride;
317 source += source_stride;
318 }
319
320 return 0;
321 }
322