~ubuntu-branches/ubuntu/trusty/silo-llnl/trusty

« back to all changes in this revision

Viewing changes to src/pdb/pdconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2011-01-02 00:03:01 UTC
  • Revision ID: james.westby@ubuntu.com-20110102000301-9s2hfsjrkguz6h4r
Tags: upstream-4.8
ImportĀ upstreamĀ versionĀ 4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
 
3
LLNL-CODE-425250.
 
4
All rights reserved.
 
5
 
 
6
This file is part of Silo. For details, see silo.llnl.gov.
 
7
 
 
8
Redistribution and use in source and binary forms, with or without
 
9
modification, are permitted provided that the following conditions
 
10
are met:
 
11
 
 
12
   * Redistributions of source code must retain the above copyright
 
13
     notice, this list of conditions and the disclaimer below.
 
14
   * Redistributions in binary form must reproduce the above copyright
 
15
     notice, this list of conditions and the disclaimer (as noted
 
16
     below) in the documentation and/or other materials provided with
 
17
     the distribution.
 
18
   * Neither the name of the LLNS/LLNL nor the names of its
 
19
     contributors may be used to endorse or promote products derived
 
20
     from this software without specific prior written permission.
 
21
 
 
22
THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
 
23
"AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
 
24
LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
25
A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
 
26
LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
 
27
CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
28
EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
 
29
PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
 
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
31
LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
 
32
NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
 
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
34
 
 
35
This work was produced at Lawrence Livermore National Laboratory under
 
36
Contract No.  DE-AC52-07NA27344 with the DOE.
 
37
 
 
38
Neither the  United States Government nor  Lawrence Livermore National
 
39
Security, LLC nor any of  their employees, makes any warranty, express
 
40
or  implied,  or  assumes  any  liability or  responsibility  for  the
 
41
accuracy, completeness,  or usefulness of  any information, apparatus,
 
42
product, or  process disclosed, or  represents that its use  would not
 
43
infringe privately-owned rights.
 
44
 
 
45
Any reference herein to  any specific commercial products, process, or
 
46
services by trade name,  trademark, manufacturer or otherwise does not
 
47
necessarily  constitute or imply  its endorsement,  recommendation, or
 
48
favoring  by  the  United  States  Government  or  Lawrence  Livermore
 
49
National Security,  LLC. The views  and opinions of  authors expressed
 
50
herein do not necessarily state  or reflect those of the United States
 
51
Government or Lawrence Livermore National Security, LLC, and shall not
 
52
be used for advertising or product endorsement purposes.
 
53
*/
 
54
/*
 
55
 * PDCONV.C - routines to do conversions between data formats
 
56
 *
 
57
 * Source Version: 9.0
 
58
 * Software Release #92-0043
 
59
 *
 
60
 */
 
61
#include <limits.h>
 
62
#include "pdb.h"
 
63
#include "pdform.h"
 
64
 
 
65
#define ONES_COMP_NEG(n, nb, incr)                                           \
 
66
    {if (nb == 8*sizeof(long))                                               \
 
67
        n = ~n + incr;                                                       \
 
68
     else                                                                    \
 
69
        {long msk;                                                           \
 
70
         msk = (1L << nb) - 1L;                                              \
 
71
         n   = (~n + incr) & msk;};}
 
72
 
 
73
/*
 
74
 * Forward declarations...
 
75
 */
 
76
static void             _PD_btrvout (char*,long,long);
 
77
static int              _PD_get_bit (char*,int,int,int*);
 
78
static void             _PD_insert_field (long,int,char*,int,int,int);
 
79
static void             _PD_ncopy (char**,char**,long,long);
 
80
static void             _PD_ones_complement (char*,long,int);
 
81
static void             _PD_reorder (char*,long,int,int*);
 
82
static void             _PD_set_bit (char*,int);
 
83
static void             _PD_sign_extend (char*,long,int,int,int*);
 
84
 
 
85
/*--------------------------------------------------------------------------*/
 
86
/*                             DATA_STANDARDS                               */
 
87
/*--------------------------------------------------------------------------*/
 
88
 
 
89
/*
 
90
 * Floating Point Format Descriptor
 
91
 *
 
92
 * -   format[0] = # of bits per number
 
93
 * -   format[1] = # of bits in exponent
 
94
 * -   format[2] = # of bits in mantissa
 
95
 * -   format[3] = start bit of sign
 
96
 * -   format[4] = start bit of exponent
 
97
 * -   format[5] = start bit of mantissa
 
98
 * -   format[6] = high order mantissa bit
 
99
 * -   format[7] = bias of exponent
 
100
 *
 
101
 */
 
102
 
 
103
int     lite_FORMAT_FIELDS = 8 ;
 
104
 
 
105
int     lite_def_float_order[]    = {1, 2, 3, 4};
 
106
int     lite_def_double_order[]   = {1, 2, 3, 4, 5, 6, 7, 8};
 
107
int     lite_ieee_float_order[]   = {1, 2, 3, 4};
 
108
int     lite_ieeea_double_order[] = {1, 2, 3, 4, 5, 6, 7, 8};
 
