Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin.h does not compile with Arduino IDE g++ #43

Open
martijn-heil opened this issue Nov 18, 2021 · 3 comments
Open

builtin.h does not compile with Arduino IDE g++ #43

martijn-heil opened this issue Nov 18, 2021 · 3 comments

Comments

@martijn-heil
Copy link

I am compiling psnip using the Arduino IDE for use on a Teensy, but it results in a compilation error when including psnip/builtin/builtin.h in a .cpp file. When I include it in a .c file it works fine, so that it suggests it has something to do specifically with C++.
In another issue I've read that you intend to support C++, but that it's just less well-tested.

In file included from C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\test.cpp:2:0:
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h: In function 'unsigned char psnip_intrin_BitScanForward64(long unsigned int*, uint64_t)':
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:636:63: error: '_' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                               ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:636:65: error: 'ctz' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                                 ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:636:68: error: 'PSNIP_BUILTIN__VARIANT_INT64' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                                    ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h: In function 'unsigned char psnip_intrin_BitScanReverse64(long unsigned int*, uint64_t)':
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:481:63: error: '_' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                               ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);
                                                                                                 ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:481:65: error: 'clz' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                                 ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);
                                                                                                 ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:481:68: error: 'PSNIP_BUILTIN__VARIANT_INT64' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                                    ^
C:\Users\ninjoh\AppData\Local\Temp\arduino_build_148318\sketch\psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);

sample code:

test.cpp
---
#include <stdint.h>
#include "psnip/builtin/builtin.h"
#include "psnip/endian/endian.h"

void test_do_nothing(void)
{
  uint16_t be = psnip_endian_be16(200);
}

How g++ is actually called by the Arduino IDE (newlines added for clarity):

"C:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" 
-c 
-O3 
-flto 
-fno-fat-lto-objects 
-g 
-Wall 
-ffunction-sections 
-fdata-sections 
-nostdlib
-MMD 
-fno-exceptions 
-fpermissive 
-felide-constructors 
-std=gnu++14 
-Wno-error=narrowing 
-fno-rtti 
-mthumb 
-mcpu=cortex-m4 
-fsingle-precision-constant 
-D__MK20DX256__ 
-DTEENSYDUINO=153 
-DARDUINO=10813 
-DARDUINO_TEENSY32 
-DF_CPU=24000000 
-DUSB_SERIAL_HID 
-DLAYOUT_US_ENGLISH
"-IC:\\Users\\ninjoh\\AppData\\Local\\Temp\\arduino_build_148318/pch" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy\\avr\\cores\\teensy3"
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\portable\\sketchbook\\libraries\\MFRC522\\src" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy\\avr\\libraries\\SPI"
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\portable\\sketchbook\\libraries\\Base64\\src" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\portable\\sketchbook\\libraries\\ArduinoBearSSL\\src" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy\\avr\\libraries\\EEPROM" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\portable\\sketchbook\\libraries\\EDB"
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy\\avr\\libraries\\Wire" 
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\hardware\\teensy\\avr\\libraries\\SD"
"-IC:\\Users\\ninjoh\\arduino-1.8.13\\portable\\sketchbook\\libraries\\ArduinoJson\\src" 
"C:\\Users\\ninjoh\\AppData\\Local\\Temp\\arduino_build_148318\\sketch\\test.cpp"
-o "C:\\Users\\ninjoh\\AppData\\Local\\Temp\\arduino_build_148318\\sketch\\test.cpp.o"
@martijn-heil
Copy link
Author

martijn-heil commented Nov 18, 2021

Trying at home on Linux I can reproduce the problem using the g++ bundled with the Arduino IDE v1.8.16
This time without all the related junk, just calling it directly, without arguments.

$ /usr/share/arduino/hardware/tools/arm/bin/arm-none-eabi-g++ -o /dev/null psnip/builtin/builtin.h 

psnip/builtin/builtin.h: In function 'unsigned char psnip_intrin_BitScanForward64(long unsigned int*, uint64_t)':
psnip/builtin/builtin.h:636:63: error: '_' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                               ^
psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
psnip/builtin/builtin.h:636:65: error: 'ctz' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                                 ^
psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
psnip/builtin/builtin.h:636:68: error: 'PSNIP_BUILTIN__VARIANT_INT64' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)
                                                                    ^
psnip/builtin/builtin.h:1253:61: note: in expansion of macro 'psnip_builtin_ctz64'
   return PSNIP_BUILTIN_UNLIKELY(Mask == 0) ? 0 : ((*Index = psnip_builtin_ctz64 (Mask)), 1);
                                                             ^
psnip/builtin/builtin.h: In function 'unsigned char psnip_intrin_BitScanReverse64(long unsigned int*, uint64_t)':
psnip/builtin/builtin.h:481:63: error: '_' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                               ^
psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);
                                                                                                 ^
