The DFPAL Library, version 2.20 © Copyright IBM Corporation, 2007. All rights reserved. |
|
[previous | contents | next] | [printer friendly] |
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 }
dfpal.h
.
decimal64
and decimal128
names to avoid name collision with compiler native decimal floating point data type names.
dfpalInit()
. This example uses memory allocated using malloc()
facility. Application can use its own memory allocation routine.
dfpalEnd()
to cleanup and free memory.
decimal64
format.
decimal64
data type, print result using dec64ToString()
.
decimal64
values to decimal128
type (type conversion).
decimal128
data types, print result using dec128ToString()
.
dfpalEnd()
to cleanup and free memory.
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 }
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. |