C99#IEEE 754 floating-point support

{{short description|C programming language standard, 1999 revision}}

{{About|the programming language dialect}}

{{Use dmy dates|date=January 2021}}

{{missing information||_Pragma(), FP_CONTRACT, CX_LIMITED_RANGE|date=November 2020}}

{{C language revisions}}

Image:ISO-IEC-9899-1999-cover.png

C99 (previously C9X, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard.{{cite web|url=https://www.iso.org/iso/iso_catalogue/catalogue_ics/catalogue_detail_ics.htm?csnumber=29237 |title=ISO/IEC 9899:1999 - Programming languages - C |publisher=Iso.org |date=2011-12-08 |access-date=2014-04-08}} It extends the previous version (C90) with new features for the language and the standard library, and helps implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology.{{cite web |url=https://grouper.ieee.org/groups/754/meeting-materials/2001-07-18-c99.pdf |title=IEEE 754 Support in C99 |website=IEEE |archive-url=https://web.archive.org/web/20171028003206/http://grouper.ieee.org/groups/754/meeting-materials/2001-07-18-c99.pdf |archive-date=2017-10-28 |access-date=2021-07-15}} The C11 version of the C programming language standard, published in 2011, updates C99.

History

After ANSI produced the official standard for the C programming language in 1989, which became an international standard in 1990, the C language specification remained relatively static for some time, while C++ continued to evolve, largely during its own standardization effort. Normative Amendment 1 created a new standard for C in 1995, but only to correct some details of the 1989 standard and to add more extensive support for international character sets. The standard underwent further revision in the late 1990s, leading to the publication of ISO/IEC 9899:1999 in 1999, which was adopted as an ANSI standard in May 2000. The language defined by that version of the standard is commonly referred to as "C99". The international C standard is maintained by the working group ISO/IEC JTC1/SC22/WG14.

Design

thumb

C99 is, for the most part, backward compatible with C89, but it is stricter in some ways.{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Standards.html |title=Standards - Using the GNU Compiler Collection (GCC) |publisher=Gcc.gnu.org |access-date=2014-04-08}}

In particular, a declaration that lacks a type specifier no longer has int implicitly assumed. The C standards committee decided that it was of more value for compilers to diagnose inadvertent omission of the type specifier than to silently process legacy code that relied on implicit int. In practice, compilers are likely to display a warning, then assume int and continue translating the program.

C99 introduced several new features, many of which had already been implemented as extensions in several compilers:{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html |title=C Dialect Options - Using the GNU Compiler Collection (GCC) |publisher=Gcc.gnu.org |date=2009-05-06 |access-date=2014-04-08}}

  • inline functions
  • intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block)
  • several new data types, including long long int, optional extended integer types, an explicit Boolean data type, and a complex type to represent complex numbers
  • variable-length arrays (although subsequently relegated in C11 to a conditional feature that implementations are not required to support)
  • flexible array members
  • support for one-line comments beginning with //, as in BCPL, C++ and Java
  • new library functions, such as snprintf
  • new headers, such as <stdbool.h>, <complex.h>, <tgmath.h>, and <inttypes.h>
  • type-generic math (macro) functions, in , which select a math library function based upon float, double, or long double arguments, etc.
  • improved support for IEEE floating point
  • designated initializers. For example, initializing a structure by field names: struct point p = { .x = 1, .y = 2 };{{cite web |url=https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Designated-Inits.html |title=Using the GNU Compiler Collection (GCC): Designated Initializers |website=gnu.org |access-date=18 September 2019}}
  • compound literals. For instance, it is possible to construct structures in function calls: function((struct x) {1, 2}){{cite web |url=https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Compound-Literals.html |title=Using the GNU Compiler Collection (GCC): Compound Literals |website=gnu.org |access-date=31 January 2016}}
  • support for variadic macros (macros with a variable number of arguments)
  • restrict qualification allows more aggressive code optimization, removing compile-time array access advantages previously held by FORTRAN over ANSI C{{cite web |url=https://lwn.net/Articles/255364/ |title=What every programmer should know about memory |date=2007-10-23 |access-date=2015-04-03 |author=Ulrich Drepper |publisher=LWN.net}}
  • universal character names, which allows user variables to contain other characters than the standard character set: four-digit {{code|\u0040}} or eight-digit hexadecimal sequences {{code|\U0001f431}}
  • keyword static in array indices in parameter declarations{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 119, § 6.7.5.3 Function declarators (including prototypes) para. 7}}

Parts of the C99 standard are included in the current version of the C++ standard, including integer types, headers, and library functions. Variable-length arrays are not among these included parts because C++'s Standard Template Library already includes similar functionality.

IEEE&nbsp;754 floating-point support

A major feature of C99 is its numerics support, and in particular its support for access to the features of IEEE 754-1985 (also known as IEC 60559) floating-point hardware present in the vast majority of modern processors (defined in "Annex F IEC 60559 floating-point arithmetic"). Platforms without IEEE 754 hardware can also implement it in software.

On platforms with IEEE 754 floating point:

{{unordered list

|1= float is defined as IEEE 754 single precision, double is defined as double precision, and long double is defined as IEEE 754 extended precision (e.g., Intel 80-bit double extended precision on x86 or x86-64 platforms), or some form of quad precision where available; otherwise, it is double precision.

|2= The four arithmetic operations and square root are correctly rounded as defined by IEEE 754.

{{(!}} class="wikitable floatright" style="margin-left: 1.5em; font-family:monospace;"

{{!-}}

! FLT_EVAL_METHOD !! float !! double !! long double

{{!-}}

{{!}}0{{!!}}float{{!!}}double{{!!}}long double

{{!-}}

{{!}}1{{!!}}double{{!!}}double{{!!}}long double

{{!-}}

{{!}}2{{!!}}long double{{!!}}long double{{!!}}long double

{{!)}}

|3= Expression evaluation is defined to be performed in one of three well-defined methods, indicating whether floating-point variables are first promoted to a more precise format in expressions: FLT_EVAL_METHOD == 2 indicates that all internal intermediate computations are performed by default at high precision (long double) where available (e.g., 80 bit double extended), FLT_EVAL_METHOD == 1 performs all internal intermediate expressions in double precision (unless an operand is long double), while FLT_EVAL_METHOD == 0 specifies each operation is evaluated only at the precision of the widest operand of each operator. The intermediate result type for operands of a given precision are summarized in the adjacent table.}}

FLT_EVAL_METHOD == 2 tends to limit the risk of rounding errors affecting numerically unstable expressions (see IEEE 754 design rationale) and is the designed default method for x87 hardware, but yields unintuitive behavior for the unwary user;{{cite web|url=https://www.validlab.com/goldberg/addendum.html|title=Differences Among IEEE 754 Implementations|author=Doug Priest|year=1997}} FLT_EVAL_METHOD == 1 was the default evaluation method originally used in K&R C, which promoted all floats to double in expressions; and FLT_EVAL_METHOD == 0 is also commonly used and specifies a strict "evaluate to type" of the operands. (For gcc, FLT_EVAL_METHOD == 2 is the default on 32 bit x86, and FLT_EVAL_METHOD == 0 is the default on 64 bit x86-64, but FLT_EVAL_METHOD == 2 can be specified on x86-64 with option -mfpmath=387.) Before C99, compilers could round intermediate results inconsistently, especially when using x87 floating-point hardware, leading to compiler-specific behaviour;{{cite web|url=https://drdobbs.com/architecture-and-design/184410314 | title=A conversation with William Kahan. | author=Jack Woehr |date=1 November 1997}} such inconsistencies are not permitted in compilers conforming to C99 (annex F).

= Example =

The following annotated example C99 code for computing a continued fraction function demonstrates the main features:

  1. include
  2. include
  3. include
  4. include
  5. include
  6. include
  7. include

double compute_fn(double z) // [1]

{

#pragma STDC FENV_ACCESS ON // [2]

assert(FLT_EVAL_METHOD == 2); // [3]

if (isnan(z)) // [4]

puts("z is not a number");

if (isinf(z))

puts("z is infinite");

long double r = 7.0 - 3.0/(z - 2.0 - 1.0/(z - 7.0 + 10.0/(z - 2.0 - 2.0/(z - 3.0)))); // [5, 6]

feclearexcept(FE_DIVBYZERO); // [7]

bool raised = fetestexcept(FE_OVERFLOW); // [8]

if (raised)

puts("Unanticipated overflow.");

return r;

}

int main(void)

{

#ifndef __STDC_IEC_559__

puts("Warning: __STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported."); // [9]

#endif

#pragma STDC FENV_ACCESS ON

#ifdef TEST_NUMERIC_STABILITY_UP

fesetround(FE_UPWARD); // [10]

#elif TEST_NUMERIC_STABILITY_DOWN

fesetround(FE_DOWNWARD);

#endif

printf("%.7g\n", compute_fn(3.0));

printf("%.7g\n", compute_fn(NAN));

return 0;

}

Footnotes:

  1. Compile with: {{code|lang=bash|1=gcc -std=c99 -mfpmath=387 -o test_c99_fp test_c99_fp.c -lm}}
  2. As the IEEE 754 status flags are manipulated in this function, this #pragma is needed to avoid the compiler incorrectly rearranging such tests when optimising. (Pragmas are usually implementation-defined, but those prefixed with STDC are defined in the C standard.)
  3. C99 defines a limited number of expression evaluation methods: the current compilation mode can be checked to ensure it meets the assumptions the code was written under.
  4. The special values such as NaN and positive or negative infinity can be tested and set.
  5. long double is defined as IEEE 754 double extended or quad precision if available. Using higher precision than required for intermediate computations can minimize round-off error{{cite web |url=https://www.cs.berkeley.edu/~wkahan/ieee754status/baleful.pdf |title=The Baleful Effect of Computer Benchmarks upon Applied Mathematics, Physics and Chemistry| author=William Kahan |date=11 June 1996}} (the typedef double_t can be used for code that is portable under all FLT_EVAL_METHODs).
  6. The main function to be evaluated. Although it appears that some arguments to this continued fraction, e.g., 3.0, would lead to a divide-by-zero error, in fact the function is well-defined at 3.0 and division by 0 will simply return a +infinity that will then correctly lead to a finite result: IEEE 754 is defined not to trap on such exceptions by default and is designed so that they can very often be ignored, as in this case. (If FLT_EVAL_METHOD is defined as 2 then all internal computations including constants will be performed in long double precision; if FLT_EVAL_METHOD is defined as 0 then additional care is need to ensure this, including possibly additional casts and explicit specification of constants as long double.)
  7. As the raised divide-by-zero flag is not an error in this case, it can simply be dismissed to clear the flag for use by later code.
  8. In some cases, other exceptions may be regarded as an error, such as overflow (although it can in fact be shown that this cannot occur in this case).
  9. __STDC_IEC_559__ is to be defined only if "Annex F IEC 60559 floating-point arithmetic" is fully implemented by the compiler and the C library (users should be aware that this macro is sometimes defined while it should not be).
  10. The default rounding mode is round to nearest (with the even rounding rule in the halfway cases) for IEEE 754, but explicitly setting the rounding mode toward + and - infinity (by defining TEST_NUMERIC_STABILITY_UP etc. in this example, when debugging) can be used to diagnose numerical instability.{{cite web|url=https://www.cs.berkeley.edu/~wkahan/Mindless.pdf | title=How Futile are Mindless Assessments of Roundoff in Floating-Point Computation? | author=William Kahan |date=11 January 2006}} This method can be used even if compute_fn() is part of a separately compiled binary library. But depending on the function, numerical instabilities cannot always be detected.

Version detection

A standard macro __STDC_VERSION__ is defined with value 199901L to indicate that C99 support is available. As with the __STDC__ macro for C90, __STDC_VERSION__ can be used to write code that will compile differently for C90 and C99 compilers, as in this example that ensures that inline is available in either case (by replacing it with static in C90 to avoid linker errors).

  1. if __STDC_VERSION__ >= 199901L

/* "inline" is a keyword */

  1. else
  2. define inline static
  3. endif

Implementations

Most C compilers provide support for at least some of the features introduced in C99.

Historically, Microsoft has been slow to implement new C features in their Visual C++ tools, instead focusing mainly on supporting developments in the C++ standards.{{cite web |website=Ars Technica |url=https://arstechnica.com/information-technology/2013/06/c99-acknowledged-at-last-as-microsoft-lays-out-its-path-to-c14/ |title=C99 acknowledged at last as Microsoft lays out its path to C++14 |author=Peter Bright |date=29 June 2013 |access-date=9 January 2015}} However, with the introduction of Visual C++ 2013 Microsoft implemented a limited subset of C99, which was expanded in Visual C++ 2015.

class="wikitable sortable collapsible collapsed"
Compiler

! Level of support

! C99 compatibility details

Acorn C/C++

| {{partial}}

| The official documentation states that "most" compiler features are supported, along with "some" of the library functions.

AMD x86 Open64 Compiler Suite

| {{okay|Mostly}}

| Has C99 support equal to that of GCC.{{cite web |url=https://developer.amd.com/wordpress/media/2012/10/open64.pdf |title=Using the x86 Open64 Compiler Suite|publisher=Developer.amd.com|access-date=2 March 2022|archive-url=https://web.archive.org/web/20220124092542/https://developer.amd.com/wordpress/media/2012/10/open64.pdf|archive-date=24 January 2022|url-status=live}}

cc65

| {{partial}}

| Full C89 and C99 support is not implemented, partly due to platform limitations (MOS Technology 6502). There is no support planned for some C99 types like _Complex and 64-bit integers (long long).{{cite web|url=https://www.cc65.org/#Features|title=cc65 - a freeware C compiler for 6502 based systems|access-date=14 September 2011}}

Ch

| {{partial}}

| Supports major C99 features.{{cite web|url=https://www.softintegration.com/demos/chstandard/c99.html |title=C/C++ interpreter Ch C99 features |publisher=SoftIntegration, Inc. |date=15 February 2008 |access-date=15 February 2008}}

Clang

| {{okay|Mostly}}

| Supports all features except C99 floating-point pragmas.{{cite web|url=https://clang.llvm.org/docs/UsersManual.html#c |title=Clang Compiler User's Manual |access-date=14 October 2017}}

CompCert

| {{okay|Mostly}}

| A certified compiler, formally proved correct. Supports all features except C99 complex numbers and VLA, and minor restrictions on switch statements (no Duff's device).{{cite web|url=https://compcert.org/man/|title=The CompCert C verified compiler documentation and user's manual (Version 3.10)|date=19 November 2021|access-date=3 March 2022}}

cparser

| {{Yes|Full}}

| Supports C99 features.{{cite web|url=https://pp.info.uni-karlsruhe.de/firm/ |title=libfirm homepage |access-date=4 February 2014}}

C++ Builder

| {{okay|Only in 64-bit mode, since latter is CLang fork}}
{{Citation needed|date=September 2011}}

|

Digital Mars C/C++ Compiler

| {{partial}}

| Lacks support for some features, such as <tgmath.h> and _Pragma.{{cite web|url=https://www.digitalmars.com/ctg/C-Language-Implementation.html|title=C Language Implementation - Digital Mars|access-date=14 September 2011}}

GCC

| {{okay|Mostly}}

| {{As of|2021|07}}, standard pragmas and IEEE 754/IEC 60559 floating-point support are missing in mainline GCC. Additionally, some features (such as extended integer types and new library functions) must be provided by the C standard library and are out of scope for GCC.{{cite web|url=https://gcc.gnu.org/c99status.html |title=Status of C99 features in GCC |publisher=Free Software Foundation, Inc. |date=28 July 2021 |access-date=13 August 2021}} GCC's 4.6 and 4.7 releases also provide the same level of compliance.{{cite web|url=https://gcc.gnu.org/gcc-4.6/c99status.html |title=Status of C99 features in GCC 4.6|publisher=Free Software Foundation, Inc. |date=23 May 2013 |access-date=23 May 2013}}{{cite web|url=https://gcc.gnu.org/gcc-4.7/c99status.html |title=Status of C99 features in GCC 4.7|publisher=Free Software Foundation, Inc. |date=23 May 2013 |access-date=23 May 2013}} Partial IEEE 754 support, even when the hardware is compliant: some compiler options may be needed to avoid incorrect optimizations (e.g., -std=c99 and -fsignaling-nans), but full support of directed rounding modes is missing even when -frounding-math is used.{{cite web|url=https://gcc.gnu.org/wiki/FloatingPointMath|title=Semantics of Floating Point Math in GCC|date=20 July 2018|access-date=12 August 2018}}

Green Hills Software

| {{Yes|Full}}

|

IBM C for AIX, V6 {{cite web|url=https://www-01.ibm.com/common/ssi/cgi-bin/ssialias?infotype=an&subtype=ca&supplier=897&appname=IBMLinkRedirect&letternum=ENUS202-161|title=IBM C for AIX, V6.0 Now Supports the C99 Standard|date=2 July 2002 |access-date=31 January 2016}} and XL C/C++ V11.1 for AIX {{cite web|url=https://www-01.ibm.com/software/awdtools/xlcpp/aix/features/|title=IBM - XL C/C++ for AIX|access-date=31 January 2016}}

| {{Yes|Full}}

|

IBM Rational logiscope

|{{Yes|Full}}

|Until Logiscope 6.3, only basic constructs of C99 were supported. C99 is officially supported in Logiscope 6.4 and later versions.{{cite web|url=https://www-01.ibm.com/support/docview.wss?uid=swg21408170|title=IBM Rational Logiscope support for C99 standard - United States|date=24 February 2012 |access-date=31 January 2016}}

The Portland Group PGI C/C++

| {{Yes|Full}}

|

IAR Systems
Embedded Workbench

| {{partial|Mostly}}

| Does not support UCN (universal character names). Compiler for embedded targets, such as ARM, Coldfire, MSP430, AVR, AVR32, 8051, ... No x86 targets.

Intel C++ compiler

| {{okay|Mostly}}
{{citation needed|date=February 2015}}

|

Microsoft Visual C++

| {{partial}}{{cite web|last=Brenner|first=Pat|title=What's New for Visual C++ in Visual Studio 2015|url=https://msdn.microsoft.com/en-us/library/hh409293.aspx|publisher=Microsoft Developer Network|access-date=27 April 2015}}

|Visual C++ 2012 and earlier did not support C99.{{cite web|url=http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/|title=Reader Q&A: What about VC++ and C99?|work=Sutter’s Mill|date=3 May 2012 |access-date=31 January 2016}}{{cite web|url=https://msdn.microsoft.com/en-us/library/zb1574zs%28v=VS.100%29.aspx|title=A.27 Use of C99 Variable Length Arrays|publisher=Microsoft|access-date=31 January 2016}}{{cite web|url=https://www.infoq.com/news/2012/05/vs_c99_support|title=Microsoft to C99 Developers: Use ISO C++|work=InfoQ|access-date=31 January 2016}}
Visual C++ 2013 implements a limited subset of C99 required to compile popular open-source projects.{{cite web|url=https://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx|title=C99 library support in Visual Studio 2013|date=19 July 2013 |publisher=Microsoft|access-date=31 January 2016}}{{cite web|url=https://devblogs.microsoft.com/cppblog/c1114-stl-features-fixes-and-breaking-changes-in-vs-2013/ |title=C++11/14 STL Features, Fixes, And Breaking Changes In VS 2013 |publisher=Blogs.msdn.com |date=2013-06-28 |access-date=2014-04-08}}
Visual C++ 2015 implements the C99 standard library, with the exception of any library features that depend on compiler features not yet supported by the compiler (for example, <tgmath.h> is not implemented).
Visual C++ 2019 (16.6) adds opt-in support for a C99 conformant preprocessor.{{cite web|url=https://devblogs.microsoft.com/cppblog/announcing-full-support-for-a-c-c-conformant-preprocessor-in-msvc/|title=Announcing full support for a C/C++ conformant preprocessor in MSVC|publisher=Microsoft|date=27 March 2020|access-date=17 September 2020}}

Open Watcom

| {{partial}}

| Implements the most commonly used parts of the standard. However, they are enabled only through the undocumented command-line switch "-za99". Three C99 features have been bundled as C90 extensions since pre-v1.0: C++ style comments (//), flexible array members, trailing comma allowed in enum declaration.{{cite web|url=https://openwatcom.org/index.php/C99_Compliance |title=C99 compliance in Open Watcom |access-date=25 September 2015 |url-status=dead |archive-url=https://web.archive.org/web/20150503232909/http://openwatcom.org/index.php/C99_Compliance |archive-date=3 May 2015 }}

Pelles C

| {{yes|Full}}

| Supports all C99 features.{{cite web |url=https://www.smorgasbordet.com/pellesc/ |title=Pelles C Overview |date=January 2013 |url-status=dead |access-date=2 March 2022 |archive-date=13 March 2022 |archive-url=https://web.archive.org/web/20220313113801/http://smorgasbordet.com/pellesc/ }}

Portable C compiler

| {{partial}}

| Working towards becoming C99-compliant.{{citation needed|date=April 2012}}

Sun Studio

| {{yes|Full}}{{cite web|url=https://docs.oracle.com/cd/E19205-01/820-4155/c.html#about |title=Sun Studio 12: C Compiler 5.9 Readme |publisher=Sun Microsystems, Inc. |date=31 May 2007 |access-date=23 September 2012}}

|

The Amsterdam Compiler Kit

| {{no}}{{citation needed|date=August 2015}}

| A C99 frontend is currently under investigation.{{citation needed|date=August 2015}}

Tiny C Compiler

| {{partial}}

| Does not support complex numbers.{{cite web|url=https://bellard.org/tcc/tcc-doc.html#SEC7|title=Tiny C Compiler Reference Documentation|access-date=31 January 2016}}According to the project's [https://repo.or.cz/w/tinycc.git/blob/HEAD:/TODO TODO list] complex types are the only missing C99 feature. Variable Length Arrays have been added in TCC 0.9.26 [https://bellard.org/tcc/] Variable Length Arrays are supported but not as arguments in functions{{citation needed|date=March 2016}}. The developers state that "TCC is heading toward full ISOC99 compliance".{{cite web|url=https://bellard.org/tcc/|title=TCC : Tiny C Compiler|access-date=31 January 2016}}

vbcc

| {{partial}}

|

Future work

Since ratification of the 1999 C standard, the standards working group prepared technical reports specifying improved support for embedded processing, additional character data types (Unicode support), and library functions with improved bounds checking. Work continues on technical reports addressing decimal floating point, additional mathematical special functions, and additional dynamic memory allocation functions. The C and C++ standards committees have been collaborating on specifications for threaded programming.

The next revision of the C standard, C11, was ratified in 2011.{{cite web|url=https://gcc.gnu.org/onlinedocs/gcc/Standards.html |title=Standards - Using the GNU Compiler Collection (GCC) |publisher=Gcc.gnu.org |access-date=2014-04-08}} The C standards committee adopted guidelines that limited the adoption of new features that have not been tested by existing implementations. Much effort went into developing a memory model, in order to clarify sequence points and to support threaded programming.

See also

{{Portal|Computer programming}}

References

{{Reflist}}

Further reading

  • [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf N1256] (final draft of C99 standard plus TC1, TC2, TC3); WG14; 2007. ([https://port70.net/~nsz/c/c99/ HTML and ASCII versions])
  • [https://www.iso.org/standard/29237.html ISO/IEC 9899:1999] (official C99 standard); ISO; 1999.
  • [https://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf Rationale for C99]; WG14; 2003.
  • {{cite journal|last=Cheng|first=Harry|title=C99 & Numeric computing|journal=Dr. Dobb's Journal|url=https://drdobbs.com/cpp/184404993|date=1 March 2002}}
  • {{cite web|url=https://www.ibm.com/developerworks/library/l-c99.html|title=Open source development using C99|last=Seebach|first=Peter|date=24 March 2004|work=developerWorks|publisher=IBM}}