0

In my specific case, I firstly developed a program to run in a Texas Instruments microcontroller (TMS320F28335). It was a real time synchronous generator simulator so it needed to perform an important amount of floating-point operations. I did not specify any suffix for floating-point constants so they were treated as doubles (I guess that what the C standards says) but the compiler provided by Texas Instruments implements those doubles as 32 bits floating-point numbers so the FPU of the microcontroller was used (see Table 6-1 from the compiler user's guide) in an efficient way let's say. Then, I had to port that program to a BeagleBone Black with an embedded linux (patched for the real time requirements). Of course, all the constants were treated again as doubles by the compiler (GCC), but in this case that did not mean 32 bits floating-point numbers but 64 bits floating-point numbers. I do not fully understand how the FPU of the ARM Cortex A8 works but as far as I read (and since it is a 32 bits processor), performance would be improved if those floating-point constants were treated as 32 bits floating-point numbers. So, all of this led me to the question: is there a way to make more portable "best type" floating-point constants? In this case, I would have solve the problem by adding the "f" suffix to every constant because both processors are more efficient (I guess) treating with float, but if I am developing something in an amd64 PC and I want to port that to a 32 bits microcontroller, is there a way to add some suffix that could be changed to be "f" for 32 bits microcontrollers and "l" for an amd64 PC? I thought of something like this (of course, it doesn't work):

architecture-dependant header file for 32 bits microcontroller:

#define BEST_TYPE f
.
.
.

architecture-dependant header file for amd64 PC:

#define BEST_TYPE l
.
.
.

architecture-independent source file:

.
.
.
a = b * 0.1BEST_TYPE;
.
.
.

To clarify, by "best type" I meant the more precise numeric data type supported by the FPU of the microprocessor.

5
  • Most efficient or most precise? Your question seems to want both at once. double is supposed to be the most precise, portably.
    – Lundin
    Nov 14, 2019 at 15:43
  • The most precise numeric data type but directly supported by the FPU, so efficiency would be the main concern between efficiency and precision. Nov 14, 2019 at 16:00
  • You could define it as a macro with a parameter: #define BEST_TYPE(x) ((float)(x)). Then you could use a = b * BEST_TYPE(0.1); in your architecture-independent source file.
    – Ian Abbott
    Nov 14, 2019 at 16:37
  • Ok, that's an option I thought about. My doubt is, is the casting performed in compilation or in execution time? Most compilers should solve that in compilation time, right? Is it necessary to set some parameter in order to do so (despite of the level of optimization)? Nov 14, 2019 at 16:47
  • 1
    @IanAbbott: That is subject to double rounding. The decimal numeral must first be converted to double and then to float. This can yield a less accurate result than converting directly to float. It is better to append a suffix to indicate the type, as with #define BEST_TYPE(x) x##f. Nov 14, 2019 at 17:24

1 Answer 1

1

The question requests something similar to the macros in C 2018 7.20.4, “Macros for integer constants.” These macros, such as UINT64_C(value), expand to integer constants suitable for initializing objects with the corresponding type.

If we look at the <stdint.h> supplied with Xcode 11 for macOS 10.14, we see the implementations simply append a suffix that indicates the type (or does nothing, if the value will have the desired type by default):

#define UINT8_C(v)   (v)
#define UINT16_C(v)  (v)
#define UINT32_C(v)  (v ## U)
#define UINT64_C(v)  (v ## ULL)

We can use this as guidance for a similar macro for floating-point types, using one of:

#define MyFloatType_C(v)   (v ## f)
#define MyFloatType_C(v)   (v)
#define MyFloatType_C(v)   (v ## l)

Which definition of the macro to use could be chosen by various compile-time means, such as testing preprocessor macros (that are either built into the compiler to describe the target or that are explicitly passed on the command line).

1
  • Exactly what I wanted to know. Thank you! Nov 14, 2019 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.