3

I'm writing a program for esp32 using the Arduino IDE that needs to count the number of '1' bits in a byte.

I found a post elsewhere that says there's a nice function in the standard library to do it: std::popcount(value)

... the catch is, it only exists in C++20.

Where is the C++ standard level defined in an Arduino IDE project, and what do I have to do to change whatever level it selected by default when I created the project to C++20?

Update

@jsotola's comment solved my immediate problem. That said, I was still curious about changing the C++ version used by Arduino IDE.

I'm still unsure about the present version, but it appears that at least as of approximately 2018, the version of C++ used by Arduino IDE is (was?) hardwired & not something you can easily change. The bundled C++ compiler itself can probably deal with newer versions, but the IDE itself instructs it to compile the code according to an older version of C++ (C++2011 or C++2014, I believe).

There's apparently still disagreement about just how much of the C++ standard library is available to code built using Arduino with ARM, and it's more of a spectrum than black & white.

In the case of 8-bit AVR, most of the C++ Standard Library is missing (though it's sometimes possible to forcibly put back the chunks that were ripped out... I distinctly remember finding a method a couple of years ago to restore sprint's ability to format floats if you're building for something like a Mega 2560 and don't have to particularly care about minimizing size).

The (as yet unconfirmed) impression I got last night is that as of 2024, there are standard library limits imposed by Arduino IDE, but not necessarily imposed by Arduino framework. Meaning, there are language features that won't work in Arduino IDE, but will (or at least, can be forcibly made to) work if you're using something like PlatformIO and VS Code.

Likewise, changing the target C++ level (11, 14, 17, 20, 23) is semi-straightforward if you're using PlatformIO + VS Code (or some other IDE that isn't "Arduino IDE"), but effectively impossible with Arduino IDE.

I got the impression that in most cases, almost any C++ standard library can be made to compile without errors and run from the canonical source code... but libraries that have never been optimized for something like esp32 might run really, really poorly compared to their performance on a PC, or even a Raspberry Pi.

Using the population-count/Hamming-distance example, a CPU like the one in an esp32 might actually have a proper instruction to do it (I don't actually know whether it does), but the code in the "canonical" C++ standard library, having never been tweaked to optimize it for esp32, might fail to use that optimized instruction & instead compile to code that does the task in just about the worst way possible.

In the specific context of ESP32, there are apparently a lot of of as-yet-unoptimized standard library classes that make poor choices about SRAM vs PSRAM (or make no choices at all, and just ignore PSRAM) when forcibly-compiled for esp32, and do bad things with dynamic memory allocation in general.

Some of those things have improved over time as other standard-library classes the standard-library classes depend upon have been improved and optimized for esp32, but until someone who knows what they're doing makes a point of really reviewing the way code in a library compiles for esp32 and optimizes for it, it's kind of a crapshoot as to whether it will compile "well", or compile to something that would horrify you if you looked at (and understood) the generated assembly language.

Take the above with a few kilograms of skeptical doubt. It's the result of a few hours of reading about a topic I don't have a particularly deep understanding of.

1

0

Your Answer

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

Browse other questions tagged or ask your own question.