109
int     lite_ieeeb_double_order[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
 
110
int     lite_intel_float_order[]  = {4, 3, 2, 1};
 
111
int     lite_intel_double_order[] = {8, 7, 6, 5, 4, 3, 2, 1};
 
112
int     lite_vax_float_order[]    = {2, 1, 4, 3};
 
113
int     lite_vax_double_order[]   = {2, 1, 4, 3, 6, 5, 8, 7};
 
114
int     lite_cray_float_order[]   = {1, 2, 3, 4, 5, 6, 7, 8};
 
115
 
 
116
long    lite_def_float[]    = {32L,  8L, 23L, 0L, 1L,  9L, 0L,   0x7FL};
 
117
long    lite_def_double[]   = {64L, 11L, 52L, 0L, 1L, 12L, 0L,  0x3FFL};
 
118
long    lite_ieee_float[]   = {32L,  8L, 23L, 0L, 1L,  9L, 0L,   0x7FL};
 
119
long    lite_ieeea_double[] = {64L, 11L, 52L, 0L, 1L, 12L, 0L,  0x3FFL};
 
120
long    lite_ieeeb_double[] = {96L, 15L, 64L, 0L, 1L, 32L, 1L, 0x3FFEL};
 
121
long    lite_intel_float[]  = {32L,  8L, 23L, 0L, 1L,  9L, 0L,   0x7FL};
 
122
long    lite_intel_double[] = {64L, 11L, 52L, 0L, 1L, 12L, 0L,  0x3FFL};
 
123
long    lite_cray_float[]   = {64L, 15L, 48L, 0L, 1L, 16L, 1L, 0x4000L};
 
124
 
 
125
#ifdef GFLOAT
 
126
long    lite_vax_float[]    = {32L,  8L, 23L, 0L, 1L,  9L, 0L,   0x81L};
 
127
long    lite_vax_double[]   = {64L, 11L, 52L, 0L, 1L, 12L, 0L,  0x401L};
 
128
#else
 
129
long    lite_vax_float[]    = {32L,  8L, 23L, 0L, 1L,  9L, 0L,   0x81L};
 
130
long    lite_vax_double[]   = {64L,  8L, 55L, 0L, 1L,  9L, 0L,   0x81L};
 
131
#endif
 
132
 
 
133
 
 
134
/*
 
135
 * Data standard for the different architectures
 
136
 *
 
137
 *   struct s_data_standard
 
138
 *      {int ptr_bytes;
 
139
 *       int short_bytes;
 
140
 *       int short_order;
 
141
 *       int int_bytes;
 
142
 *       int int_order;
 
143
 *       int long_bytes;
 
144
 *       int long_order;
 
145
 *       int float_bytes;
 
146
 *       long *float_format;
 
147
 *       int *float_order;
 
148
 *       int double_bytes;
 
149
 *       long *double_format;
 
150
 *       int *double_order;};
 
151
 *
 
152
 *   typedef struct s_data_standard data_standard;
 
153
 *
 
154
 *   Mark C. Miller, Fri Nov 13 15:33:42 PST 2009
 
155
 *   Added support for long long datatype.
 
156
 */
 
157
 
 
158
data_standard
 
159
 lite_DEF_STD    = {4,                                    /* size of pointer */
 
160
                    2, NORMAL_ORDER,              /* size and order of short */
 
161
                    4, NORMAL_ORDER,                /* size and order of int */
 
162
                    4, NORMAL_ORDER,               /* size and order of long */
 
163
                    4, NORMAL_ORDER,          /* size and order of long long */
 
164
                    4, lite_def_float,                   /* float definition */
 
165
                    lite_def_float_order,
 
166
                    8, lite_def_double,                 /* double definition */
 
167
                    lite_def_double_order},
 
168
 lite_IEEEA_STD  = {4,                                    /* size of pointer */
 
169
                    2, NORMAL_ORDER,              /* size and order of short */
 
170
                    4, NORMAL_ORDER,                /* size and order of int */
 
171
                    4, NORMAL_ORDER,               /* size and order of long */
 
172
                    8, NORMAL_ORDER,          /* size and order of long long */
 
173
                    4, lite_ieee_float,                  /* float definition */
 
174
                    lite_ieee_float_order,
 
175
                    8, lite_ieeea_double,               /* double definition */
 
176
                    lite_ieeea_double_order},
 
177
 lite_IEEEB_STD  = {4,                                    /* size of pointer */
 
178
                    2, NORMAL_ORDER,              /* size and order of short */
 
179
                    2, NORMAL_ORDER,                /* size and order of int */
 
180
                    4, NORMAL_ORDER,               /* size and order of long */
 
181
                    4, NORMAL_ORDER,          /* size and order of long long */
 
182
                    4, lite_ieee_float,                  /* float definition */
 
183
                    lite_ieee_float_order,
 
184
                    12, lite_ieeeb_double,              /* double definition */
 
185
                    lite_ieeeb_double_order},
 
186
 lite_INTELA_STD = {4,                                    /* size of pointer */
 
187
                    2, REVERSE_ORDER,             /* size and order of short */
 
188
                    2, REVERSE_ORDER,               /* size and order of int */
 
189
                    4, REVERSE_ORDER,              /* size and order of long */
 
190
                    4, REVERSE_ORDER,         /* size and order of long long */
 
191
                    4, lite_intel_float,                 /* float definition */
 
192
                    lite_intel_float_order,
 
193
                    8, lite_intel_double,               /* double definition */
 
194
                    lite_intel_double_order},
 
195
 lite_VAX_STD    = {4,                                    /* size of pointer */
 
196
                    2, REVERSE_ORDER,             /* size and order of short */
 
197
                    4, REVERSE_ORDER,               /* size and order of int */
 
198
                    4, REVERSE_ORDER,              /* size and order of long */
 
199
                    4, REVERSE_ORDER,         /* size and order of long long */
 
200
                    4, lite_vax_float,                   /* float definition */
 
201
                    lite_vax_float_order,
 
202
                    8, lite_vax_double,                 /* double definition */
 
203
                    lite_vax_double_order},
 
204
 lite_CRAY_STD   = {8,                                    /* size of pointer */
 
205
                    8, NORMAL_ORDER,              /* size and order of short */
 
206
                    8, NORMAL_ORDER,                /* size and order of int */
 
207
                    8, NORMAL_ORDER,               /* size and order of long */
 
208
                    8, NORMAL_ORDER,          /* size and order of long long */
 
209
                    8, lite_cray_float,                  /* float definition */
 
210
                    lite_cray_float_order,
 
211
                    8, lite_cray_float,                 /* double definition */
 
212
                    lite_cray_float_order};
 
213
 
 
214
 
 
215
/*--------------------------------------------------------------------------*/
 
216
/*                            DATA_ALIGNMENTS                               */
 
217
/*--------------------------------------------------------------------------*/
 
218
 
 
219
/*
 
220
 * Word Alignment for the different architectures
 
221
 *
 
222
 *
 
223
 *   struct s_data_alignment
 
224
 *      {int char_alignment;
 
225
 *       int ptr_alignment;
 
226
 *       int short_alignment;
 
227
 *       int int_alignment;
 
228
 *       int long_alignment;
 
229
 *       int float_alignment;
 
230
 *       int double_alignment;
 
231
 *       int struct_alignment;};
 
232
 *
 
233
 *   typedef struct s_data_alignment data_alignment;
 
234
 *
 
235
 *   Mark C. Miller, Fri Nov 13 15:33:42 PST 2009
 
236
 *   Added support for long long datatype.
 
237
 */
 
238
 
 
239
/*
 
240
                                     long long
 
241
                                         |          
 
242
                                         |
 
243
                                         V
 
244
                          0  1  2  3  4  5  6  7  8
 
245
*/
 
246
data_alignment
 
247
 lite_RS6000_ALIGNMENT = {1, 4, 2, 4, 4, 8, 4, 4, 0},
 
248
 lite_SPARC_ALIGNMENT  = {1, 4, 2, 4, 4, 4, 4, 8, 0},
 
249
 lite_MIPS_ALIGNMENT   = {1, 4, 2, 4, 4, 8, 4, 8, 0},
 
250
 lite_M68000_ALIGNMENT = {1, 2, 2, 2, 2, 2, 2, 2, 0},
 
251
 lite_INTELA_ALIGNMENT = {1, 2, 2, 2, 2, 2, 2, 2, 0},
 
252
 lite_UNICOS_ALIGNMENT = {4, 8, 8, 8, 8, 8, 8, 8, 8},
 
253
 lite_DEF_ALIGNMENT    = {1, 4, 4, 4, 4, 4, 4, 4, 0};
 
254
 
 
255
 
 
256
/*--------------------------------------------------------------------------*/
 
257
/*--------------------------------------------------------------------------*/
 
258
 
 
259
 
 
260
/*-------------------------------------------------------------------------
 
261
 * Function:    lite_PD_convert
 
262
 *
 
263
 * Purpose:     Convert from one machine format to another NITEMS of type
 
264
 *              TYPE from IN and put them in OUT.  ISTD and OSTD are the
 
265
 *              data format standards of IN and OUT respectively.
 
266
 *
 
267
 * Return:      Success:
 
268
 *
 
269
 *              Failure:
 
270
 *
 
271
 * Programmer:  Adapted from PACT PDB
 
272
 *              Mar  6, 1996 11:34 AM EST
 
273
 *
 
274
 * Modifications:
 
275
 *
 
276
 *-------------------------------------------------------------------------
 
277
 */
 
278
int
 
279
lite_PD_convert (char **out, char **in, char *intype, char *outtype,
 
280
                 long nitems, data_standard *istd, data_standard *ostd,
 
281
                 data_standard *hstd, long *pin_offs, long *pout_offs,
 
282
                 HASHTAB *in_chart, HASHTAB *out_chart, int boffs, int error) {
 
283
 
 
284
   int ret, tmp;
 
285
   long i, mitems, in_incr, out_incr;
 
286
   char *mtype;
 
287
   defstr *idp, *odp, *midp, *modp;
 
288
   memdes *desc;
 
289
 
 
290
   if (_lite_PD_indirection(outtype)) {
 
291
      in_incr     = _lite_PD_align(*pin_offs, intype, in_chart, &tmp);
 
292
      out_incr    = _lite_PD_align(*pout_offs, outtype, out_chart, &tmp);
 
293
      *pin_offs  += in_incr;
 
294
      *pout_offs += out_incr;
 
295
      *in        += istd->ptr_bytes + in_incr;
 
296
      *out       += ostd->ptr_bytes + out_incr;
 
297
      return(TRUE);
 
298
   }
 
299
 
 
300
   idp = PD_inquire_table_type(in_chart, intype);
 
301
   if (idp == NULL)
 
302
      lite_PD_error("BAD TYPE IN_CHART - PD_CONVERT", error);
 
303
 
 
304
   odp = PD_inquire_table_type(out_chart, outtype);
 
305
   if (odp == NULL)
 
306
      lite_PD_error("BAD TYPE OUT_CHART - PD_CONVERT", error);
 
307
 
 
308
   /*
 
309
    * If members is non-NULL then it is a derived type.
 
310
    */
 
311
   if (odp->members != NULL) {
 
312
      for (i = 0L; i < nitems; i++) {
 
313
         in_incr     = _lite_PD_align(*pin_offs, intype, in_chart, &tmp);
 
314
         out_incr    = _lite_PD_align(*pout_offs, outtype, out_chart, &tmp);
 
315
         *pin_offs  += in_incr;
 
316
         *pout_offs += out_incr;
 
317
         *in        += in_incr;
 
318
         *out       += out_incr;
 
319
 
 
320
         for (desc = odp->members; desc != NULL; desc = desc->next) {
 
321
            mitems   = desc->number;
 
322
            mtype    = desc->type;
 
323
            in_incr  = _lite_PD_align(*pin_offs, mtype, in_chart, &tmp);
 
324
            out_incr = _lite_PD_align(*pout_offs, mtype, out_chart, &tmp);
 
325
 
 
326
            /*
 
327
             * Increment the offsets to the alignments.
 
328
             */
 
329
            *pin_offs  += in_incr;
 
330
            *pout_offs += out_incr;
 
331
            *in        += in_incr;
 
332
            *out       += out_incr;
 
333
 
 
334
            if (_lite_PD_indirection(mtype)) {
 
335
               /*
 
336
                * Don't even think about converting pointers - they will be
 
337
                * recomputed. However, don't throw away information about
 
338
                * whether or not pointer is null.
 
339
                */
 
340
               if (!_lite_PD_null_pointer(*in, istd->ptr_bytes)) **out = 1;
 
341
               *in        += istd->ptr_bytes;
 
342
               *out       += ostd->ptr_bytes;
 
343
               *pin_offs  += istd->ptr_bytes;
 
344
               *pout_offs += ostd->ptr_bytes;
 
345
               ret         = TRUE;
 
346
 
 
347
            } else if (_lite_PD_prim_typep(desc->base_type, in_chart, error)) {
 
348
               /*
 
349
                * Check for direct primitives.
 
350
                */
 
351
               modp = PD_inquire_table_type(out_chart, mtype);
 
352
               if (modp == NULL)
 
353
                  lite_PD_error("BAD OUT TYPE IN STRUCT - PD_CONVERT", error);
 
354
 
 
355
               midp = PD_inquire_table_type(in_chart, mtype);
 
356
               if (midp == NULL)
 
357
                  lite_PD_error("BAD IN TYPE IN STRUCT - PD_CONVERT", error);
 
358
 
 
359
               ret = _lite_PD_convert((char **) out, in, mitems, boffs,
 
360
                                      midp, modp, hstd, pin_offs, pout_offs);
 
361
 
 
362
            } else {
 
363
               /*
 
364
                * Recurse for direct derived types.
 
365
                */
 
366
               ret = lite_PD_convert(out, in, mtype, mtype, mitems,
 
367
                                     istd, ostd, hstd, pin_offs, pout_offs,
 
368
                                     in_chart, out_chart, boffs, error);
 
369
            }
 
370
 
 
371
            if (!ret)
 
372
               lite_PD_error("STRUCT CONVERSION FAILED - PD_CONVERT",
 
373
                             error);
 
374
         }
 
375
      }
 
376
 
 
377
   } else {
 
378
      /*
 
379
       * if members is NULL then it is a primitive type.
 
380
       */
 
381
      ret = _lite_PD_convert((char **) out, in, nitems, boffs, idp, odp,
 
382
                             hstd, pin_offs, pout_offs);
 
383
      if (!ret)
 
384
         lite_PD_error("PRIMITIVE CONVERSION FAILED - PD_CONVERT", error);
 
385
   }
 
386
 
 
387
   return(ret);
 
388
}
 
389
 
 
390
 
 
391
/*-------------------------------------------------------------------------
 
392
 * Function:    _lite_PD_null_pointer
 
393
 *
 
394
 * Purpose:     Does input contain only null bytes?
 
395
 *
 
396
 * Return:      Success:        TRUE
 
397
 *
 
398
 *              Failure:        FALSE
 
399
 *
 
400
 * Programmer:  Adapted from PACT PDB
 
401
 *              Mar  6, 1996 12:02 PM EST
 
402
 *
 
403
 * Modifications:
 
404
 *
 
405
 *-------------------------------------------------------------------------
 
406
 */
 
407
int
 
408
_lite_PD_null_pointer (char *in, int bytes) {
 
409
 
 
410
   int i;
 
411
 
 
412
   for (i = 0; i < bytes; i++) {
 
413
      if (*in++) return(FALSE);
 
414
   }
 
415
 
 
416
   return(TRUE);
 
417
}
 
418
 
 
419
 
 
420
/*-------------------------------------------------------------------------
 
421
 * Function:    _lite_PD_prim_typep
 
422
 *
 
423
 * Purpose:     Is the given struct member a primitive data type?
 
424
 *
 
425
 * Return:      Success:        TRUE
 
426
 *
 
427
 *              Failure:        FALSE
 
428
 *
 
429
 * Programmer:  Adapted from PACT PDB
 
430
 *              Mar  6, 1996 12:03 PM EST
 
431
 *
 
432
 * Modifications:
 
433
 *
 
434
 *-------------------------------------------------------------------------
 
435
 */
 
436
int
 
437
_lite_PD_prim_typep (char *memb, HASHTAB *chrt, int error) {
 
438
 
 
439
   defstr *pd;
 
440
 
 
441
   pd = PD_inquire_table_type(chrt, memb);
 
442
   if (pd == NULL)
 
443
      lite_PD_error("BAD TYPE FROM STRUCTURE CHART - _PD_PRIM_TYPEP", error);
 
444
 
 
445
   return((pd->members == NULL));
 
446
}
 
447
 
 
448
 
 
449
/*-------------------------------------------------------------------------
 
450
 * Function:    _lite_PD_convert
 
451
 *
 
452
 * Purpose:     Convert primitive types from one machine format to another.
 
453
 *              Guaranteed that there will be no indirects here. Convert
 
454
 *              NITEMS of type TYPE from IN and put them in OUT.  ISTD
 
455
 *              defines the data format of the data from IN.  PIN_OFFS and
 
456
 *              POUT_OFFS are pointers to external offset counters and
 
457
 *              they are incremented to account for data alignment.
 
458
 *
 
459
 * Return:      Success:        TRUE
 
460
 *
 
461
 *              Failure:        FALSE
 
462
 *
 
463
 * Programmer:  Adapted from PACT PDB
 
464
 *              Mar  6, 1996 11:58 AM EST
 
465
 *
 
466
 * Modifications:
 
467
 *
 
468
 *-------------------------------------------------------------------------
 
469
 */
 
470
int
 
471
_lite_PD_convert (char **out, char **in, long nitems, int boffs,
 
472
                  defstr *idp, defstr *odp, data_standard *hstd,
 
473
                  long *pin_offs, long *pout_offs) {
 
474
 
 
475
   int l_order, l_bytes, in_bits, onescmp, unsgned, ret;
 
476
   long in_bytes, out_bytes;
 
477
   int in_flag, out_flag, *in_order, *out_order;
 
478
   long *in_format, *out_format;
 
479
   char *in_type, *out_type;
 
480
 
 
481
   in_type    = idp->type;
 
482
   out_type   = odp->type;
 
483
   in_bits    = idp->size_bits;
 
484
   in_bytes   = idp->size;
 
485
   out_bytes  = odp->size;
 
486
   in_order   = idp->order;
 
487
   out_order  = odp->order;
 
488
   in_flag    = idp->order_flag;
 
489
   out_flag   = odp->order_flag;
 
490
   in_format  = idp->format;
 
491
   out_format = odp->format;
 
492
   onescmp    = idp->onescmp;
 
493
   unsgned    = idp->unsgned;
 
494
 
 
495
   if ((strchr(in_type, '*') != NULL) || (strchr(out_type, '*') != NULL))
 
496
      return(FALSE);
 
497
 
 
498
   l_order = hstd->long_order;
 
499
   l_bytes = hstd->long_bytes;
 
500
 
 
501
   ret = TRUE;
 
502
   if ((in_format != NULL) && (out_format != NULL)) {
 
503
      /*
 
504
       * Handle floating point conversions.
 
505
       */
 
506
      _lite_PD_fconvert(out, in, nitems, boffs, in_format, in_order,
 
507
                        out_format, out_order, l_order, l_bytes, onescmp);
 
508
 
 
509
      *pin_offs  += in_bytes*nitems;
 
510
      *pout_offs += out_bytes*nitems;
 
511
   } else if (in_bits > 0) {
 
512
      /*
 
513
       * Handle integral bitstreams.
 
514
       */
 
515
      int ityp;
 
516
 
 
517
      if (out_bytes == sizeof(char)) ityp = SC_CHAR_I;
 
518
      else if (out_bytes == sizeof(short)) ityp = SC_SHORT_I;
 
519
      else if (out_bytes == sizeof(int)) ityp = SC_INTEGER_I;
 
520
      else if (out_bytes == sizeof(long)) ityp = SC_LONG_I;
 
521
      else return(FALSE);
 
522
 
 
523
      /*
 
524
       * Unpack the bitstream into a bytestream.
 
525
       */
 
526
      ret = _lite_PD_unp_bits(*out, *in, ityp, in_bits,
 
527
                              0, nitems, nitems, boffs);
 
528
 
 
529
      if (strcmp(in_type, lite_SC_CHAR_S) == 0) {
 
530
         /*
 
531
          * Convert characters.
 
532
          */
 
533
         _lite_PD_convert_ascii(*out, *in, nitems, in_bits, 0);
 
534
 
 
535
      } else {
 
536
         /*
 
537
          * Convert integers.
 
538
          */
 
539
         if (!unsgned)
 
540
            _PD_sign_extend(*out, nitems, out_bytes,
 
541
                            in_bits, out_order);
 
542
 
 
543
         *in  += in_bytes*nitems;
 
544
         *out += out_bytes*nitems;
 
545
         *pin_offs  += in_bytes*nitems;
 
546
         *pout_offs += out_bytes*nitems;
 
547
      }
 
548
 
 
549
   } else if ((in_flag != -1) && (out_flag != -1)) {
 
550
      /*
 
551
       * Handle integer conversions.
 
552
       */
 
553
      _lite_PD_iconvert(out, in, nitems,
 
554
                        (int) in_bytes, in_flag,
 
555
                        (int) out_bytes, out_flag, onescmp);
 
556
 
 
557
      *pin_offs  += in_bytes*nitems;
 
558
      *pout_offs += out_bytes*nitems;
 
559
 
 
560
   } else {
 
561
      /*
 
562
       * Handle character or unconverted types.
 
563
       */
 
564
      _PD_ncopy(out, in, nitems, in_bytes);
 
565
 
 
566
      *pin_offs  += in_bytes*nitems;
 
567
      *pout_offs += out_bytes*nitems;
 
568
   }
 
569
 
 
570
   return(ret);
 
571
}
 
572
 
 
573
/*--------------------------------------------------------------------------*/
 
574
/*                           ASCII CONVERSION ROUTINES                      */
 
575
/*--------------------------------------------------------------------------*/
 
576
 
 
577
 
 
578
/*-------------------------------------------------------------------------
 
579
 * Function:    _lite_PD_convert_ascii
 
580
 *
 
581
 * Purpose:     Translate characters fromBCPI bit ASCII to 8-bit ASCII.
 
582
 *              This is only here to allow PDB to read old machine data
 
583
 *              which may not be 8-bit ASCII.
 
584
 *
 
585
 * Inputs:      IN:     Input buffer.
 
586
 *              NITEMS: Number of fields to translate.
 
587
 *              BCPI:   Bits per character on input.
 
588
 *              OFFS:   Zero-origin bit offset from IN to start of data.
 
589
 *
 
590
 * Output:      OUT:    Output buffer.
 
591
 *
 
592
 * Return:      void
 
593
 *
 
594
 * Programmer:  Adapted from PACT PDB
 
595
 *              Mar  6, 1996 12:05 PM EST
 
596
 *
 
597
 * Modifications:
 
598
 *      Sean Ahern, Wed Apr 12 10:44:26 PDT 2000
 
599
 *      Put in an ARGSUSED comment to prevent compiler warnings.  I don't want
 
600
 *      to remove the parameters, since I don't know how this is called.
 
601
 *-------------------------------------------------------------------------*/
 
602
/* ARGSUSED */
 
603
void
 
604
_lite_PD_convert_ascii (char *out, char *in, long nitems, int bpci, int offs) {
 
605
 
 
606
   long i;
 
607
 
 
608
   /*
 
609
    * Stretch input characters into 8 bits right-justified
 
610
    * with left bits 0.
 
611
    */
 
612
 
 
613
   /* translate chars by adding a blank (0x20) character
 
614
    * to give upper-case letters then or-ing the sum with a blank
 
615
    * to give lower-case letters
 
616
    */
 
617
   for (i = 0; i < nitems; i++) out[i] = (out[i] + ' ') | ' ';
 
618
}
 
619
 
 
620
/*--------------------------------------------------------------------------*/
 
621
/*                      INTEGER CONVERSION ROUTINES                         */
 
622
/*--------------------------------------------------------------------------*/
 
623
 
 
624
 
 
625
/*-------------------------------------------------------------------------
 
626
 * Function:    _lite_PD_iconvert
 
627
 *
 
628
 * Purpose:     Convert integers of nbi bytes to integers of nbo bytes.
 
629
 *              The number of bytes for each integer are give.
 
630
 *
 
631
 * Return:      void
 
632
 *
 
633
 * Programmer:  Adapted from PACT PDB
 
634
 *              Mar  6, 1996 12:14 PM EST
 
635
 *
 
636
 * Modifications:
 
637
 *
 
638
 *-------------------------------------------------------------------------
 
639
 */
 
640
void
 
641
_lite_PD_iconvert (char **out, char **in, long nitems, long nbi, int ordi,
 
642
                   long nbo, int ordo, int onescmp) {
 
643
 
 
644
   long i;
 
645
   int j;
 
646
   char *lout, *lin, *po, *pi;
 
647
 
 
648
   lin = *in;
 
649
   lout = *out;
 
650
 
 
651
   /*
 
652
    * Convert nitems integers.
 
653
    * test sign bit to properly convert negative integers
 
654
    */
 
655
   if (nbi < nbo) {
 
656
      if (ordi == REVERSE_ORDER) {
 
657
         for (j = nbi; j < nbo; j++) {
 
658
            po = lout + j - nbi;
 
659
            pi = lin + nbi - 1;
 
660
            for (i = 0L; i < nitems; i++) {
 
661
               *po = (*pi & 0x80) ? 0xff : 0;
 
662
               po += nbo;
 
663
               pi += nbi;
 
664
            }
 
665
         }
 
666
         for (j = nbi; j > 0; j--) {
 
667
            po = lout + nbo - j;
 
668
            pi = lin + j - 1;
 
669
            for (i = 0L; i < nitems; i++) {
 
670
               *po = *pi;
 
671
               po += nbo;
 
672
               pi += nbi;
 
673
            }
 
674
         }
 
675
      } else {
 
676
         for (j = nbi; j < nbo; j++) {
 
677
            po = lout + j - nbi;
 
678
            pi = lin;
 
679
            for (i = 0L; i < nitems; i++) {
 
680
               *po = (*pi & 0x80) ? 0xff : 0;
 
681
               po += nbo;
 
682
               pi += nbi;
 
683
            }
 
684
         }
 
685
         for (j = 0; j < nbi; j++) {
 
686
            po = lout + j + nbo - nbi;
 
687
            pi = lin + j;
 
688
            for (i = 0L; i < nitems; i++) {
 
689
               *po = *pi;
 
690
               po += nbo;
 
691
               pi += nbi;
 
692
            }
 
693
         }
 
694
      }
 
695
 
 
696
   } else if (nbi >= nbo) {
 
697
      if (ordi == REVERSE_ORDER) {
 
698
         for (j = nbo; j > 0; j--) {
 
699
            po = lout + nbo - j;
 
700
            pi = lin + j - 1;
 
701
            for (i = 0L; i < nitems; i++) {
 
702
               *po = *pi;
 
703
               po += nbo;
 
704
               pi += nbi;
 
705
            }
 
706
         }
 
707
      } else {
 
708
         for (j = nbi - nbo; j < nbi; j++) {
 
709
            po = lout + j - nbi + nbo;
 
710
            pi = lin + j;
 
711
            for (i = 0L; i < nitems; i++) {
 
712
               *po = *pi;
 
713
               po += nbo;
 
714
               pi += nbi;
 
715
            }
 
716
         }
 
717
      }
 
718
   }
 
719
 
 
720
   /*
 
721
    * If the input used ones complement arithmetic
 
722
    * convert to twos complement.
 
723
    */
 
724
   if (onescmp) _PD_ones_complement(*out, nitems, nbo);
 
725
 
 
726
   if (ordo == REVERSE_ORDER) _PD_btrvout(*out, nbo, nitems);
 
727
 
 
728
   *in  += nitems*nbi;
 
729
   *out += nitems*nbo;
 
730
}
 
731
 
 
732
/*--------------------------------------------------------------------------*/
 
733
/*                       FLOAT CONVERSION ROUTINES                          */
 
734
/*--------------------------------------------------------------------------*/
 
735
 
 
736
/* Parametrized Data Conversion Method
 
737
 *
 
738
 * Floating point formats are characterized by a set of parameters which
 
739
 * describe the fundamental elements of a floating point number. These are
 
740
 *
 
741
 *  Sign     - always assumed to be a single bit
 
742
 *           - requires bit offset
 
743
 *  Exponent - assumed to be a biased integer smaller than 32 bits
 
744
 *           - (this allows the conversion to use a long on all known
 
745
 *           - platforms - an exponent greater than 32 bits long would
 
746
 *           - allow much larger numbers than should be needed for
 
747
 *           - scientific computations)
 
748
 *           - requires a bit offset, a bit length, and a bias
 
749
 * Mantissa  - assumed to be a bitstream of arbitrary length
 
750
 *           - requires a bit offset and a bit length
 
751
 * HMB       - in all floating point representations the mantissa is
 
752
 *           - normalized so that the most significant bit is one.
 
753
 *           - in some formats the one is explicitly included in the
 
754
 *           - representation and in others it is only implicit
 
755
 *           - this gives some formats an extra bit of precision.
 
756
 *           - requires a flag which is TRUE if the HMB is explicit
 
757
 *
 
758
 * Two other factors involved are: the byte order which could be
 
759
 * mixed with the bit layout of the numbers but isn't in actual practice
 
760
 * on current machines; and whether one's complement or two's complement
 
761
 * arithmetic is used. Modern machines all use two's complement arithmetic
 
762
 * and the model used here and now is that data from one's complement
 
763
 * machines is to be read only.  This restriction is relatively easy
 
764
 * to relax, but there is no evidence that it should be.
 
765
 *
 
766
 * An issue which is not a problem in the current implementation is that
 
767
 * old machines with byte sizes other than 8 bits can be accomodated
 
768
 * because the conversions treat the input and output as bitstreams
 
769
 * instead of bytestreams.
 
770
 *
 
771
 * The conversion process is summarized as follows:
 
772
 *   1) Extract the sign bit and exponent field from the input number
 
773
 *   2) Subtract the bias of the source format and add the bias
 
774
 *      of the target format
 
775
 *   3) Check for overflow in the exponent
 
776
 *   4) Insert the new exponent and the sign bit in the target stream
 
777
 *   5) Copy the mantissa bits from the source to the target
 
778
 *      compensating for differences in the HMB between the two
 
779
 *      formats
 
780
 *   6) Take care of any known anomalies - e.g. CRAY format is
 
781
 *      inconsistent in that the HMB is explicitly on for all numbers
 
782
 *      with the exception of 0.0
 
783
 *   7) Reorder the bytes of the target stream appropriately
 
784
 *
 
785
 * The floating point formats for a variety of platforms are supplied by
 
786
 * PDBLib and are defined at the top of this file
 
787
 *
 
788
 */
 
789
 
 
790
 
 
791
/*-------------------------------------------------------------------------
 
792
 * Function:    _lite_PD_fconvert
 
793
 *
 
794
 * Purpose:     General floating point conversion routine.  Convert from
 
795
 *              floating point format specified by infor to format
 
796
 *              specified by outfor.
 
797
 *
 
798
 *              floating point format specification:
 
799
 *
 
800
 *                format[0] = # of bits per number
 
801
 *                format[1] = # of bits in exponent
 
802
 *                format[2] = # of bits in mantissa
 
803
 *                format[3] = start bit of sign
 
804
 *                format[4] = start bit of exponent
 
805
 *                format[5] = start bit of mantissa
 
806
 *                format[6] = high order mantissa bit (CRAY needs this)
 
807
 *                format[7] = bias of exponent
 
808
 *
 
809
 * Return:      void
 
810
 *
 
811
 * Programmer:  Adapted from PACT PDB
 
812
 *              Mar  6, 1996 12:08 PM EST
 
813
 *
 
814
 * Modifications:
 
815
 *      Sean Ahern, Fri Mar  2 09:39:53 PST 2001
 
816
 *      Added a logic path suggested by Dan Schikore for when the formats
 
817
 *      are the same.
 
818
 *
 
819
 *      Sean Ahern, Fri Mar  2 09:40:15 PST 2001
 
820
 *      Reformatted some of the code.
 
821
 *
 
822
 *-------------------------------------------------------------------------*/
 
823
void
 
824
_lite_PD_fconvert (char **out, char **in, long nitems, int boffs, long *infor,
 
825
                   int *inord, long *outfor, int *outord, int l_order,
 
826
                   int l_bytes, int onescmp) {
 
827
 
 
828
   long i, expn, expn_max, hexpn, mant, DeltaBias, hmbo, hmbi;
 
829
   int nbits, inbytes, outbytes, sign;
 
830
   int indxin, indxout, inrem, outrem, dindx;
 
831
   int bi_sign, bo_sign, bi_exp, bo_exp, bi_mant, bo_mant;
 
832
   int nbi_exp, nbo_exp, nbi, nbo;
 
833
   char *lout, *lin;
 
834
   unsigned char *rout;
 
835
   static int BitsMax = 8*sizeof(long);
 
836
 
 
837
   nbi     = infor[0];
 
838
   nbo     = outfor[0];
 
839
   nbi_exp = infor[1];
 
840
   nbo_exp = outfor[1];
 
841
 
 
842
   bi_sign = infor[3] + boffs;
 
843
   bo_sign = outfor[3];
 
844
   bi_exp  = infor[4] + boffs;
 
845
   bo_exp  = outfor[4];
 
846
   bi_mant = infor[5] + boffs;
 
847
   bo_mant = outfor[5];
 
848
 
 
849
   hmbo    = (outfor[6] & 1L);
 
850
   hmbi    = (infor[6] & 1L);
 
851
 
 
852
   inbytes   = (nbi + 7) >> 3;
 
853
   outbytes  = (nbo + 7) >> 3;
 
854
   DeltaBias = outfor[7] + hmbo - infor[7] - hmbi;
 
855
   hexpn     = 1L << (outfor[1] - 1L);
 
856
   expn_max  = (1L << outfor[1]) - 1L;
 
857
 
 
858
    if ( (inord[0] != outord[0]) ||
 
859
         (infor[0] != outfor[0]) || (infor[1] != outfor[1]) ||
 
860
         (infor[2] != outfor[2]) || (infor[3] != outfor[3]) ||
 
861
         (infor[4] != outfor[4]) || (infor[5] != outfor[5]) ||
 
862
         (infor[6] != outfor[6]) || (infor[7] != outfor[7]) )
 
863
   {
 
864
       /*
 
865
        * Zero out the output buffer.
 
866
        */
 
867
       memset(*out, 0, nitems*outbytes);
 
868
 
 
869
       lout = *out;
 
870
       lin  = *in;
 
871
       for (i = 0L; i < nitems; i++) {
 
872
          /*
 
873
           * Move the exponent over.
 
874
           */
 
875
          expn = _lite_PD_extract_field(lin, bi_exp, nbi_exp, inbytes, inord);
 
876
          sign = _PD_get_bit(lin, bi_sign, inbytes, inord);
 
877
 
 
878
          /*
 
879
           * If we have a negative number and ones complement arithmetic on the
 
880
           * input side (won't have it on the output side with modern data)
 
881
           * take the complement of the exponent and mantissa.
 
882
           */
 
883
          if (onescmp) {
 
884
             if (sign) {
 
885
                ONES_COMP_NEG(expn, nbi_exp, 1L);
 
886
             } else {
 
887
                expn += (expn < hexpn);
 
888
             }
 
889
          }
 
890
 
 
891
          if (expn != 0) expn += DeltaBias;
 
892
 
 
893
          if ((0 <= expn) && (expn < expn_max)) {
 
894
             _PD_insert_field(expn, nbo_exp, lout, bo_exp,
 
895
                              l_order, l_bytes);
 
896
 
 
897
             if (sign) _PD_set_bit(lout, bo_sign);
 
898
 
 
899
             indxin  = bi_mant;
 
900
             inrem   = infor[2];
 
901
             indxout = bo_mant;
 
902
             outrem  = outfor[2];
 
903
 
 
904
             dindx = hmbo - hmbi;
 
905
             if (dindx > 0) {
 
906
                /*
 
907
                 * If input high mantissa bit (HMB) is assumed 1 and not written
 
908
                 * (e.g. IEEE) but output HMB is assumed 0 (e.g. CRAY) write the
 
909
                 * input starting at the output HMB+1 and set the HMB.
 
910
                 */
 
911
                _PD_set_bit(lout, indxout);
 
912
                indxout += dindx;
 
913
                outrem  -= dindx;
 
914
             } else if (dindx < 0) {
 
915
                /*
 
916
                 * If input HMB is assumed 0 (e.g. CRAY) but output HMB is assumed
 
917
                 * 1 and not written (e.g. IEEE) take the input from HMB+1 and
 
918
                 * write it to output HMB.
 
919
                 */
 
920
                indxin -= dindx;
 
921
                inrem  += dindx;
 
922
             }
 
923
 
 
924
             /*
 
925
              * Move the mantissa over in sizeof(long) packets.
 
926
              */
 
927
             while ((inrem > 0) && (outrem > 0)) {
 
928
                nbits = MIN(BitsMax, inrem);
 
929
                nbits = MIN(nbits, outrem);
 
930
                mant  = _lite_PD_extract_field(lin, indxin, nbits,
 
931
                                               inbytes, inord);
 
932
 
 
933
                /*
 
934
                 * Do complement for negative ones complement data.
 
935
                 */
 
936
                if (onescmp && sign) ONES_COMP_NEG(mant, nbits, 0L);
 
937
 
 
938
                _PD_insert_field(mant, nbits, lout, indxout,
 
939
                                 l_order, l_bytes);
 
940
 
 
941
                indxin  += nbits;
 
942
                indxout += nbits;
 
943
                inrem   -= nbits;
 
944
                outrem  -= nbits;
 
945
             }
 
946
          } else if (expn_max <= expn) {
 
947
             /*
 
948
              * In case of overflow use 1.0e+(expn_max)
 
949
              */
 
950
             _PD_insert_field(expn_max, nbo_exp, lout, bo_exp,
 
951
                              l_order, l_bytes);
 
952
 
 
953
             if (_PD_get_bit(lin, bi_sign, inbytes, inord))
 
954
                _PD_set_bit(lout, bo_sign);
 
955
          }
 
956
 
 
957
          bi_sign += nbi;
 
958
          bi_exp  += nbi;
 
959
          bi_mant += nbi;
 
960
          bo_sign += nbo;
 
961
          bo_exp  += nbo;
 
962
          bo_mant += nbo;
 
963
       }
 
964
 
 
965
       /*
 
966
        * Handle CRAY inconsistency which has zero as the only floating point
 
967
        * number with a 0 in the HMB. Also problem for IEEE 96 bit float.
 
968
        * Fixed by Dave Munro
 
969
        */
 
970
       if (hmbo) {
 
971
          int j, mask = (1 << (7 - bo_mant % 8));
 
972
 
 
973
          indxout = outfor[5]/8;
 
974
          rout    = (unsigned char *) *out;
 
975
          for (i = 0L; i < nitems; i++, rout += outbytes) {
 
976
             for (j = 0; j < outbytes; j++) {
 
977
                if ((j == indxout) ? (rout[j] != mask) : rout[j]) break;
 
978
             }
 
979
             if (j == outbytes) rout[indxout] = 0;
 
980
          }
 
981
       }
 
982
    }
 
983
    else
 
984
       memcpy(*out, *in, nitems*outbytes);
 
985
 
 
986
   /*
 
987
    * Put the output bytes into the specified order.
 
988
    */
 
989
    for (i=0; i<outbytes; i++)
 
990
       if (inord[i] != outord[i])
 
991
          break;
 
992
    if (i != outbytes)
 
993
       _PD_reorder(*out, nitems, outbytes, outord);
 
994
 
 
995
   *in  += nitems*inbytes;
 
996
   *out += nitems*outbytes;
 
997
}
 
998
 
 
999
/*--------------------------------------------------------------------------*/
 
1000
/*                             HELPER ROUTINES                              */
 
1001
/*--------------------------------------------------------------------------*/
 
1002
 
 
1003
 
 
1004
/*-------------------------------------------------------------------------
 
1005
 * Function:    _PD_ncopy
 
1006
 *
 
1007
 * Purpose:     Copy the NITEMS of size BYTEPITEM from IN to OUT
 
1008
 *
 
1009
 * Return:      void
 
1010
 *
 
1011
 * Programmer:  Adapted from PACT PDB
 
1012
 *              Mar  6, 1996 12:22 PM EST
 
1013
 *
 
1014
 * Modifications:
 
1015
 *
 
1016
 *-------------------------------------------------------------------------
 
1017
 */
 
1018
static void
 
1019
_PD_ncopy (char **out, char **in, long nitems, long bytepitem) {
 
1020
 
 
1021
   long nbytes;
 
1022
 
 
1023
   nbytes = nitems*bytepitem;
 
1024
   memcpy(*out, *in, nbytes);
 
1025
 
 
1026
   *in  += nbytes;
 
1027
   *out += nbytes;
 
1028
}
 
1029
 
 
1030
 
 
1031
/*-------------------------------------------------------------------------
 
1032
 * Function:    _lite_PD_unp_bits
 
1033
 *
 
1034
 * Purpose:     Unpack an array that contains a bitstream.  Arguments are
 
1035
 *
 
1036
 *              ITYP    the target type of the data when unpacked
 
1037
 *              NBITS   the number of bits per item
 
1038
 *              PADSZ   the number of bits of pad preceding the fields
 
1039
 *              FPP     the number of fields per pad
 
1040
 *              NITEMS  the number of items expected
 
1041
 *              OFFS    the bit offset of the first pad
 
1042
 *
 
1043
 * Return:      Success:
 
1044
 *
 
1045
 *              Failure:
 
1046
 *
 
1047
 * Programmer:  Adapted from PACT PDB
 
1048
 *              Mar  6, 1996 12:26 PM EST
 
1049
 *
 
1050
 * Modifications:
 
1051
 *
 
1052
 *-------------------------------------------------------------------------
 
1053
 */
 
1054
int
 
1055
_lite_PD_unp_bits (char *out, char *in, int ityp, int nbits, int padsz,
 
1056
                   int fpp, long nitems, long offs) {
 
1057
 
 
1058
   long i, bita, fld, np, *pl;
 
1059
   char *pc;
 
1060
   int *pi;
 
1061
   short *ps;
 
1062
 
 
1063
   switch (ityp) {
 
1064
   case SC_CHAR_I :
 
1065
      pc = (char *) out;
 
1066
      break;
 
1067
   case SC_SHORT_I :
 
1068
      ps = (short *) out;
 
1069
      break;
 
1070
   case SC_INTEGER_I :
 
1071
      pi = (int *) out;
 
1072
      break;
 
1073
   case SC_LONG_I :
 
1074
      pl = (long *) out;
 
1075
      break;};
 
1076
 
 
1077
   for (i = 0L; i < nitems; i++) {
 
1078
      np   = 1 + i/fpp;
 
1079
      bita = np*padsz + i*nbits + offs;
 
1080
      fld  = _lite_PD_extract_field(in, bita, nbits, INT_MAX, NULL);
 
1081
 
 
1082
      switch (ityp) {
 
1083
      case SC_CHAR_I :
 
1084
         pc[i] = (char) fld;
 
1085
         break;
 
1086
      case SC_SHORT_I :
 
1087
         ps[i] = (short) fld;
 
1088
         break;
 
1089
      case SC_INTEGER_I :
 
1090
         pi[i] = (int) fld;
 
1091
         break;
 
1092
      case SC_LONG_I :
 
1093
         pl[i] = (long) fld;
 
1094
         break;
 
1095
      }
 
1096
   }
 
1097
 
 
1098
   return(TRUE);
 
1099
}
 
1100
 
 
1101
 
 
1102
/*-------------------------------------------------------------------------
 
1103
 * Function:    _PD_sign_extend
 
1104
 *
 
1105
 * Purpose:     Sign extend integers which may come from odd bit
 
1106
 *              sized sources.
 
1107
 *
 
1108
 * Return:      void
 
1109
 *
 
1110
 * Programmer:  Adapted from PACT PDB
 
1111
 *              Mar  6, 1996 12:23 PM EST
 
1112
 *
 
1113
 * Modifications:
 
1114
 *
 
1115
 *-------------------------------------------------------------------------
 
1116
 */
 
1117
static void
 
1118
_PD_sign_extend (char *out, long nitems, int nbo, int nbti, int *ord) {
 
1119
 
 
1120
   int i, j, sba, mask, sign, indx;
 
1121
   unsigned char *lout;
 
1122
 
 
1123
   sba = 8*nbo - nbti;
 
1124
 
 
1125
   /*
 
1126
    * If sba is less than zero we have truncated the integers and really
 
1127
    * don't know about the sign.
 
1128
    */
 
1129
   if (sba < 0) return;
 
1130
 
 
1131
   lout = (unsigned char *) out;
 
1132
   for (i = 0L; i < nitems; i++) {
 
1133
      sign = _PD_get_bit((char *) lout, sba, nbo, ord);
 
1134
      if (sign) {
 
1135
         for (j = 0; (sba > 8); j++) {
 
1136
            indx = (ord != NULL) ? ord[j] : j;
 
1137
            lout[indx] = 0xFF;
 
1138
         }
 
1139
 
 
1140
         mask = ((1 << sba) - 1) << nbti;
 
1141
         indx = (ord != NULL) ? ord[j] : j;
 
1142
         lout[indx] |= mask;
 
1143
      }
 
1144
      lout += nbo;
 
1145
   }
 
1146
}
 
1147
 
 
1148
 
 
1149
/*-------------------------------------------------------------------------
 
1150
 * Function:    _PD_ones_complement
 
1151
 *
 
1152
 * Purpose:     Convert ones complement integers to twos complement.
 
1153
 *              Note: modern machines use two's complement arithmetic
 
1154
 *                    and therefore this is a one way conversion.
 
1155
 *
 
1156
 * Return:      void
 
1157
 *
 
1158
 * Programmer:  Adapted from PACT PDB
 
1159
 *              Mar  6, 1996 12:40 PM EST
 
1160
 *
 
1161
 * Modifications:
 
1162
 *
 
1163
 *-------------------------------------------------------------------------
 
1164
 */
 
1165
static void
 
1166
_PD_ones_complement (char *out, long nitems, int nbo) {
 
1167
 
 
1168
   int i, j;
 
1169
   unsigned int carry;
 
1170
   signed char *lout;
 
1171
 
 
1172
   lout = (signed char *) out;
 
1173
   for (i = 0L; i < nitems; i++) {
 
1174
      if (*lout < 0) {
 
1175
         carry = 1;
 
1176
         for (j = nbo-1; (j >= 0) && (carry > 0); j--) {
 
1177
            carry  += lout[j];
 
1178
            lout[j] = carry & 0xFF;
 
1179
            carry   = (carry > 0xFF);
 
1180
         }
 
1181
      }
 
1182
 
 
1183
      lout += nbo;
 
1184
   }
 
1185
}
 
1186
 
 
1187
 
 
1188
/*-------------------------------------------------------------------------
 
1189
 * Function:    _PD_btrvout
 
1190
 *
 
1191
 * Purpose:     Byte reverse nitems words each word is nb bytes long
 
1192
 *              where nb is even.
 
1193
 *
 
1194
 * Return:      void
 
1195
 *
 
1196
 * Programmer:  Adapted from PACT PDB
 
1197
 *              Mar  6, 1996 12:31 PM EST
 
1198
 *
 
1199
 * Modifications:
 
1200
 *
 
1201
 *-------------------------------------------------------------------------
 
1202
 */
 
1203
static void
 
1204
_PD_btrvout (char *out, long nb, long nitems) {
 
1205
 
 
1206
   long i, jl, jh, nbo2;
 
1207
   char tmp;
 
1208
   char *p1, *p2;
 
1209
 
 
1210
   nbo2 = nb >> 1;
 
1211
   for (jl = 0; jl < nbo2; jl++) {
 
1212
      jh = nb - jl - 1;
 
1213
      p1 = out + jh;
 
1214
      p2 = out + jl;
 
1215
      for (i = 0L; i < nitems; i++) {
 
1216
         tmp = *p1;
 
1217
         *p1 = *p2;
 
1218
         *p2 = tmp;
 
1219
         p1 += nb;
 
1220
         p2 += nb;
 
1221
      }
 
1222
   }
 
1223
}
 
1224
 
 
1225
 
 
1226
/*-------------------------------------------------------------------------
 
1227
 * Function:    _lite_PD_extract_field
 
1228
 *
 
1229
 * Purpose:     Make a copy of the bit field specified by the starting
 
1230
 *              bit OFFS and the number of bits NBI from the byte array
 
1231
 *              pointed to by IN.  All indexing is zero based.  The copy
 
1232
 *              is to be put in a long and returned.  This imposes a 32
 
1233
 *              bit limit (minimum) so repeated calls must be made for
 
1234
 *              longer fields.
 
1235
 *
 
1236
 * Return:
 
1237
 *
 
1238
 * Programmer:  Adapted from PACT PDB
 
1239
 *              Mar  6, 1996 12:32 PM EST
 
1240
 *
 
1241
 * Modifications:
 
1242
 *
 
1243
 *-------------------------------------------------------------------------
 
1244
 */
 
1245
long
 
1246
_lite_PD_extract_field (char *in, int offs, int nbi, int nby, int *ord) {
 
1247
 
 
1248
   long n, bit_field;
 
1249
   int offy, tgt, ind;
 
1250
   unsigned char mask, bpb;
 
1251
 
 
1252
   bit_field = 0L;
 
1253
 
 
1254
   /*
 
1255
    * Move past the apropriate number of bytes so that the start bit is
 
1256
    * in the first byte.  OFFY is the offset of the byte containing the
 
1257
    * bit OFFS.
 
1258
    */
 
1259
   n    = offs >> 3;
 
1260
   offy = n % nby;
 
1261
   n   -= offy;
 
1262
   offs = offs % 8;
 
1263
 
 
1264
   /*
 
1265
    * Advance the pointer past the unneeded items.
 
1266
    */
 
1267
   in += n;
 
1268
 
 
1269
   bpb  = 8 - offs;
 
1270
   if (ord == NULL) {
 
1271
      ind = offy++;
 
1272
   } else {
 
1273
      if (offy >= nby) {
 
1274
         offy -= nby;
 
1275
         in += nby;
 
1276
      }
 
1277
      ind = (ord[offy++] - 1);
 
1278
   }
 
1279
 
 
1280
   tgt  = in[ind];
 
1281
   mask = (1 << bpb) - 1;
 
1282
   bit_field = ((bit_field << bpb) | (tgt & mask));
 
1283
   nbi -= bpb;
 
1284
   if (nbi < 0) {
 
1285
      bit_field = bit_field >> (-nbi);
 
1286
   } else {
 
1287
      for (; nbi > 0; nbi -= bpb) {
 
1288
         if (ord == NULL) {
 
1289
            ind = offy++;
 
1290
         } else {
 
1291
            if (offy >= nby) {
 
1292
               offy -= nby;
 
1293
               in += nby;
 
1294
            }
 
1295
            ind = (ord[offy++] - 1);
 
1296
         }
 
1297
 
 
1298
         tgt  = in[ind];
 
1299
         bpb  = MIN(nbi, 8);
 
1300
         mask = (1 << bpb) - 1;
 
1301
         bit_field = ((bit_field << bpb) |
 
1302
                      ((tgt >> (8 - bpb)) & mask));
 
1303
      }
 
1304
   }
 
1305
   return(bit_field);
 
1306
}
 
1307
 
 
1308
 
 
1309
/*-------------------------------------------------------------------------
 
1310
 * Function:    _PD_insert_field
 
1311
 *
 
1312
 * Purpose:     Copy the least significatn NB bits from the given long
 
1313
 *              into the byte array pointed to by OUT.  All indexint
 
1314
 *              is zero based. OFFS is the offst from the beginning of
 
1315
 *              OUT in bits.  This assumes that the output bit array is
 
1316
 *              initialized to all zeros after offs.
 
1317
 *
 
1318
 * Return:      void
 
1319
 *
 
1320
 * Programmer:  Adapted from PACT PDB
 
1321
 *              Mar  6, 1996 12:35 PM EST
 
1322
 *
 
1323
 * Modifications:
 
1324
 *
 
1325
 *-------------------------------------------------------------------------
 
1326
 */
 
1327
static void
 
1328
_PD_insert_field (long in_long, int nb, char *out, int offs,
 
1329
                  int l_order, int l_bytes) {
 
1330
 
 
1331
   int mi, n, dm;
 
1332
   long longmask;
 
1333
   static int MaxBits = 8*sizeof(long);
 
1334
   unsigned char fb;
 
1335
   char *in;
 
1336
 
 
1337
   in = (char *) &in_long;
 
1338
 
 
1339
   /*
 
1340
    * If the output start bit is not in the first byte move past the apropriate
 
1341
    * number of bytes so that the start bit is in the first byte.
 
1342
    */
 
1343
   if (offs > 7) {
 
1344
      out  += (offs >> 3);
 
1345
      offs %= 8;
 
1346
   }
 
1347
 
 
1348
   /*
 
1349
    * If mi is less than offs, copy the first dm bits over, reset offs to 0,
 
1350
    * advance mi by dm, and handle the rest as if mi >= offs.
 
1351
    */
 
1352
   mi = MaxBits - nb;
 
1353
   if (mi < offs) {
 
1354
      dm = MaxBits - (8 - offs);
 
1355
      if (nb == MaxBits) {
 
1356
         longmask = ~((1L << dm) - 1L);
 
1357
      } else {
 
1358
         longmask = ((1L << nb) - 1L) ^ ((1L << dm) - 1L);
 
1359
      }
 
1360
 
 
1361
      fb = ((in_long & longmask) >> dm) & ((1L << (nb - dm)) - 1L);
 
1362
      *(out++) |= fb;
 
1363
 
 
1364
      mi  += 8 - offs;
 
1365
      offs = 0;
 
1366
   }
 
1367
 
 
1368
   /*
 
1369
    * Assuming mi >= offs, left shift the input so that it is bit aligned
 
1370
    * with the output.
 
1371
    */
 
1372
   dm = mi - offs;
 
1373
   longmask = ~((1L << dm) - 1L);
 
1374
   in_long  = (in_long << dm) & longmask;
 
1375
 
 
1376
   /*
 
1377
    * Reorder the bytes apropriately.
 
1378
    */
 
1379
   if (l_order == REVERSE_ORDER) _PD_btrvout(in, l_bytes, 1L);
 
1380
 
 
1381
   /*
 
1382
    * Copy the remaining aligned bytes over.
 
1383
    */
 
1384
   for (n = (offs+nb+7)/8; n > 0; n--, *(out++) |= *(in++)) /*void*/ ;
 
1385
}
 
1386
 
 
1387
 
 
1388
/*-------------------------------------------------------------------------
 
1389
 * Function:    _PD_set_bit
 
1390
 *
 
1391
 * Purpose:     Set the bit specified as o offset from the given pointer.
 
1392
 *
 
1393
 * Return:      void
 
1394
 *
 
1395
 * Programmer:  Adapted from PACT PDB
 
1396
 *              Mar  6, 1996 12:37 PM EST
 
1397
 *
 
1398
 * Modifications:
 
1399
 *
 
1400
 *-------------------------------------------------------------------------
 
1401
 */
 
1402
static void
 
1403
_PD_set_bit (char *base, int offs) {
 
1404
 
 
1405
   int nbytes, mask;
 
1406
 
 
1407
   nbytes  = offs >> 3;
 
1408
   base   += nbytes;
 
1409
   offs   -= 8*nbytes;
 
1410
 
 
1411
   mask    = (1 << (7 - offs));
 
1412
   *base  |= mask;
 
1413
}
 
1414
 
 
1415
 
 
1416
/*-------------------------------------------------------------------------
 
1417
 * Function:    _PD_get_bit
 
1418
 *
 
1419
 * Purpose:     Return the bit specified as on offst from the given ptr.
 
1420
 *
 
1421
 * Return:      Success:        0 or 1
 
1422
 *
 
1423
 *              Failure:
 
1424
 *
 
1425
 * Programmer:  Adapted from PACT PDB
 
1426
 *              Mar  6, 1996 12:38 PM EST
 
1427
 *
 
1428
 * Modifications:
 
1429
 *
 
1430
 *-------------------------------------------------------------------------
 
1431
 */
 
1432
static int
 
1433
_PD_get_bit (char *base, int offs, int nby, int *ord) {
 
1434
 
 
1435
   int nbytes, mask, n;
 
1436
 
 
1437
   n      = offs >> 3;
 
1438
   nbytes = n % nby;
 
1439
   n     -= nbytes;
 
1440
   offs   = offs % 8;
 
1441
 
 
1442
   if (ord == NULL) base += (n + nbytes);
 
1443
   else base += (n + (ord[nbytes] - 1));
 
1444
 
 
1445
   mask = (1 << (7 - offs));
 
1446
 
 
1447
   return((*base & mask) != 0);
 
1448
}
 
1449
 
 
1450
 
 
1451
/*-------------------------------------------------------------------------
 
1452
 * Function:    _PD_reorder
 
1453
 *
 
1454
 * Purpose:     Given a pointer to an array ARR with NITEMS of NBYTES each
 
1455
 *              put them in the order defined by ORD.
 
1456
 *
 
1457
 * Return:      void
 
1458
 *
 
1459
 * Programmer:  Adapted from PACT PDB
 
1460
 *              Mar  6, 1996 12:39 PM EST
 
1461
 *
 
1462
 * Modifications:
 
1463
 *
 
1464
 *-------------------------------------------------------------------------
 
1465
 */
 
1466
static void
 
1467
_PD_reorder (char *arr, long nitems, int nbytes, int *ord) {
 
1468
 
 
1469
   char local[MAXLINE];
 
1470
   int j;
 
1471
 
 
1472
   for (; nitems > 0; nitems--) {
 
1473
      arr--;
 
1474
      for (j = 0; j < nbytes; local[j] = arr[ord[j]], j++) /*void*/ ;
 
1475
      arr++;
 
1476
      for (j = 0; j < nbytes; *(arr++) = local[j++]) /*void*/ ;
 
1477
   }
 
1478
}