psnip/builtin/builtin.h:481:65: error: 'clz' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                                 ^
psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);
                                                                                                 ^
psnip/builtin/builtin.h:481:68: error: 'PSNIP_BUILTIN__VARIANT_INT64' was not declared in this scope
 #  define psnip_builtin_clz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,clz)(x)
                                                                    ^
psnip/builtin/builtin.h:1287:97: note: in expansion of macro 'psnip_builtin_clz64'
   return (PSNIP_BUILTIN_UNLIKELY(Mask == 0)) ? 0 : ((*Index = ((sizeof(Mask) * CHAR_BIT) - 1) - psnip_builtin_clz64 (Mask)), 1);
arm-none-eabi-g++ (GNU Tools for ARM Embedded Processors) 5.4.1 20160919 (release) [ARM/embedded-5-branch revision 240496]
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@martijn-heil
Copy link
Author

Which leads me to the central problem:

psnip/builtin/builtin.h:636:68: error: 'PSNIP_BUILTIN__VARIANT_INT64' was not declared in this scope
 #  define psnip_builtin_ctz64(x) PSNIP_BUILTIN__VARIANT_INT64(_,ctz)(x)

@martijn-heil martijn-heil changed the title builtin.h does not compile with C++ builtin.h does not compile with Arduino IDE g++ Nov 18, 2021
@martijn-heil
Copy link
Author

martijn-heil commented Nov 18, 2021

PSNIP_BUILTIN__VARIANT_INT64 is not defined because it depends on
PSNIP_BUILTIN_SUFFIXINT64 which is not defined because it depends on one of

PSNIP_BUILTIN__SIZEOF_CHAR
PSNIP_BUILTIN__SIZEOF_SHRT
PSNIP_BUILTIN__SIZEOF_INT
PSNIP_BUILTIN__SIZEOF_LONG
PSNIP_BUILTIN__SIZEOF_LLONG

being equal to 64.
Their respective values are:

PSNIP_BUILTIN__SIZEOF_CHAR:   (undefined)
PSNIP_BUILTIN__SIZEOF_SHRT:   16
PSNIP_BUILTIN__SIZEOF_INT:    32
PSNIP_BUILTIN__SIZEOF_LONG:   32
PSNIP_BUILTIN__SIZEOF_LLONG:  (undefined)

For PSNIP_BUILTIN__SIZEOF_LLONG the following code defines it:

#if !defined(PSNIP_BUILTIN__SIZEOF_LLONG)
#  if   LLONG_MIN == (-0x7fLL-1) && LLONG_MAX == 0x7fLL
#    define PSNIP_BUILTIN__SIZEOF_LLONG 8
#  elif LLONG_MIN == (-0x7fffLL-1) && LLONG_MAX == 0x7fffLL
#    define PSNIP_BUILTIN__SIZEOF_LLONG 16
#  elif LLONG_MIN == (-0x7fffffffLL-1) && LLONG_MAX == 0x7fffffffLL
#    define PSNIP_BUILTIN__SIZEOF_LLONG 32
#  elif LLONG_MIN == (-0x7fffffffffffffffLL-1) && LLONG_MAX == 0x7fffffffffffffffLL
#    define PSNIP_BUILTIN__SIZEOF_LLONG 64
#  endif
#endif

But in this environment, LLONG_MIN and LLONG_MAX are apparently not defined.

CHAR_MIN is defined as 0.
CHAR_MAX is defined as (0x7f * 2 + 1).

Relevant extract from /usr/share/arduino/hardware/tools/arm/lib/gcc/arm-none-eabi/5.4.1/include-fixed/limits.h:

#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
/* Minimum and maximum values a `signed long long int' can hold.  */
# undef LLONG_MIN
# define LLONG_MIN (-LLONG_MAX - 1LL)
# undef LLONG_MAX
# define LLONG_MAX __LONG_LONG_MAX__

/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0).  */
# undef ULLONG_MAX
# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL)
#endif

#if defined (__GNU_LIBRARY__) ? defined (__USE_GNU) : !defined (__STRICT_ANSI__)
/* Minimum and maximum values a `signed long long int' can hold.  */
# undef LONG_LONG_MIN
# define LONG_LONG_MIN (-LONG_LONG_MAX - 1LL)
# undef LONG_LONG_MAX
# define LONG_LONG_MAX __LONG_LONG_MAX__

/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0).  */
# undef ULONG_LONG_MAX
# define ULONG_LONG_MAX (LONG_LONG_MAX * 2ULL + 1ULL)
#endif

the cplusplus.com article about limits.h states the following:

LLONG_MIN, LLONG_MAX and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).

But even compiling with -std=gnu++17, the issue remains.
Changing the #include <limits.h> at the top of builtin.h to #include <climits> does solve the issue on this environment though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant