/* * Classified Tydeman Consulting Proprietary * Copyright (C) 2002, 2008 Tydeman Consulting. All rights reserved. * * Fred Tydeman tydeman@tybor.com * * Any person is hereby authorized to copy, use, and distribute, this * one sample test, subject to the following conditions: * * 1. the software may not be redistributed for a fee except as * reasonable to cover media costs; * 2. any copy of the software must include this notice, as well as * any other embedded copyright notices; and * 3. any distribution of this software or derivative works thereof * must comply with all applicable U.S. export control laws. * * Floating-Point C Extensions (FPCE) have been added to C99 (the * revision of the C language finished in late 1999). If you are * interested in other sample tests of the FPCE Test Suite, which * tests the floating-point and numerics capabilities of C/C++ * compilers and libraries, please see the FTP site: * ftp://ftp.tybor.com * user ID: anonymous * password: your email address * *********************************************************************** * FILE: tint2flt.c * * Assertion being tested: * * C99: * 6.3.1.4 Real floating and integer * When a value of integer type is converted to a real floating type, * if the value being converted can be represented exactly in the new * type, it is unchanged. * * This test is for C99 with IEEE-754 floating-point support. * Test conversions from integer types to floating-point types. * For all integer types, test with powers of 2. * For signed integer types, also test with negative of powers of 2. * Checks for same value and no exceptions. * * Results of running this test on various compilers: * (as reported by several users of this test; your results may vary) * * Failures Hardware Operating system Compiler, version, and options * 0 Apple Mac PPC G4 Mac OS X 10.3.7 Gnu gcc 3.3 (build 1495) * 0 Apple Mac PPC G4 Mac OS X 10.3.7 IBM XL C 6.0 + Gnu gcc 3.3 * 0 Apple PowerPC G3 Linux 2.4.19 Gnu gcc 3.2.2 w/ glibc ?; no long long * 0 Apple PowerPC G4 7450 MacOS 10.2.5 Gnu gcc 3.3.2 w/ glibc ? * 0 DEC Alpha Linux 2.4.18 Gnu gcc 3.3.1; no long long * 0 DEC Alpha 21064 Linux 2.2.22 Gnu gcc 3.3 * 0 HP PA RISC rp5470 Linux 3.0 cc 3.0.4; no long long * 0 HP ia64 Itanium II HP UX 11 HP cc; no long long * 0 Intel Pentium 4 OS/2 eCS 1.1 Open Watcom C/C++ v1.3 * 0 Intel Pentium 4 OS/2 eCS 1.1 Open Watcom C/C++ v1.4 * 0 Intel Pentium 4 OS/2 eCS 1.1 Open Watcom C/C++ v1.5 * 0 Intel Pentium 4 SuSE Linux 9.1 Gnu gcc 3.3.3-41 + glibc 2.3.3-97 * 0 Intel Pentium 4 Windows XP Digital Mars 8.39 * 0 Intel Pentium 4 Windows XP Digital Mars 8.47 * 0 Intel Pentium 4 Windows XP Digital Mars 8.48 * 0 Intel Pentium 4 Windows XP Digital Mars 8.50 * 0 Intel Pentium 4 Windows XP LCC 2003/01/12 * 0 Intel Pentium 4 Windows XP LCC 2004/07/26 * 0 Intel Pentium 4 Windows XP LCC 2006/04/10 * 0 Intel Pentium 4 Windows XP Metrowerks CodeWarrior Pro 8.3 * 0 Intel Pentium 4 Windows XP Metrowerks CodeWarrior Pro 9.2 * 0 Intel Pentium III OS/2 v4 Gnu gcc 2.5.7 + emx 0.8h; no long long * 0 Intel Pentium III OS/2 v4 Gnu gcc 2.7.2 + emx 0.9b + fix05 * 0 Intel Pentium III OS/2 v4 Gnu gcc 2.7.2.1 + emx 0.9c + fix02 * 0 Intel Pentium III OS/2 v4 Gnu gcc 2.8.1 + emx 0.9d * 0 Intel Pentium III OS/2 v4+DOS Borland C/C++ v4 * 0 Intel Pentium III RedHat Linux 7 Gnu gcc 2.96 + glibc 2.1.92 * 0 Intel Pentium III RedHat Linux 7 Intel C/C++ icc 7 w/ Gnu gcc 2.96 + glibc 2.1.92 * 0 Intel Pentium III RedHat Linux 8 Comeau 4.3.0.1 w/ Gnu gcc 3.2 + glibc 2.2.93 * 0 Intel Pentium III RedHat Linux 8 Gnu gcc 3.2 + glibc 2.2.93 * 0 Intel Pentium III RedHat Linux 8 Gnu gcc 3.3.1 + glibc 2.3.2-4.80.6 * 0 Intel Pentium III RedHat Linux 8 Intel C/C++ icc 7 w/ Gnu gcc 3.2 + glibc 2.2.93 * 0 Intel Pentium III RedHat Linux 8 Intel C/C++ icc 7.1 w/ Gnu gcc 3.3 + glibc 2.3.2-4.80.6 * 0 Intel Pentium III Windows 98 Digital Mars 8.35.6 * 0 Intel Pentium III Windows 98 LCC 2002/02/10; no ULLONG * 0 Intel Pentium III Windows 98 LCC 2002/06/06; no ULLONG * 0 Intel Pentium Pro RedHat Linux 4 Gnu gcc 2.7.2.1 + libc 5.3.12 + libg++ 2.7.1.4 * 0 Intel Pentium Pro Windows NT IBM VisualAge for C/C++ 3.5.7 * 0 Intel Pentium Pro Windows NT Intel C/C++ 2.4 * 0 Intel x86 Solaris 9 Sun ONE Studio 8, Sun C 5.5 * 0 Intel/ARM XScale RISC OS 5 Norcroft C 5.53 * 0 Sun SPARC Solaris 9 Sun ONE Studio 8, Sun C 5.5 * 1 Intel Pentium 4 Windows XP Microsoft C/C++ V 12.00 aka Visual Studio 6 [semi-broken preprocessor] * 1 Intel Pentium 4 Windows XP Microsoft C/C++ V 14.00 aka Visual Studio 8 [semi-broken preprocessor] * 2 Intel Pentium III Windows 98 LCC 2003/01/12 * 3 Intel Pentium III Windows 98 LCC 2002/06/09 * 3 Intel Pentium III Windows 98 LCC 2002/11/30 * 54 Intel Pentium Pro Windows NT Metrowerks CodeWarrior Pro 4 * 108 Intel Pentium Pro Windows NT Metrowerks CodeWarrior Pro 5 * 108 Intel Pentium Pro Windows NT Metrowerks CodeWarrior Pro 6 * ?????? DEC/Compaq/HP OSF/1 4.0 Missing * ?????? DEC/Compaq/HP OSF/1 5.1 (aka Tru64 Unix) Missing * ?????? DEC/Compaq/HP Alpha OpenVMS 7.3 Missing * ?????? IBM RS/6000 AIX 4.2 Missing * ?????? Intel Pentium III OS/2 v4+DOS Borland Turbo C v2 [dies in preprocessor] * ?????? Intel Pentium III OS/2 v4+DOS Symantec C/C++ v7.01 [dies in preprocessor] * ?????? Intel Pentium III Windows 98 Digital Mars 8.16 [dies in preprocessor] * ?????? Intel Pentium III Windows 98 Digital Mars 8.19 [dies in preprocessor] * ?????? Intel Pentium III Windows 98 Digital Mars 8.20 [dies in preprocessor] * ?????? Intel Pentium III Windows 98 Microsoft C/C++ V 12.00 aka Visual Studio 6 [dies in preprocessor] * ?????? Intel Pentium III Windows NT Borland C/C++ v5 [dies in preprocessor] * ?????? Intel Pentium Pro Windows NT Microsoft C/C++ V 10.10.6038 [dies in preprocessor] * ?????? Intel Pentium Pro Windows NT Watcom C/C++ 10.5 [dies in preprocessor] * ?????? Intel x86 FreeBSD 4.4 Missing * ?????? Intel x86 FreeBSD 5.0 Missing * ?????? Intel x86 NetBSD 1.6 Missing * ?????? Intel x86 OpenBSD 3.2 Missing * ?????? SGI IRIX 6.5 Missing * * I wish to acknowledge the following people for taking the time and effort * to run this test and sending me their results: * Nelson H. F. Beebe, Center for Scientific Computing, University of Utah * Zhishun Alex Liu, Sun * * MODIFICATION HISTORY: * * 2002/06/27 - Create as an easier to use version of t0120400. * 2002/07/04 - Use SCHAR_MAX in place of CHAR_MAX. * 2003/02/07 - Add ability to tailor via external file (TAILOR). * 2003/02/22 - Add DISALLOW_IMPLICIT_FLUSH. * 2003/07/10 - Add DISALLOW_INVALID_OCTAL. * 2003/07/16 - Add DISALLOW_SEMIBROKEN_PP. * 2003/08/21 - Add FENV_ACCESS pragma. * 2003/08/21 - Add NEED_TRAPS_DISABLED and FPU_DISABLE_TRAPS. * 2004/04/23 - Add uses of CLEAR_ALL and RAISE_ALL. * 2004/07/30 - Update DEBUG section. * 2005/02/05 - Add DISALLOW_SYM_FE_*_IS_ENUM. * 2005/02/05 - Indent #if's. * 2006/07/23 - Mods due to Lint 8. * 2006/07/25 - Update DEBUG section. * 2006/07/25 - Indent #if's. * 2007/11/21 - Add const as per PC-Lint 8. * ***********************************************************************/ /*lint -e506 // constant value Boolean */ /*lint -e537 // repeated include */ /*lint -e737 // loss of sign on promotion */ /*lint -e766 // header not needed */ /*lint -e774 // constant value in if */ /*lint -e792 // void cast of void expression */ /*lint -e801 // use of goto is deprecated */ /*lint -e909 // implicit conversion */ /*lint -e912 // implicit conversion */ /* * If you need to disable trapping of floating-point exceptions, * you might try one of the next two ways: * #define FPU_DISABLE_TRAPS (void)feholdexcept( ¤t_fenv ); * #define FPU_DISABLE_TRAPS (void)fedisabletrap(FE_TRAP_ALL); * in tflt2int.h. You will also need to define NEED_TRAPS_DISABLED. */ #ifdef TAILOR /* A way to tailor via external file */ #include "tint2flt.h" /* A place to define DISALLOW_BROKEN_PP, ... */ #endif #ifdef DEBUG /* local override of DISALLOWs to force errors to show */ #ifdef DEBUG2 /* Kills some compilers */ #undef DISALLOW_BROKEN_PP #undef DISALLOW_NAMESPACE_STD #undef DISALLOW_SEMIBROKEN_PP #endif #ifdef DEBUG3 #undef CLEAR_ALL #undef NEED_TRAPS_DISABLED #endif #undef DISALLOW_FP_FROM_ULLI #undef DISALLOW_IMPLICIT_FLUSH #undef DISALLOW_INVALID_OCTAL #undef DISALLOW_PRAGMAS #undef DISALLOW_PRAGMA_FENV_ACCESS #undef DISALLOW_SYM_FE_DIVBYZERO_IS_ENUM #undef DISALLOW_SYM_FE_INEXACT_IS_ENUM #undef DISALLOW_SYM_FE_INVALID_IS_ENUM #undef DISALLOW_SYM_FE_OVERFLOW_IS_ENUM #undef DISALLOW_SYM_FE_UNDERFLOW_IS_ENUM #endif /* * Tell implementation that we will be checking the floating-point status flags. */ #if (!defined DISALLOW_PRAGMAS) && (!defined DISALLOW_PRAGMA_FENV_ACCESS) #pragma STDC FENV_ACCESS ON #endif #ifdef FENV_H #include FENV_H #else #include /* fe clear/raise/test except(), FE_ALL_EXCEPT */ #endif #include /* *_MAX_EXP */ #include /* *_MAX */ #include /* printf() */ /* * Clear all FP exception flags. */ #ifndef CLEAR_ALL #define CLEAR_ALL (void)feclearexcept( FE_ALL_EXCEPT ) #endif #ifndef DISALLOW_NAMESPACE_STD #ifdef __cplusplus /* For C++ to call C functions */ using namespace std; /* Add implicit std:: to each function call */ #endif #endif #define LD long double, LDBL_MAX_EXP, "%#.0Lf" #define D double, DBL_MAX_EXP, "%#.0f" #define F float, FLT_MAX_EXP, "%#.0f" #ifdef ULLONG_MAX #ifdef ULONGLONG #define ULL ULONGLONG, "%#llx", ULLONG_MAX #else #define ULL unsigned long long, "%#llx", ULLONG_MAX #endif #endif #ifdef LLONG_MAX #ifdef LONGLONG #define SLL signed LONGLONG, "%#llx", LLONG_MAX #else #define SLL signed long long, "%#llx", LLONG_MAX #endif #endif #define UL unsigned long, "%#lx", ULONG_MAX #define SL signed long, "%#lx", LONG_MAX #define UI unsigned int, "%#x", UINT_MAX #define SI signed int, "%#x", INT_MAX #define US unsigned short, "%#hx", USHRT_MAX #define SS signed short, "%#hx", SHRT_MAX #define UC unsigned char, "%#x", UCHAR_MAX #define SC signed char, "%#x", SCHAR_MAX static unsigned long log2radix; static int failed = 0; #define STR(s) # s #define STN(n) tst_ ## n /* * 'Type generic' function done via macro. * i = 2**j and is the value to convert. * First, test for expected value and no flags being raised. * Second, for signed types, test from negative. */ #define DEF_TSTS(N,I_TYPE,I_FMT,I_MAX,F_TYPE,F_MAX_EXP,F_FMT)\ static void STN(N)(void){\ F_TYPE f_exp = (F_TYPE)1.0;\ F_TYPE f;\ I_TYPE i = (I_TYPE)1;\ I_TYPE imax = (I_TYPE)I_MAX;\ unsigned long j;\ unsigned long const max_exp = log2radix * (unsigned long)F_MAX_EXP;\ int flags;\ int lcl_failed=0;\ (void)printf("Testing conversion from " STR(I_TYPE) " to " STR(F_TYPE) "\n");\ for(j=0uL; imax && j>= 1; /* next smaller power of 2 */\ /*lint +e915 +e704 +e703 +e702 +e701 */\ if(9<=lcl_failed){\ (void)printf("Too many failures\n");\ break;\ }\ }\ failed += lcl_failed;\ } /* * Same tests as above, but for unsigned types (no test of -i) */ #define DEF_TSTU(N,I_TYPE,I_FMT,I_MAX,F_TYPE,F_MAX_EXP,F_FMT)\ static void STN(N)(void){\ F_TYPE f_exp = (F_TYPE)1.0;\ F_TYPE f;\ I_TYPE i = (I_TYPE)1;\ I_TYPE imax = (I_TYPE)I_MAX;\ unsigned long j;\ unsigned long const max_exp = log2radix * (unsigned long)F_MAX_EXP;\ int flags;\ int lcl_failed=0;\ (void)printf("Testing conversion from " STR(I_TYPE) " to " STR(F_TYPE) "\n");\ for(j=0uL; imax && j>= 1; /* next smaller power of 2 */\ /*lint +e915 +e704 +e702 */\ if(9<=lcl_failed){\ (void)printf("Too many failures\n");\ break;\ }\ }\ failed += lcl_failed;\ } #if (!defined DISALLOW_BROKEN_PP) /* many pre-processors die on these macros */ /* * Used to convert 3 arguments (which contain embedded ',' chars) * into 7 arguments. */ #if (!defined DISALLOW_SEMIBROKEN_PP) /* many pre-processors die on these macros */ #define DEF_TESTU(N,I,F) DEF_TSTU(N,I,F) #define DEF_TESTS(N,I,F) DEF_TSTS(N,I,F) #else /* a workaround for some(?) compilers */ #define DEF_TESTU(N,I,F) DEF_TEST_UI((N,I,F)) #define DEF_TEST_UI(args) DEF_TSTU args #define DEF_TESTS(N,I,F) DEF_TEST_SI((N,I,F)) #define DEF_TEST_SI(args) DEF_TSTS args #endif #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) DEF_TESTU(00,ULL,LD) #endif #ifdef LLONG_MAX DEF_TESTS(01,SLL,LD) #endif DEF_TESTU(02, UL,LD) DEF_TESTS(03, SL,LD) DEF_TESTU(04, UI,LD) DEF_TESTS(05, SI,LD) DEF_TESTU(06, US,LD) DEF_TESTS(07, SS,LD) #ifdef DISALLOW_INVALID_OCTAL DEF_TESTU(108, UC,LD) DEF_TESTS(109, SC,LD) #else DEF_TESTU(08, UC,LD) DEF_TESTS(09, SC,LD) #endif #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) DEF_TESTU(10,ULL, D) #endif #ifdef LLONG_MAX DEF_TESTS(11,SLL, D) #endif DEF_TESTU(12, UL, D) DEF_TESTS(13, SL, D) DEF_TESTU(14, UI, D) DEF_TESTS(15, SI, D) DEF_TESTU(16, US, D) DEF_TESTS(17, SS, D) DEF_TESTU(18, UC, D) DEF_TESTS(19, SC, D) #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) DEF_TESTU(20,ULL, F) #endif #ifdef LLONG_MAX DEF_TESTS(21,SLL, F) #endif DEF_TESTU(22, UL, F) DEF_TESTS(23, SL, F) DEF_TESTU(24, UI, F) DEF_TESTS(25, SI, F) DEF_TESTU(26, US, F) DEF_TESTS(27, SS, F) DEF_TESTU(28, UC, F) DEF_TESTS(29, SC, F) #endif /* DISALLOW_BROKEN_PP */ int main(void){ (void)printf("This program tests conversions from integer types to floating-point types.\n"); (void)printf("For all types, it test positive powers of 2, e.g., 2**j\n"); (void)printf("For signed types, it also tests negative of positive powers of 2, e.g., -(2**j)\n"); (void)printf("For numbers that are representable, it checks expected value and no exceptions.\n"); #ifndef __STDC_IEC_559__ (void)printf("This implementation does not claim conformance to IEC-60559 or IEEE-754\n"); (void)printf("Checks of the floating-point exception flags may be meaningless.\n"); #endif switch (FLT_RADIX){ case 2: log2radix = 1uL; break; case 4: log2radix = 2uL; break; case 8: log2radix = 3uL; break; case 16: log2radix = 4uL; break; default: (void)printf("Failed. Unsupported FLT_RADIX value: %i\n", (int)FLT_RADIX); goto death; } #if (defined DISALLOW_BROKEN_PP) failed++; (void)printf("Failed due to broken PreProcessor.\n"); #else #if (defined DISALLOW_SEMIBROKEN_PP) failed++; (void)printf("Failed due to semibroken PreProcessor.\n"); #endif #if (defined NEED_TRAPS_DISABLED) failed++; (void)printf("Failed due to traps enabled.\n"); { fenv_t current_fenv; FPU_DISABLE_TRAPS } #endif #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) tst_00(); /* tests from long double */ #endif #ifdef LLONG_MAX tst_01(); #endif tst_02(); tst_03(); tst_04(); tst_05(); tst_06(); tst_07(); #ifdef DISALLOW_INVALID_OCTAL tst_108(); tst_109(); #else tst_08(); tst_09(); #endif #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) tst_10(); /* tests from double */ #endif #ifdef LLONG_MAX tst_11(); #endif tst_12(); tst_13(); tst_14(); tst_15(); tst_16(); tst_17(); tst_18(); tst_19(); #if (defined ULLONG_MAX) && (!defined DISALLOW_FP_FROM_ULLI) tst_20(); /* tests from float */ #endif #ifdef LLONG_MAX tst_21(); #endif tst_22(); tst_23(); tst_24(); tst_25(); tst_26(); tst_27(); tst_28(); tst_29(); if(failed){ (void)printf("To help you figure out which flags were raised:\n"); #if (defined FE_INVALID) || (defined DISALLOW_SYM_FE_INVALID_IS_ENUM) (void)printf(" FE_INVALID = %#x\n", (unsigned)FE_INVALID ); #endif #if (defined FE_DIVBYZERO) || (defined DISALLOW_SYM_FE_DIVBYZERO_IS_ENUM) (void)printf(" FE_DIVBYZERO = %#x\n", (unsigned)FE_DIVBYZERO); #endif #if (defined FE_OVERFLOW) || (defined DISALLOW_SYM_FE_OVERFLOW_IS_ENUM) (void)printf(" FE_OVERFLOW = %#x\n", (unsigned)FE_OVERFLOW ); #endif #if (defined FE_UNDERFLOW) || (defined DISALLOW_SYM_FE_UNDERFLOW_IS_ENUM) (void)printf(" FE_UNDERFLOW = %#x\n", (unsigned)FE_UNDERFLOW); #endif #if (defined FE_INEXACT) || (defined DISALLOW_SYM_FE_INEXACT_IS_ENUM) (void)printf(" FE_INEXACT = %#x\n", (unsigned)FE_INEXACT ); #endif } #endif /* DISALLOW_BROKEN_PP */ death:; if(failed){ (void)printf("Failed with at least %i failures\n", failed); }else{ (void)printf("Passed\n"); } #ifdef DISALLOW_IMPLICIT_FLUSH (void)fflush(stdout); /* need explicit flush */ #endif #if (defined RETVAL) && (defined STANDALONE_TEST) return RETVAL; /* Being run as part of test suite */ #else return 0; #endif }