-
Notifications
You must be signed in to change notification settings - Fork 1
/
dat1_init_ndr.c
1212 lines (1137 loc) · 51.1 KB
/
dat1_init_ndr.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <limits.h>
#include <float.h>
#include <stdio.h>
#include "hds1.h" /* Global definitions for HDS */
#include "rec1.h" /* Internal rec_ definitions */
#include "dat1.h" /* Internal dat_ definitions */
#include "dat_err.h" /* DAT__ error code definitions */
#include "ems.h" /* ems_ public definitions */
#include "ems_par.h" /* ems__ constants */
void dat1_init_ndr( int *status )
{
/*+ */
/* Name: */
/* dat1_init_ndr */
/* Purpose: */
/* Initialise the global native data representation structure. */
/* Invocation: */
/* dat1_init_ndr( status ) */
/* Description: */
/* This function initialises the global native data representation */
/* structure which holds information about the way primitive values are */
/* stored on the current host machine. */
/* Parameters: */
/* int *status */
/* The inherited global status. */
/* Returned Value: */
/* void */
/* Notes: */
/* - This routine makes considerable use of macros because it must */
/* apply similar processing to a range of arithmetic data types whose */
/* characteristics are not known directly (typically they are themselves */
/* parameterised via the HDS primitive type macros). The macros used */
/* here appear, and are commented, as if they formed part of the normal */
/* executable code. */
/* - Although this routine works correctly on a renge of known */
/* compilers with the maximum level of optimisation enabled, it is */
/* always possible that a particularly awkward compiler may give */
/* problems. If this routine inexplicably fails to recognise the machine */
/* data representation, then a reduction in the level of compiler */
/* optimisation may help. */
/* Copyright: */
/* Copyright (C) 1992 Science & Engineering Research Council */
/* Copyright (C) 2006 Particle Physics and Engineering Research Council */
/* Licence: */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License as */
/* published by the Free Software Foundation; either version 2 of */
/* the License, or (at your option) any later version. */
/* This program 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 General Public License for more details. */
/* You should have received a copy of the GNU General Public */
/* License along with this program; if not, write to the Free */
/* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, */
/* MA 02110-1301, USA */
/* Authors: */
/* RFWS: R.F. Warren-Smith (STARLINK) */
/* TIMJ: Tim Jenness (JAC, Hawaii) */
/* {@enter_new_authors_here@} */
/* History: */
/* 3-SEP-1992 (RFWS): */
/* Original version. */
/* 5-OCT-1992 (RFWS): */
/* Modified to initialise the number of decimal digits of precision. */
/* 22-DEC-1992 (RFWS): */
/* Added the _specialbits macro to avoid problems with integer */
/* overflow reported by some compilers when setting up special bit */
/* patterns in signed integer data types. Also added dat1_decoy calls */
/* to prevent unwanted compiler optimisation. */
/* 03-JAN-2005 (TIMJ): */
/* Turn on 'long double' code via autoconf tests */
/* 25-APR-2012 (TIMJ): */
/* Add _INT64 support. */
/* {@enter_further_changes_here@} */
/* Bugs: */
/* {@note_any_bugs_here@} */
/*- */
/* Local constants: */
#define BUFSIZE 100 /* Size of text buffers for formatting */
/* Local Data Types: */
enum TYPID { /* Code to identify arithmetic data types */
SCHARTYPE,
UCHARTYPE,
SHRTTYPE,
USHRTTYPE,
INTTYPE,
UINTTYPE,
LONGTYPE,
ULONGTYPE,
FLTTYPE,
DBLTYPE,
I64TYPE,
UI64TYPE,
LDBLTYPE
};
union TYP { /* Union for holding any C arithmetic type */
#if HAVE_SIGNED_CHAR
signed char scharval;
#else
char scharval;
#endif
unsigned char ucharval;
short int shrtval;
unsigned short int ushrtval;
int intval;
unsigned int uintval;
long int longval;
unsigned long int ulongval;
float fltval;
double dblval;
int64_t i64val;
uint64_t ui64val;
#if HAVE_LONG_DOUBLE
long double ldblval;
#endif
};
/* Local Variables: */
_DOUBLE eps_DOUBLE; /* Smallest increment for _DOUBLE type */
_REAL eps_REAL; /* Smallest increment for _REAL type */
int i; /* Loop counter for bytes */
unsigned char *ptr; /* Pointer to byte sequence */
unsigned char IEEE_D_dig; /* No. digits for IEEE_D numbers */
unsigned char IEEE_S_dig; /* No. digits for IEEE_S numbers */
unsigned char VAXD_dig; /* No. digits for VAXD numbers */
unsigned char VAXF_dig; /* No. digits for VAXF numbers */
unsigned char dbl_dig; /* No. digits for double numbers */
unsigned char dbl_format; /* Data format code for double values */
#if HAVE_LONG_DOUBLE
unsigned char ldbl_dig; /* No. digits for long double numbers */
unsigned char ldbl_format; /* Data format code for long double values */
#endif
unsigned char dig_DOUBLE; /* No. digits for _DOUBLE numbers */
unsigned char dig_REAL; /* No. digits for _REAL numbers */
unsigned char flt_dig; /* No. digits for float numbers */
unsigned char flt_format; /* Data format code for float values */
unsigned char format_DOUBLE; /* Data format code for _DOUBLE values */
unsigned char format_REAL; /* Data format code for _REAL values */
/*. */
/* Check the inherited global status. */
if ( !_ok( *status ) ) return;
/* Initialisations to stop optimized compiler warnings */
format_REAL = 0;
format_DOUBLE = 0;
dig_REAL = 0;
dig_DOUBLE = 0;
eps_REAL = 0.0;
eps_DOUBLE = 0.0;
/* Set up the name of each primitive data type. */
/* =========================================== */
dat_gl_ndr[ DAT__C ].name = "_CHAR";
dat_gl_ndr[ DAT__L ].name = "_LOGICAL";
dat_gl_ndr[ DAT__B ].name = "_BYTE";
dat_gl_ndr[ DAT__UB ].name = "_UBYTE";
dat_gl_ndr[ DAT__W ].name = "_WORD";
dat_gl_ndr[ DAT__UW ].name = "_UWORD";
dat_gl_ndr[ DAT__I ].name = "_INTEGER";
dat_gl_ndr[ DAT__R ].name = "_REAL";
dat_gl_ndr[ DAT__D ].name = "_DOUBLE";
dat_gl_ndr[ DAT__K ].name = "_INT64";
/* Set up the length of each primitive data type. */
/* ============================================= */
dat_gl_ndr[ DAT__C ].length = sizeof( _CHAR );
dat_gl_ndr[ DAT__L ].length = sizeof( _LOGICAL );
dat_gl_ndr[ DAT__B ].length = sizeof( _BYTE );
dat_gl_ndr[ DAT__UB ].length = sizeof( _UBYTE );
dat_gl_ndr[ DAT__W ].length = sizeof( _WORD );
dat_gl_ndr[ DAT__UW ].length = sizeof( _UWORD );
dat_gl_ndr[ DAT__I ].length = sizeof( _INTEGER );
dat_gl_ndr[ DAT__R ].length = sizeof( _REAL );
dat_gl_ndr[ DAT__D ].length = sizeof( _DOUBLE );
dat_gl_ndr[ DAT__K ].length = sizeof( _INT64 );
/* Identify the format used for each C floating-point type. */
/* ======================================================= */
/* Use the ANSI C floating point description in <float.h> to distinguish */
/* the important formats. Note that this must be done at run-time, because */
/* ANSI does not guarantee that these values are suitable for use in */
/* pre-processor directives. */
/* Note that the objective here is simply to distinguish the currently */
/* supported floating-point types. There is no guarantee that we can also */
/* distinguish these from new types not yet encountered (although in */
/* practice this may happen). If new formats are to be supported, then new */
/* format codes must be assigned (in dat1.h), new tests installed here, and */
/* new format-conversion algorithms provided. */
/* Set the number of decimal digits of precision for each floating point */
/* type. */
IEEE_S_dig = 9; /* Value given in IEEE standard */
IEEE_D_dig = 17; /* Value given in IEEE standard */
VAXF_dig = 9;
VAXD_dig = 18;
/* Identify the float format: */
if ( ( FLT_RADIX == 2 ) &&
( FLT_MANT_DIG == 24 ) &&
( FLT_MIN_EXP == -127 ) &&
( FLT_MAX_EXP == 127 ) )
{
flt_format = DAT__VAXF; /* VAX F-floating (single-precision) */
flt_dig = VAXF_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( FLT_MANT_DIG == 56 ) &&
( FLT_MIN_EXP == -127 ) &&
( FLT_MAX_EXP == 127 ) )
{
flt_format = DAT__VAXD; /* VAX D-floating (double precision) */
flt_dig = VAXD_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( FLT_MANT_DIG == 24 ) &&
( FLT_MIN_EXP == -125 ) &&
( FLT_MAX_EXP == 128 ) )
{
flt_format = DAT__IEEE_S; /* IEEE standard single precision */
flt_dig = IEEE_S_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( FLT_MANT_DIG == 53 ) &&
( FLT_MIN_EXP == -1021 ) &&
( FLT_MAX_EXP == 1024 ) )
{
flt_format = DAT__IEEE_D; /* IEEE standard double precision */
flt_dig = IEEE_D_dig;
}
/* If the format is not recognised, note this fact, but do not report an */
/* error yet since it may not actually be used as a primitive HDS data */
/* type. */
else
{
flt_format = DAT__UNKNOWN;
flt_dig = DAT__UNKNOWN;
}
/* Identify the double format: */
if ( ( FLT_RADIX == 2 ) &&
( DBL_MANT_DIG == 24 ) &&
( DBL_MIN_EXP == -127 ) &&
( DBL_MAX_EXP == 127 ) )
{
dbl_format = DAT__VAXF; /* VAX F-floating (single-precision) */
dbl_dig = VAXF_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( DBL_MANT_DIG == 56 ) &&
( DBL_MIN_EXP == -127 ) &&
( DBL_MAX_EXP == 127 ) )
{
dbl_format = DAT__VAXD; /* VAX D-floating (double precision) */
dbl_dig = VAXD_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( DBL_MANT_DIG == 24 ) &&
( DBL_MIN_EXP == -125 ) &&
( DBL_MAX_EXP == 128 ) )
{
dbl_format = DAT__IEEE_S; /* IEEE standard single precision */
dbl_dig = IEEE_S_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( DBL_MANT_DIG == 53 ) &&
( DBL_MIN_EXP == -1021 ) &&
( DBL_MAX_EXP == 1024 ) )
{
dbl_format = DAT__IEEE_D; /* IEEE standard double precision */
dbl_dig = IEEE_D_dig;
}
/* If the format is not recognised, note this fact, but do not report an */
/* error yet since it may not actually be used as a primitive HDS data */
/* type. */
else
{
dbl_format = DAT__UNKNOWN;
dbl_dig = DAT__UNKNOWN;
}
#if HAVE_LONG_DOUBLE
/* Identify the long double format: */
if ( ( FLT_RADIX == 2 ) &&
( LDBL_MANT_DIG == 24 ) &&
( LDBL_MIN_EXP == -127 ) &&
( LDBL_MAX_EXP == 127 ) )
{
ldbl_format = DAT__VAXF; /* VAX F-floating (single-precision) */
ldbl_dig = VAXF_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( LDBL_MANT_DIG == 56 ) &&
( LDBL_MIN_EXP == -127 ) &&
( LDBL_MAX_EXP == 127 ) )
{
ldbl_format = DAT__VAXD; /* VAX D-floating (double precision) */
ldbl_dig = VAXD_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( LDBL_MANT_DIG == 24 ) &&
( LDBL_MIN_EXP == -125 ) &&
( LDBL_MAX_EXP == 128 ) )
{
ldbl_format = DAT__IEEE_S; /* IEEE standard single precision */
ldbl_dig = IEEE_S_dig;
}
else if ( ( FLT_RADIX == 2 ) &&
( LDBL_MANT_DIG == 53 ) &&
( LDBL_MIN_EXP == -1021 ) &&
( LDBL_MAX_EXP == 1024 ) )
{
ldbl_format = DAT__IEEE_D; /* IEEE standard double precision */
ldbl_dig = IEEE_D_dig;
}
/* If the format is not recognised, note this fact, but do not report an */
/* error yet since it may not actually be used as a primitive HDS data */
/* type. */
else
{
ldbl_format = DAT__UNKNOWN;
ldbl_dig = DAT__UNKNOWN;
}
#endif
/* Set lower and upper limits for the primitive data types. */
/* ======================================================= */
/* The _CHAR and _LOGICAL types do not have limits - just set them to zero. */
dat_gl_ndr[ DAT__C ].min.C = (_CHAR) 0;
dat_gl_ndr[ DAT__C ].max.C = (_CHAR) 0;
dat_gl_ndr[ DAT__L ].min.L = (_LOGICAL) 0;
dat_gl_ndr[ DAT__L ].max.L = (_LOGICAL) 0;
/* The remaining data types will have their limits specified in <limits.h> */
/* or <float.h>, but we first have to identify which limits belong with */
/* which HDS primitive type. */
/* Define a macro to determine whether an integer type is signed or not, */
/* returning 0 or 1. */
#define _signed( dtype ) ( ( (dtype) -1 ) < ( (dtype) 0 ) )
/* Define a macro to classify an integer type and to assign appropriate */
/* limits. */
#define _limitsi( dtype, min, max )\
{\
enum TYPID id; /* Code to identify data type */\
union TYP hi; /* Temporary storage for upper limit */\
union TYP lo; /* Temporary storage for lower limit */\
\
/* Match the size and signed-ness of the integer data type against each */\
/* possibility. */\
if ( _ok( *status ) )\
{\
if ( ( sizeof( dtype ) == sizeof( char ) ) &&\
_signed( dtype ) )\
{\
\
/* When a match is found, set a code to identify it and obtain the lower */\
/* and upper bounds for that C data type, setting these in the appropriate */\
/* element of the unions lo and hi. */\
id = SCHARTYPE;\
lo.scharval = SCHAR_MIN;\
hi.scharval = SCHAR_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( unsigned char ) ) &&\
!_signed( dtype ) )\
{\
id = UCHARTYPE;\
lo.ucharval = (unsigned char) 0;\
hi.ucharval = UCHAR_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( short int ) ) &&\
_signed( dtype ) )\
{\
id = SHRTTYPE;\
lo.shrtval = SHRT_MIN;\
hi.shrtval = SHRT_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( unsigned short int ) ) &&\
!_signed( dtype ) )\
{\
id = USHRTTYPE;\
lo.ushrtval = (unsigned short int) 0;\
hi.ushrtval = USHRT_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( int ) ) &&\
_signed( dtype ) )\
{\
id = INTTYPE;\
lo.intval = INT_MIN;\
hi.intval = INT_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( unsigned int ) ) &&\
!_signed( dtype ) )\
{\
id = UINTTYPE;\
lo.uintval = (unsigned int) 0;\
hi.uintval = UINT_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( long int ) ) &&\
_signed( dtype ) )\
{\
id = LONGTYPE;\
lo.longval = LONG_MIN;\
hi.longval = LONG_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( unsigned long int ) ) &&\
!_signed( dtype ) )\
{\
id = ULONGTYPE;\
lo.ulongval = (unsigned long int) 0;\
hi.ulongval = ULONG_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( int64_t ) ) &&\
_signed( dtype ) )\
{\
id = I64TYPE;\
lo.i64val = INT64_MIN;\
hi.i64val = INT64_MAX;\
}\
else if ( ( sizeof( dtype ) == sizeof( uint64_t ) ) &&\
!_signed( dtype ) )\
{\
id = UI64TYPE;\
lo.ui64val = (uint64_t) 0;\
hi.ui64val = UINT64_MAX;\
}\
\
/* If a primitive data type cannot be identified, then report an error. */\
else\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_1",\
"Unable to identify an integer data type; HDS may \
require modification for use on this machine.",\
status );\
}\
}\
\
/* If the data type has been identified, make a decoy call so defeat */\
/* optimisation. Then test the type identification code and move the limits */\
/* obtained to the required locations. (Note this intermediate stage of */\
/* storage is solely to defeat compilers which are too clever; without it */\
/* they may try to assign the constant limit values at compile-time. This */\
/* can cause overflow and prevent compilation.) */\
if ( _ok( *status ) )\
{\
dat1_decoy( (int) id, (void *) &id );\
switch( id )\
{\
case SCHARTYPE:\
{\
(min) = (dtype) lo.scharval;\
(max) = (dtype) hi.scharval;\
break;\
}\
case UCHARTYPE:\
{\
(min) = (dtype) lo.ucharval;\
(max) = (dtype) hi.ucharval;\
break;\
}\
case SHRTTYPE:\
{\
(min) = (dtype) lo.shrtval;\
(max) = (dtype) hi.shrtval;\
break;\
}\
case USHRTTYPE:\
{\
(min) = (dtype) lo.ushrtval;\
(max) = (dtype) hi.ushrtval;\
break;\
}\
case INTTYPE:\
{\
(min) = (dtype) lo.intval;\
(max) = (dtype) hi.intval;\
break;\
}\
case UINTTYPE:\
{\
(min) = (dtype) lo.uintval;\
(max) = (dtype) hi.uintval;\
break;\
}\
case LONGTYPE:\
{\
(min) = (dtype) lo.longval;\
(max) = (dtype) hi.longval;\
break;\
}\
case ULONGTYPE:\
{\
(min) = (dtype) lo.ulongval;\
(max) = (dtype) hi.ulongval;\
break;\
}\
case I64TYPE:\
{\
(min) = (dtype) lo.i64val;\
(max) = (dtype) hi.i64val;\
break;\
}\
case UI64TYPE:\
{\
(min) = (dtype) lo.ui64val;\
(max) = (dtype) hi.ui64val;\
break;\
}\
default:\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_1b",\
"Fell off end of switch statement unexpectedly when determining integer data type",\
status );\
}\
}\
}\
}
/* Define a similar macro to classify a floating-point type and to assign */
/* appropriate limits. In this case, we also require values for the */
/* smallest increment value, the number of decimal digits of precision and */
/* the data format code. */
#if HAVE_LONG_DOUBLE
#define _limitsf( dtype, min, max, eps, dig, format )\
{\
enum TYPID id; /* Code to identify data type */\
union TYP hi; /* Temporary storage for upper limit */\
union TYP lo; /* Temporary storage for lower limit */\
union TYP tiny; /* Temporary storage for tinyest increment */\
\
/* Match the size of the floating-point data type against each possibility. */\
if ( _ok( *status ) )\
{\
if ( sizeof( dtype ) == sizeof( float ) )\
{\
\
/* When a match is found, set a code to identify it and obtain the lower */\
/* and upper bounds for that C data type, setting these in the appropriate */\
/* element of the unions lo and hi. Also set a value for the smallest */\
/* increment in the tiny union. */\
id = FLTTYPE;\
lo.fltval = - FLT_MAX;\
hi.fltval = FLT_MAX;\
tiny.fltval = FLT_EPSILON;\
}\
else if ( sizeof( dtype ) == sizeof( double ) )\
{\
id = DBLTYPE;\
lo.dblval = - DBL_MAX;\
hi.dblval = DBL_MAX;\
tiny.dblval = DBL_EPSILON;\
}\
else if ( sizeof( dtype ) == sizeof( long double ) )\
{\
id = LDBLTYPE;\
lo.ldblval = - LDBL_MAX;\
hi.ldblval = LDBL_MAX;\
tiny.ldblval = LDBL_EPSILON;\
}\
\
/* If a primitive data type cannot be identified, then report an error. */\
else\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_2",\
"Unable to identify a floating-point data type; HDS \
may require modification for use on this machine.",\
status );\
}\
}\
\
/* If the data type has been identified, make a decoy call to defeat */\
/* optimisation. Then test the type identification code and move the */\
/* limits obtained to the required locations. (Note this intermediate stage */\
/* of storage is solely to defeat compilers which are too clever; without */\
/* it they may try to assign the constant limit values at compile-time. */\
/* This can cause overflow and prevent compilation.) */\
if ( _ok( *status ) )\
{\
dat1_decoy( (int) id, (void *) &id );\
switch( id )\
{\
case FLTTYPE:\
{\
(min) = (dtype) lo.fltval;\
(max) = (dtype) hi.fltval;\
(eps) = (dtype) tiny.fltval;\
\
/* Also set the number of decimal digits of precision and the data format */\
/* code appropriate to this data type (as determined earlier). */\
(dig) = flt_dig;\
(format) = flt_format;\
break;\
}\
case DBLTYPE:\
{\
(min) = (dtype) lo.dblval;\
(max) = (dtype) hi.dblval;\
(eps) = (dtype) tiny.dblval;\
(dig) = dbl_dig;\
(format) = dbl_format;\
break;\
}\
case LDBLTYPE:\
{\
(min) = (dtype) lo.ldblval;\
(max) = (dtype) hi.ldblval;\
(eps) = (dtype) tiny.ldblval;\
(dig) = ldbl_dig;\
(format) = ldbl_format;\
break;\
}\
default:\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_2b",\
"Fell off end of switch statement unexpectedly when determining float data type",\
status );\
}\
}\
}\
}
#else
/* Don't use long double if it is not supported by the compiler. */
#define _limitsf( dtype, min, max, eps, dig, format )\
{\
enum TYPID id; /* Code to identify data type */\
union TYP hi; /* Temporary storage for upper limit */\
union TYP lo; /* Temporary storage for lower limit */\
union TYP tiny; /* Temporary storage for tinyest increment */\
if ( _ok( *status ) )\
{\
if ( sizeof( dtype ) == sizeof( float ) )\
{\
id = FLTTYPE;\
lo.fltval = - FLT_MAX;\
hi.fltval = FLT_MAX;\
tiny.fltval = FLT_EPSILON;\
}\
else if ( sizeof( dtype ) == sizeof( double ) )\
{\
id = DBLTYPE;\
lo.dblval = - DBL_MAX;\
hi.dblval = DBL_MAX;\
tiny.dblval = DBL_EPSILON;\
}\
else\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_3",\
"Unable to identify a floating-point data type; HDS \
may require modification for use on this machine.",\
status );\
}\
}\
if ( _ok( *status ) )\
{\
dat1_decoy( (int) id, (void *) &id );\
switch( id )\
{\
case FLTTYPE:\
{\
(min) = (dtype) lo.fltval;\
(max) = (dtype) hi.fltval;\
(eps) = (dtype) tiny.fltval;\
(dig) = flt_dig;\
(format) = flt_format;\
break;\
}\
case DBLTYPE:\
{\
(min) = (dtype) lo.dblval;\
(max) = (dtype) hi.dblval;\
(eps) = (dtype) tiny.dblval;\
(dig) = dbl_dig;\
(format) = dbl_format;\
break;\
}\
default:\
{\
*status = DAT__FATAL;\
emsRep( "DAT1_INIT_NDR_2b",\
"Fell off end of switch statement unexpectedly when determining float data type",\
status );\
}\
}\
}\
}
#endif
/* Invoke these macros to assign limits to each of the remaining primitive */
/* data types. */
_limitsi( _BYTE,
dat_gl_ndr[ DAT__B ].min.B,
dat_gl_ndr[ DAT__B ].max.B )
_limitsi( _UBYTE,
dat_gl_ndr[ DAT__UB ].min.UB,
dat_gl_ndr[ DAT__UB ].max.UB )
_limitsi( _WORD,
dat_gl_ndr[ DAT__W ].min.W,
dat_gl_ndr[ DAT__W ].max.W )
_limitsi( _UWORD,
dat_gl_ndr[ DAT__UW ].min.UW,
dat_gl_ndr[ DAT__UW ].max.UW )
_limitsi( _INTEGER,
dat_gl_ndr[ DAT__I ].min.I,
dat_gl_ndr[ DAT__I ].max.I )
_limitsi( _INT64,
dat_gl_ndr[ DAT__K ].min.K,
dat_gl_ndr[ DAT__K ].max.K )
_limitsf( _REAL,
dat_gl_ndr[ DAT__R ].min.R,
dat_gl_ndr[ DAT__R ].max.R,
eps_REAL, dig_REAL, format_REAL )
_limitsf( _DOUBLE,
dat_gl_ndr[ DAT__D ].min.D,
dat_gl_ndr[ DAT__D ].max.D,
eps_DOUBLE, dig_DOUBLE, format_DOUBLE )
/* Assign the "bad" value for each data type. */
/* ========================================= */
/* A '*' is always used for the bad _CHAR value. */
if ( _ok( *status ) )
{
dat_gl_ndr[ DAT__C ].bad.C = (_CHAR) '*';
/* The bad _LOGICAL value is set to an alternating sequence of zero and one */
/* bits which is unlikely to occur by accident. It is also made */
/* palindromic, so its value does not alter with byte reversal. */
ptr = (unsigned char *) &( dat_gl_ndr[ DAT__L ].bad.L );
for ( i = 0; i < ( ( sizeof( _LOGICAL ) + 1 ) / 2 ); i++ )
{
ptr[ i ] = (unsigned char) ( ( i % 2 ) ? 0x5aU : 0xa5U );
ptr[ sizeof( _LOGICAL ) - i - 1 ] = ptr[ i ];
}
/* The remaining bad values are set to the appropriate limit. */
dat_gl_ndr[ DAT__B ].bad.B = dat_gl_ndr[ DAT__B ].min.B;
dat_gl_ndr[ DAT__UB ].bad.UB = dat_gl_ndr[ DAT__UB ].max.UB;
dat_gl_ndr[ DAT__W ].bad.W = dat_gl_ndr[ DAT__W ].min.W;
dat_gl_ndr[ DAT__UW ].bad.UW = dat_gl_ndr[ DAT__UW ].max.UW;
dat_gl_ndr[ DAT__I ].bad.I = dat_gl_ndr[ DAT__I ].min.I;
dat_gl_ndr[ DAT__R ].bad.R = dat_gl_ndr[ DAT__R ].min.R;
dat_gl_ndr[ DAT__D ].bad.D = dat_gl_ndr[ DAT__D ].min.D;
dat_gl_ndr[ DAT__K ].bad.K = dat_gl_ndr[ DAT__K ].min.K;
}
/* Determine the byte storage order for each primitive data type. */
/* ============================================================= */
/* This is done by comparing two values of each type which differ by the */
/* smallest significant difference. The byte which has changed is then */
/* identified and the storage order assigned depending on whether this */
/* change occurs before or after the half-way point. */
/* Define a macro to perform the comparisons and to assign the result. */
#define _order( dtype, value1, value2, order )\
{\
unsigned char *ptr1; /* Pointer to byte sequence */\
unsigned char *ptr2; /* Pointer to byte sequence */\
dtype var1; /* Temporary variable of appropriate type */\
dtype var2; /* Temporary variable of appropriate type */\
\
/* DAT__MSB is always assigned if the size is one. */\
if ( sizeof( dtype ) == 1 )\
{\
(order) = DAT__MSB;\
}\
\
/* Otherwise, assign each value to a variable of the appropriate type and */\
/* obtain pointers to the resulting byte sequences. */\
else\
{\
(var1) = (dtype) (value1);\
(var2) = (dtype) (value2);\
ptr1 = (unsigned char *) &var1;\
ptr2 = (unsigned char *) &var2;\
\
/* Make decoy calls to ensure the required values are actually evaluated. */\
dat1_decoy( (int) var1, (void *) &var1 );\
dat1_decoy( (int) var2, (void *) &var2 );\
\
/* Find the first byte which differs and test if it is before the half-way */\
/* point. */\
for ( i = 0; i < sizeof( dtype ); i++ )\
{\
if ( ptr1[ i ] != ptr2[ i ] ) break;\
}\
(order) = ( i < ( ( sizeof( dtype ) + 1 ) / 2 ) ) ?\
DAT__LSB : DAT__MSB;\
}\
}
/* Invoke this macro to determine the storage order for each primitive */
/* type. Avoid using long double constants unless they are supported by the */
/* compiler. */
if ( _ok( *status ) )
{
_order( _CHAR, 'A', 'B', dat_gl_ndr[ DAT__C ].order )
_order( _LOGICAL, 0, 1, dat_gl_ndr[ DAT__L ].order )
_order( _BYTE, 0, 1, dat_gl_ndr[ DAT__B ].order )
_order( _UBYTE, 0, 1, dat_gl_ndr[ DAT__UB ].order )
_order( _WORD, 0, 1, dat_gl_ndr[ DAT__W ].order )
_order( _UWORD, 0, 1, dat_gl_ndr[ DAT__UW ].order )
_order( _INTEGER, 0, 1, dat_gl_ndr[ DAT__I ].order )
_order( _INT64, 0, 1, dat_gl_ndr[ DAT__K ].order )
#if HAVE_LONG_DOUBLE
_order( _REAL, 1.0L, ( 1.0L + eps_REAL ),
dat_gl_ndr[ DAT__R ].order )
_order( _DOUBLE, 1.0L, ( 1.0L + eps_DOUBLE ),
dat_gl_ndr[ DAT__D ].order )
#else
_order( _REAL, 1.0, ( 1.0 + eps_REAL ),
dat_gl_ndr[ DAT__R ].order )
_order( _DOUBLE, 1.0, ( 1.0 + eps_DOUBLE ),
dat_gl_ndr[ DAT__D ].order )
#endif
}
/* Calculate the number of characters required to format each data type. */
/* ==================================================================== */
/* The _CHAR and _LOGICAL types have fixed lengths. */
if ( _ok( *status ) )
{
dat_gl_ndr[ DAT__C ].txtsize = 1;
dat_gl_ndr[ DAT__L ].txtsize = 5; /* To accommodate "FALSE" */
/* Set their number of decimal digits of precision to zero (not used). */
dat_gl_ndr[ DAT__C ].digits = 0;
dat_gl_ndr[ DAT__L ].digits = 0;
}
/* For the integer types, determine the number of characters required by */
/* formatting the minimum and maximum values and seeing how many characters */
/* are required. Use the maximum number of characters for the "txtsize" */
/* value and the number of characters required for the maximum value for */
/* the "digits" value. Define a macro to do this: */
#define _txtsizei( dtype, min, max, txtsize, digits )\
{\
char buf[ BUFSIZE + 1 ];/* Temporary buffer for formatting */\
int len1; /* No. characters for minimum */\
int len2; /* No. characters for maximum */\
\
/* If the values are signed, cast to 64-bit int type. */\
if( _signed( dtype ) )\
{\
len1 = snprintf( buf, sizeof(buf), "%-"INT_BIG_S, (INT_BIG) (min) ); \
len2 = snprintf( buf, sizeof(buf), "%-"INT_BIG_S, (INT_BIG) (max) ); \
}\
\
/* If the values are unsigned, cast to unsigned long int */\
else\
{\
len1 = snprintf( buf, sizeof(buf), "%-"INT_BIG_U, (UINT_BIG) (min) ); \
len2 = snprintf( buf, sizeof(buf), "%-"INT_BIG_U, (UINT_BIG) (max) ); \
}\
\
/* Assign the results. */\
(txtsize) = ( len1 > len2 ) ? len1 : len2;\
(digits) = len2;\
}
/* Invoke this macro to determine the number of characters required to */
/* format all the integer primitive types. */
if ( _ok( *status ) )
{
_txtsizei( _BYTE,
dat_gl_ndr[ DAT__B ].min.B,
dat_gl_ndr[ DAT__B ].max.B,
dat_gl_ndr[ DAT__B ].txtsize,
dat_gl_ndr[ DAT__B ].digits )
_txtsizei( _UBYTE,
dat_gl_ndr[ DAT__UB ].min.UB,
dat_gl_ndr[ DAT__UB ].max.UB,
dat_gl_ndr[ DAT__UB ].txtsize,
dat_gl_ndr[ DAT__UB ].digits )
_txtsizei( _WORD,
dat_gl_ndr[ DAT__W ].min.W,
dat_gl_ndr[ DAT__W ].max.W,
dat_gl_ndr[ DAT__W ].txtsize,
dat_gl_ndr[ DAT__W ].digits )
_txtsizei( _UWORD,
dat_gl_ndr[ DAT__UW ].min.UW,
dat_gl_ndr[ DAT__UW ].max.UW,
dat_gl_ndr[ DAT__UW ].txtsize,
dat_gl_ndr[ DAT__UW ].digits )
_txtsizei( _INTEGER,
dat_gl_ndr[ DAT__I ].min.I,
dat_gl_ndr[ DAT__I ].max.I,
dat_gl_ndr[ DAT__I ].txtsize,
dat_gl_ndr[ DAT__I ].digits )
_txtsizei( _INT64,
dat_gl_ndr[ DAT__K ].min.K,
dat_gl_ndr[ DAT__K ].max.K,
dat_gl_ndr[ DAT__K ].txtsize,
dat_gl_ndr[ DAT__K ].digits )
}
/* Define a macro to format the minimum floating point values using a */
/* specified number of decimal digits of precision to determine how many */
/* characters are required. */
#if HAVE_LONG_DOUBLE && ! __MINGW32__
#define _txtsizef( min, digits, txtsize )\
{\
char buf[ BUFSIZE + 1 ];/* Buffer for formatted value */\
int len; /* Number of characters in formatted value */\
\
/* Format the minimum value (cast to long double) using the appropriate */\
/* number of digits of precision and a minimum field width which is able to */\
/* accommodate the additional characters required (including one digit of */\
/* the exponent). Record the actual number of characters produced. This */\
/* will include additional exponent characters if needed. */\
len = snprintf( buf, sizeof(buf), "%*.*LE", \
(int) ( (digits) + 5 ), (int) ( (digits) - 1 ),\
(long double) (min) );\
\
/* Assign the result. */\
(txtsize) = len;\
}
#else
/* Don't use long double if it is not supported by the compiler. */
#define _txtsizef( min, digits, txtsize )\
{\
char buf[ BUFSIZE + 1 ]; /* Buffer for formatted value */\
int len; /* Number of characters in formatted value */\
len = snprintf( buf, sizeof(buf), "%*.*E", \
(int) ( (digits) + 5 ), (int) ( (digits) - 1 ),\
(double) (min) );\
(txtsize) = len;\
}
#endif
/* Assign the number of decimal digits as determined previously and invoke */
/* the macro above to determine the formatted size of the _REAL and _DOUBLE */
/* data types. */
if ( _ok( hds_gl_status ) )
{
dat_gl_ndr[ DAT__R ].digits = dig_REAL;
dat_gl_ndr[ DAT__D ].digits = dig_DOUBLE;
_txtsizef( dat_gl_ndr[ DAT__R ].min.R,
dat_gl_ndr[ DAT__R ].digits,
dat_gl_ndr[ DAT__R ].txtsize )
_txtsizef( dat_gl_ndr[ DAT__D ].min.D,
dat_gl_ndr[ DAT__D ].digits,
dat_gl_ndr[ DAT__D ].txtsize )
}
/* Determine the number format for each data type. */
/* ============================================== */
/* (Note we are only interested in distinguishing the currently supported */
/* types.) */
/* Identify ASCII character format by testing a few key characters. */
if ( _ok( *status ) )
{
if ( ( ( (_CHAR) 'A' ) == ( (_CHAR) 0x41 ) ) &&
( ( (_CHAR) '0' ) == ( (_CHAR) 0x30 ) ) )
{
dat_gl_ndr[ DAT__C ].format = DAT__ASCII;
}