Author Topic: C macro to determine if a type is signed  (Read 12783 times)

0 Members and 1 Guest are viewing this topic.

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 2540
  • Country: pl
Re: C macro to determine if a type is signed
« Reply #25 on: November 29, 2019, 10:46:17 pm »
Due to it not working in msvc should be an indicator that you're entering terrain that will need to carry ifdefs and erroring out on unknown compilers. Not very useful.
C is not really supported by MSVC, so it is not expected that any C code will work there. Nowadays MSVC is a Microsoft C++/CLI compiler, which can also handle C++(1). C is “supported” only as long as it more or less falls under what C++ covers.
____
(1) But I heard it may fail to reject invalid C++ code despite required to do so.
Why 📎 | We live in times when half of people have IQ below 100.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4628
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: C macro to determine if a type is signed
« Reply #26 on: November 29, 2019, 10:59:29 pm »
As I explained before, it is possible to compile and run at build time a program that generates the necessary macros for the types needed.
The downside is that this won't work when cross-compiling to a different architecture or OS.
I have written a code generator in python for some C project. It fills a linked-list from snippets defined in many files. You could go this route.
There is even a C parser (compiled C) for python if you want.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8349
  • Country: fi
    • My home page and email address
Re: C macro to determine if a type is signed
« Reply #27 on: November 30, 2019, 01:47:03 am »
As I explained before, it is possible to compile and run at build time a program that generates the necessary macros for the types needed.
The downside is that this won't work when cross-compiling to a different architecture or OS.
I have written a code generator in python for some C project. It fills a linked-list from snippets defined in many files. You could go this route.
There is even a C parser (compiled C) for python if you want.
No, the issue is that the C code must be compiled and run on the target OS and architecture, not on the host, to determine the signedness.  You cannot determine the signedness of arbitrary types on the target by doing stuff on the host.  Having a code generator does not help at all there.

As to the types whose signedness is needed, that is trivial to maintain, and does not need a code generator: consider the practical use cases.
« Last Edit: November 30, 2019, 06:36:32 pm by Nominal Animal »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: C macro to determine if a type is signed
« Reply #28 on: November 30, 2019, 12:48:04 pm »
Someone posted a solution right here. But removed it!

It was probably me. I didn't like the solution as it was *really* an ugly hack, and requires manual maintenance etc. Also, it won't work with something like
Code: [Select]
IS_SIGNED(unsigned int) /* Will not work */
However, here is the principle:

Code: [Select]
#define char_is_signed 1
#define int_is_signed 1
#define int8_t_is_signed 1
#define uint8_t_is_signed 0
#define int16_t_is_signed 1
#define uint16_t_is_signed 0
#define int32_t_is_signed 1
#define uint32_t_is_signed 0

#define IS_SIGNED(T) (T ## _is_signed)
« Last Edit: November 30, 2019, 03:23:15 pm by Kalvin »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->