The DFPAL Library, version 2.20
© Copyright IBM Corporation, 2007. All rights reserved.
[previous | contents | next] [printer friendly]

DFPAL examples

Simple addition

      1 #include <stdio.h>          // printf() etc.
      2 #include <stdlib.h>         // malloc()/free()
      3 
      4 #include "dfpal.h"          // DFPAL API
      5 
      6 int main(int argc, char **argv)
      7 {
      8 decimal64 n1, n2, n3;       // 64-bit work numbers
      9 decimal128 N1, N2, N3;      // 128-bit work numbers
     10 char dfpstr[64];            // buffer for 'to string' conversion
     11 int initErr, initOSErr;     // DFPAL initialization error place-holder
     12 char *errStr=NULL;          // ...
     13 
     14   if (argc < 3) {
     15     printf("Please supply two numbers to add.\n");
     16     return(1);
     17   }
     18 
     19   // initialize and check error; free memory if unsuccessful
     20   if (dfpalInit((void *)malloc(dfpalMemSize())) != DFPAL_ERR_NO_ERROR) {
     21     dfpalGetError(&initErr, &initOSErr, &errStr);
     22     fprintf(stderr, "DFPAL Init error number:%d, error: %s\n",
     23       initErr, errStr);
     24     dfpalEnd(free);
     25     return(1);
     26   }
     27 
     28   // what version?
     29   printf("Version of DFPAL: %s\n", dfpalVersion());
     30 
     31   // Is it running in software or hardware?
     32   if (dfpalGetExeMode()!=PPCHW)
     33     printf("DFPAL is operating in software\n");
     34   else
     35     printf("DFPAL is operating in hardware\n");
     36 
     37   // convert input strings to decimal64 format
     38   n1=dec64FromString(argv[1]);
     39   n2=dec64FromString(argv[2]);
     40 
     41   // add
     42   n3 = dec64Add(n1,n2);
     43   printf("n1 + n2 = %s\n", dec64ToString(n3, dfpstr));
     44 
     45   // convert decimal64 to decimal128
     46   N1=dec128FromDecimal64(n1); // N1=(decimal128)n1
     47   N2=dec128FromDecimal64(n2); // N2=(decimal128)n2
     48   N3 = dec128Add(N1,N2);
     49   printf("N1 + N2 = %s\n", dec128ToString(N3, dfpstr));
     50 
     51   dfpalEnd(free);  // cleanup; free memory used by DFPAL
     52
     53   return(0);
     54 }


Line #: 4
     Include required header file dfpal.h.

Line #: 8-9
     Declare decimal floating point datatype work variables to hold 64-bit and 128-bit decimal floating point numbers. Application is required to use decimal64 and decimal128 names to avoid name collision with compiler native decimal floating point data type names.

Line #: 20
     Initialize DFPAL using dfpalInit(). This example uses memory allocated using malloc() facility. Application can use its own memory allocation routine.

Line #: 21
     Retrieve some information about the error.

Line #: 22-23
     Print error.

Line #: 24
     Call dfpalEnd() to cleanup and free memory.

Line #: 29
     Check DFPAL version.

Line #: 32-35
     Find out whether DFPAL is operating in hardware or software.

Line #: 38-39
     Convert input string to decimal64 format.

Line #: 42-43
     Perform add operation on decimal64 data type, print result using dec64ToString().

Line #: 46-47
     Typecast decimal64 values to decimal128 type (type conversion).

Line #: 48-49
     Perform add operation on decimal128 data types, print result using dec128ToString().

Line #: 51
     Call dfpalEnd() to cleanup and free memory.




