1

Sample code (t91.c):

#include <stdio.h>
#include <fenv.h>

#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif

void show_fe_exceptions(void)
{
    printf("exceptions raised:");
    if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID)) printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
int main(void)
{
    feraiseexcept(FE_OVERFLOW);
    show_fe_exceptions();
    return 0;
}

Invocations:

$ clang t91.c -Wall -Wextra -pedantic && ./a.exe
t91.c:7:14: warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma [-Wunknown-pragmas]
#pragma STDC FENV_ACCESS ON
             ^
1 warning generated.
exceptions raised: FE_OVERFLOW

$ gcc t91.c -Wall -Wextra -pedantic && ./a.exe
t91.c:7: warning: ignoring ‘#pragma STDC FENV_ACCESS’ [-Wunknown-pragmas]
    7 | #pragma STDC FENV_ACCESS ON
      |
exceptions raised: FE_OVERFLOW

$ cl t91.c /fp:strict && t91
exceptions raised: FE_INEXACT FE_OVERFLOW

ISO/IEC 9899:2011 (E):

7.6.2.2 The fegetexceptflag function

Whether the feraiseexcept function additionally raises the ‘‘inexact’’ floating-point exception whenever it raises the ‘‘overflow’’ or ‘‘underflow’’ floating-point exception is implementation-defined.

However, documentation for gcc and documentation for Microsoft C does not (or I cannot find it) document the exact behavior of feraiseexcept function. Note, that Clang/LLVM documents its implementation-defined behavior directly via the source code.

Also ISO C conformant implementation shall be accompanied by a document that defines all implementation-defined and locale-specific characteristics and all extensions (ISO/IEC 9899:2011 (E), section 4 paragraph 8).

Question (finally!): why C implementations do not document all the implementation-defined characteristics / behavior?

UPD. Yes, GCC's manual says wrt Library Functions: The behavior of most of these points are dependent on the implementation of the C library, and are not defined by GCC itself. Hence, we have to look at glibc implementation of feraiseexcept (which is a weak_alias for __feraiseexcept).

pmor
  • 5,392
  • 4
  • 17
  • 36
  • 1
    In the case of MS it is well known that their C implementations are not fully compliant. For example in C18 7.27.2.1 **The clock function** *The clock function determines the processor time used.* But [MS says](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/clock?view=msvc-160) *Calculates the wall-clock time used by the calling process.* – Weather Vane Mar 15 '21 at 19:33
  • For most things like this, GCC's manual says "This is dependent on the implementation of the C library, and is not defined by GCC itself." In other words, GCC itself isn't the entire implementation, so it can't be expected to document the parts it doesn't implement. If your C library doesn't define it, take it up with them. – Nate Eldredge Mar 15 '21 at 20:41
  • 1
    "why C implementations do not document all the implementation-defined characteristics / behavior?" Tautological answer: because nobody spent the time and energy to do it. It's not like there is some ISO C police that is going to arrest them if they don't. For the free software implementations, if you ask them, their response will surely be "yes, that would be great, feel free to submit a patch". – Nate Eldredge Mar 15 '21 at 20:43
  • @Nate Eldredge, thanks for the answer about free / open source implementations. However, what about non-free / closed source commercial C compiler / C standard library implementations? As far as I understand, in order to be ISO C conformant, such implementations are mandated to provide the documentation of all the implementation-defined characteristics / behavior. Otherwise, the customers have false impression regarding the ISO C conformance. – pmor Mar 16 '21 at 10:55
  • 1
    What examples do you know of a combined compiler/library implementation that explicitly claims full ISO C11 conformance? I don't think I know of any. – Nate Eldredge Mar 16 '21 at 15:18
  • The floating point environment features were added with C99, so to begin with you can obviously not use pre-C99 compilers. It's not clear which version of each compiler you are using. – Lundin Mar 18 '21 at 10:03
  • @NateEldredge Pretty much all commercial C compilers claim C11 compliance. Including [Microsoft Visual Studio 2019](https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/). That they don't conform to anything at all in practice is another story. It's not something that ISO will police, but government authorities in each country may act on false marketing scams. – Lundin Mar 18 '21 at 10:15
  • @Lundin Is there any known examples (of which you're aware) when government authorities (in some country) has acted on false marketing scams w.r.t. false conformance / compliance to C11 / IEEE 754? – pmor Apr 12 '21 at 12:50
  • @pmor I think they mostly lack the specialized knowledge necessary to do such things. I think American NIST did compiler validation at some point in the past, though I'm not sure at what extent it would be mandatory. – Lundin Apr 12 '21 at 14:49
  • 1
    Generally, such investigations nowadays would only happen after some sort of major incident, in which case it will be the relevant authorities for the application doing the research. Typically just safety- or mission-critical software, which caused accidents or got exploited by criminals etc. There are plenty of examples where defect software has been the subject of such investigations, but the fault in every well-known case I can think of have been made in the specification or by the application programmers, not the compiler. – Lundin Apr 12 '21 at 14:51
  • 1
    Also, compilers likely will label themselves "not suitable for any purpose" so that they can in particular dodge the usual crazy lawsuits that only happen in USA. – Lundin Apr 12 '21 at 14:53
  • @Lundin _By expressly declining fitness, they not only protect themselves from lawsuits from users, but also for any consequences from the generated code._ Source: https://stackoverflow.com/q/65743764/1778275. – pmor Apr 16 '21 at 18:51
  • @Lundin Re: "false marketing scams": Here is my experience. A non-conforming behavior has been reported to MS. (This code: `struct S x; struct S {int i;};` shall be accepted.) MS agreed, but decided to "won't fix, use workaround". However, the [documentation](https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170) says "The `/std:c11` option enables ISO C11 conformance". Tests show that "no, it doesn't". – pmor Mar 25 '22 at 23:37

0 Answers0