5 ms·
How would you write different code for different architectures or based on compilation flags without preprocessor directives? Rust has directives for specifying
by stochastic_monk 8y ago
How would you write different code for different architectures or based on compilation flags without preprocessor directives? Rust has directives for specifying which version to compile, but C++ currently doesn’t. However, I find #if __AVX512BW__/#elif __AVX2__/#elif__SSE2__#else/#endif to be easy and flexible, allowing only a subset of code to vary by architecture. I also find that macros allow me to write much more concise, maintainable code.
It’s archaic and low level, but it’s also powerful and expressive. Replacing the CPP would probably just require a new language.
- dingo_bat 8y ago> more concise, maintainable code This sounds like an oxymoron.
- pjmlp 8y agoLike when one does proper C or C++ clean coding without pre-processor spaghetti, an implementation file for each architecture or OS specific coding.
- MrStonedOne 8y agoSo, copy and pasted code spread out everywhere rather than centralized?
- banachtarski 8y agoYes thank you. Not to mention that the parent comment neglects the fact that splitting out files just means you have to now worry about conditional inclusion or tighter coupling with your build system.
- pjmlp 8y agoThat approach has successfully turned a pile of spaghetti C pre-processor code into a managable secure server application deployed across Aix, HP-UX, Solaris, Linux and Windows NT/2000. Yes it was tighly coupled with Make that took care of selecting the proper set of files to compile and link based on the platform. The anti-module crowd seems to want to have it all, which to me is the same anti-exceptions and anti-RTTI crowd, and in that case modules are indeed dead-on-arrival.
- frutiger 8y ago> That approach has successfully turned a pile of spaghetti C pre-processor code into a managable secure server application deployed across Aix, HP-UX, Solaris, Linux and Windows NT/2000. I have also weitten and deployed what you’re denigrating as “preprocessor spaghetti code” across the above exact platforms, with a lot of success. Perhaps we work(ed) at the same company.
- pjmlp 8y agoNot at all, fine tuned in small functions or architecture specific headers.
- tambre 8y agoWhat about using `if constexpr`? Sure, there currently are no equivalent constant variables to use, but adding those should be very easy.
- stochastic_monk 8y agoif constexpr only works for procedural code, not data members. (e.g., use __m128i v[4] for SSE2, __m256 v[2] for __AVX2__, etc.) It could be templated, as long as there was a compile-time method besides the CPP to get architectural information, so that would be a way (if much more verbose) forward. That being said, if constexpr is great; iterating over either sparse or dense arrays with in Blaze without wrapping in two functions was mind-blowing when I discovered I could so easily.
- gmueckl 8y agoThat is why static if and version in D work very differently. Alexei Alexandescu criticized if constexpr in C++ for being a poor copy of static if that misses the point, but that was dismissed. But it is powerful enough to handle almost all cases of conditional compilation.
- rini17 8y agoIsn't C++ supposed to be object-oriented? Why you couldn't have specific class optimized for each architecture?
- wbkang 8y agoCouldn't you just link against a different version? e.g. mymodule-avx2.o vs mymodule-avx.o