Decimal floating point exception status

      1 #include <stdio.h>          // printf() etc.
      2 #include <stdlib.h>         // malloc()/free()
      3 
      4 #include "dfpal.h"          // DFPAL API
      5 
      6 #define PRINT_EXCEPTIONS(where, st) {                                      \
      7   printf(#where "\n");                                                     \
      8   printf("INVALID  OVERFLOW   UNDERFLOW  DIV_BY_ZERO  INEXACT\n");         \
      9   printf("   %1d        %1d           %1d           %1d          %1d\n",   \
     10     ((st)&DFPAL_FP_INVALID)?1:0, ((st)&DFPAL_FP_OVERFLOW)?1:0,             \
     11     ((st)&DFPAL_FP_UNDERFLOW)?1:0, ((st)&DFPAL_FP_DIV_BY_ZERO)?1:0,        \
     12     ((st)&DFPAL_FP_INEXACT)?1:0);                                          \
     13 }
     14 
     15 
     16 int main(int argc, char **argv)
     17 {
     18 decimal64 n1, n2, n3, n4;   // 64-bit work numbers
     19 decimal128 N1, N2, N3, N4;  // 128-bit work numbers
     20 char dfpstr[64];            // buffer for 'to string' conversion
     21 int initErr, initOSErr;     // DFPAL initialization error place-holder
     22 char *errStr=NULL;          // ...
     23 dfpalflag_t xstatus;
     24 
     25   // initialize and check error; free memory if unsuccessful
     26   if (dfpalInit((void *)malloc(dfpalMemSize())) != DFPAL_ERR_NO_ERROR) {
     27     dfpalGetError(&initErr, &initOSErr, &errStr);
     28     fprintf(stderr, "DFPAL Init error number:%d, error: %s\n",
     29       initErr, errStr);
     30     dfpalEnd(free);
     31     return(1);
     32   }
     33 
     34   dfpalClearStatusFlag(DFPAL_FP_ALL);
     35 
     36   // create some number to play with
     37   n1=dec64FromString("9E+200");
     38   n2=dec64FromString("9E-200");
     39   N1=dec128FromString("9E+3200");
     40   N2=dec128FromString("9E-3200");
     41   // did anything go wrong in string conversion?
     42   if ((xstatus=dfpalReadStatusFlag()) & DFPAL_FP_ALL)
     43     PRINT_EXCEPTIONS(String Conversion, xstatus);
     44 
     45   // multiplication: this should cause overflow and inexact
     46   n3=dec64Multiply(n1,n1);
     47   printf("n1 * n2 = %s\n", dec64ToString(n3, dfpstr));
     48   // overflow and inexact should be present
     49   if ((xstatus=dfpalReadStatusFlag()) & DFPAL_FP_ALL)
     50     PRINT_EXCEPTIONS(decimal64 multiply, xstatus);
     51 
     52   // division: this should cause divide by zero
     53   N3=dec128Divide(N1, dec128Zero());
     54   printf("N1 / 0 = %s\n", dec128ToString(N3, dfpstr));
     55   // overflow, inexact, divide by zero should be present
     56   if ((xstatus=dfpalReadStatusFlag()) & DFPAL_FP_ALL)
     57     PRINT_EXCEPTIONS(decimal64 multiply, xstatus);
     58 
     59   // division: this should cause underflow and inexact
     60   N3=dec128Divide(N2,N1);
     61   printf("N1 / N2 = %s\n", dec128ToString(N3, dfpstr));
     62   // overflow, inexact, underflow, divide by zero should be present
     63   if ((xstatus=dfpalReadStatusFlag()) & DFPAL_FP_ALL)
     64     PRINT_EXCEPTIONS(decimal128 divide, xstatus);
     65 
     66   // divide Inf/Inf: should cause invalid
     67   n3=dec64Divide(n3, n3);
     68   printf("Inf / Inf = %s\n", dec64ToString(n3, dfpstr));
     69   // overflow, inexact, underflow, divide by zero, invalid  should be present
     70   if ((xstatus=dfpalReadStatusFlag()) & DFPAL_FP_ALL)
     71     PRINT_EXCEPTIONS(decimal64 divide, xstatus);
     72 
     73   dfpalEnd(free);  // cleanup; free memory used by DFPAL
     74
     75   return(0);
     76 }


Line #: 6-13
     Define helper macro to print floating point exception. In reality, application typically takes some corrective action when floating point exception(s) are encountered.

Line #: 34
     Clear all floating point exception status at beginning.

Line #: 37-41
     Create some numbers from strings.

Line #: 42-43
     Check if string conversion caused any floating point exception. There should not be any.

Line #: 46-50
     Multiply operation to induce INEXACT and OVERFLOW floating point exceptions. Retrieve and print floating point exception flags.

Line #: 53-57
     Divide operation to induce DIVIDE_BY_ZERO floating point exception. Retrieve and print floating point exception flags. At this time, all three floating point exceptions occurred so far should appear in the output.

Line #: 60-64
     Divide operation to induce UNDERFLOW and INEXACT floating point exceptions. Retrieve and print floating point exception flags. At this time, all four floating point exceptions occurred so far should appear in the output.

Line #: 67-71
     Divide operation using Infinity value for both operands to induce INVALID floating point exception.
[
previous | contents | next]

DFPAL is authored by Punit Shah (punit@us.ibm.com).
Please send any corrections, comments or questions to dfpal-l@austin.ibm.com.
This page was updated on 21 Dec 2007.