Reputation: 57718
What are the C standard fixed width floating point types and where are they defined?
From MISRA-C:2004, Rule 6.3:
typedefs
that indicate size and signedness should be used in place of the basic numerical types.
The MISRA-C:2004, Rule 6.3 quotes that ISO (POSIX) typedefs are:
typedef char char_t;
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef float float32_t;
typedef double float64_t;
typedef long double float128_t;
At my company we are using C-99.
IAR Electronic Workbench, version 8.4
We are at MISRA-C:2004 because they voted not to upgrade MISRA, which would require create a validation test protocol and running the protocol.
Platform is ARM Cortex M running MicroCOS operating system.
Here are the detailed questions:
Upvotes: 1
Views: 1401
Reputation: 153517
C standard fixed width floating point types are not defined
C floating point (FP) goals are designed to embrace variations and many implementations.
MISRA FP goals are to restrict variety.
Fixed size FP types do not result in uniform bit encoding nor other consistent FP properties. They have limited usefulness in C - hence they are not part of the C standard or library.
Fall-back
Code could use below and a _Static_assert
(since C11) or a C99 substitute.
typedef float float32_t;
typedef double float64_t;
typedef long double float128_t;
_Static_assert(sizeof(float32_t)*CHAR_BIT == 32, "float 32");
_Static_assert(sizeof(float64_t)*CHAR_BIT == 64, "float 64");
_Static_assert(sizeof(float128_t)*CHAR_BIT == 128, "float 128");
Further notes
Compliant C may not have all 32, 64, 128 bit FP types, thus unable to define all float32_t, float64_t, float128_t
.
2 different Compliant C implementations may have a 32-bit FP types, but different encoding. Compare float32 vs. CCSI resulting in different range, precision and sub-normal support.
2 different Compliant C implementations may have a 32-bit FP types with the same encoding, but different endians, even if their integer endians agree.
Rule 6.3 (advisory): typedefs that indicate size and signedness should be used in place of the basic numerical types.: that goal "helps to clarify the size of the storage" and not much else.
Rule 1.5 (advisory): Floating-point implementations should comply with a defined floating-point standard. is particularly difficult to achieve. Even if an implementation uses the same FP encoding as IEEE 754, C allows the operations enough implementation defined behavior to differ from IEE 754.
Ideally, in C, an implementation that conforms to IEEE 754 defines __STDC_IEC_559__
. Yet proving and maintaining conformity is challenging enough that an implementation may forego defining __STDC_IEC_559__
as it may only be 99.999% conforming.
Upvotes: 4
Reputation: 222923
Apparently (unofficial source), MISRA-C:2004 rule 6.3 says:
… For example, the ISO (POSIX) typedefs as shown below are recommended and are used for all basic numerical and character types in this document. For a 32-bit integer machine, these are as follows:…
This is bad phrasing. It does not mean that POSIX or any ISO standard provides these typedef
declarations. It means the software conforming to MISRA, i.e., the software you are writing, should itself define these typedef
names and use them.
If that is not the intended meaning of the rule, then the rule is wrong to imply these types are specified by POSIX, because float64_t
does not appear in the POSIX 2008 specification. (I did not check earlier versions; I am assuming if it appeared in earlier versions, there would at least be mention of it in the POSIX 2008 version.)
Upvotes: 0
Reputation: 153517
What are the C standard fixed width floating point types and where are they defined?
There are none.
A fixed width FP type is likely insufficient for OP's higher level (unstated) goal.
If a fixed width floating point existed, it may only make for a fixed width. If would not certainly make for a fixed range, precision, encoding, etc.
If a fixed width floating point also defined encoding, portability is reduced. If will to live with that, consider instead a series of #if
tests for common FP encoding characteristics and simply use float, double, long double
.
Upvotes: 1