Occasionally, it may be necessary to convert data passed into a generic routine in a generic variable into another data type. A general set of conversion functions was originally provided within GENERIC to make this possible. However, new code requiring such conversions should invoke the PRIMDAT library of functions. PRIMDAT also provides many other instrinsic functions that are amenable to inclusion in generic code. This section remains only to interpret legacy code that still calls DCV routines.
A naming convention is used so that the function DCV_<T1>TO<T2> converts data type <T1> into <T2>.
The functions which can be are defined using Fortran statement functions in a file which may be included in a program by
which should come after any data type definitions, but before any DATA initialisation or executable code. In the standard prologue of SGP/16, it should go in the “Internal References” section.
Conversions which cannot be defined as statement functions are provided as external functions in an
object library (/star/lib/dcv.a on a Starlink system). Programs requiring these functions should be
compiled
on Unix.
The types of these functions can be declared in a program by including
(in the “External References” section of the standard prologue).
NOTE: Statement functions are considerably faster than external functions, so programs which use the common conversions defined in ‘DCV_FUN’ will execute a lot faster.
Only numeric conversions are supported at present, and there are no DCV_ functions for conversions involving CHARACTER and LOGICAL data types. A full list of the functions, the conversion performed, and their location and restrictions, is given in Appendix B.
As an example, a generic subroutine for finding the mean of the data contained in an array, where the array is of varying type but the mean is always REAL, might look like the following routine.
In the above example, for the INTEGER version of the routine for instance, the DCV_ITOR( SUM )
statement will be converted to REAL( SUM ) by the statement function in DCV_FUN.
The DCV_<T1>TO<T2> functions will not check the range of the values given to them, and overflow errors are possible from some conversions (e.g. INTEGER to BYTE). It is recommended that an application check the range of any value given to a DCV function, to ensure it is within the allowed range. Global parameters of the form DCV__<T>MIN and DCV__<T>MAX are available for this purpose, which give the allowed range for type <T> (e.g. to for a BYTE). These by may included in a program using the following line.
(in the “Global Parameters” section of the standard prologue).
Here is an example of the use of these parameters (for instance converting an INTEGER to another type).
Note that ranges are only provided for numeric data types.
An alternative (and easier) way of preventing numerical overflow from causing problems is to use the PRIMDAT routines instead of the DCV ones. The PRIMDAT routines will not crash your program if an overflow occurs. See SUN/39 for details.