/* ------------------------------------------------------------------ */ /* The 'telco' 0.53 benchmark in C (using decNumber) */ /* This is the version with 'full optimization' (internal format */ /* everywhere). See also cTelco754r1 and cTelco754r0. */ /* ------------------------------------------------------------------ */ /* Copyright (c) IBM Corporation, 2001, 2005. All rights reserved. */ /* ------------------------------------------------------------------ */ /* Call as: */ /* */ /* cTelco754r [infile [outfile]] [flags] */ /* */ /* where the parameters are: */ /* infile -- input file name [default telco.testr] */ /* outfile -- output file name [default telco.outc] */ /* flags -- may be before or after file names, and may be */ /* */ /* -nocalc -- omit all calculations for each number */ /* -notax -- omit tax calculations (and summations) */ /* */ /* See 'http://speleotrove.com/decimal/telco.html' for background */ /* and details. */ /* */ /* ------------------------------------------------------------------ */ /* Note that a non-ANSI timer function is used; be prepared to modify */ /* 2003.04.28 -- changed initialization constant for decNumber 3.x */ /* 2005.04.01 -- version for data in IEEE 754r decimal64 format */ #define INNUMLEN 8 // input number length in bytes #define MAXFILE 255 // maximum filename length #define DECNUMDIGITS 31 // maximum precision #include "decNumber.h" // base number library #include "decimal64.h" // decimal64 conversions #include #include #include #include // ----- Non-ANSI timer things; may need altering ------------------- #ifndef TOPTIMEB #include // timeb.h is in \include\sys #else #include // timeb.h is in \include #endif // ------------------------------------------------------------------ static void showus(char *, struct timeb, struct timeb, int); /* ------------------------------------------------------------------ */ /* main entry point */ /* ------------------------------------------------------------------ */ int main(int argc, char *argv[]) { decNumber sumT, sumB, sumD; // sums decNumber n; // number from file decNumber p; // price decNumber b; // base tax decNumber d; // distance tax decNumber t; // total price decNumber baserate, distrate; // call rates decNumber basetax, disttax; // tax rates decNumber mtwo; // constant -2 decContext set; // working context decimal64 in64; // we read into this char string[DECNUMDIGITS+14]; // conversion buffer int i, j, numbers; // counters FILE *inp=NULL; // input stream structure FILE *oup=NULL; // output stream structure char filein[MAXFILE+1]; // full input filename char fileou[MAXFILE+1]; // full output filename int readlen; // read data length int frc; // ferror return code int calltype; // call type (0 or 1) int calc=1; // 1 for calculations, 0 to skip int tax=1; // 1 for tax calculations, 0 to skip struct timeb tbStart, tbFinis; // time buffers unsigned int urc, len; // work char **parm; // .. // set up arithmetic context decContextDefault(&set, DEC_INIT_DECIMAL128); // initialize set.traps=0; // no traps set.digits=31; // working precision // set up the call rates, tax rates and other constants decNumberFromString(&baserate, "0.0013", &set); // low call rate decNumberFromString(&distrate, "0.00894", &set);// high call rate decNumberFromString(&basetax, "0.0675", &set); // base tax rate decNumberFromString(&disttax, "0.0341", &set); // distance tax rate decNumberFromString(&mtwo, "-2", &set); // scale setting // Get any parameters strcpy(filein, "telco.testr"); // default filenames strcpy(fileou, "telco.outc"); // .. j=0; // parameter number for (i=0, parm=argv; iMAXFILE) { // no buffer overruns, please printf("Parameter word too long (>%d characters): %s\n", MAXFILE, *parm); exit(1); // abandon } if (i==0) ; // 0. is program name else if (*parm[0]=='-') { // flag expected if (strcmp(*parm, "-nocalc")==0) calc=0; else if (strcmp(*parm, "-notax")==0) tax=0; else printf("Flag '%s' ignored\n", *parm); } else { j++; // have a parameter if (j==1) strcpy(filein, *parm); // is input file name else if (j==2) strcpy(fileou, *parm); // is output file name else printf("Extra parameter '%s' ignored\n", *parm); } } /* getting arguments */ printf("telco C benchmark; processing '%s'\n", filein); remove(fileou); // delete output file if it exists /* ---------------------------------------------------------------- */ /* Benchmark timing starts here */ /* ---------------------------------------------------------------- */ ftime(&tbStart); // timestamp decNumberZero(&sumT); // zero accumulators decNumberZero(&sumB); decNumberZero(&sumD); inp=fopen(filein,"rb"); // open file for reading, binary if (inp==NULL) { printf("Error: file '%s' not found\n", filein); exit(0); } oup=fopen(fileou,"wb"); // open file for writing, binary if (oup==NULL) { fclose(inp); printf("Error: file '%s' could not be opened\n", fileou); exit(0); } // From now on, files must be closed explicitly /* Start of the by-number loop */ for (numbers=0; ; numbers++) { // get next 8-byte decimal64 number readlen=fread(in64.bytes, 1, DECIMAL64_Bytes, inp); if (readlen