GCC builtin

Hey, I am currently trying to convert a uint16_t from big-endian to little-endian in a relatively optimized way.

From what I have looked at it seems that __builtin_bswap16 is the best way to do this. However when trying to compile, I get a build error saying that it isn’t a function I can call:

error C3861: '__builtin_bswap16': identifier not found

Should it be doing this or am I missing something?

Thanks!

That sure looks like an error from the Microsoft compiler.

Anyway, I’d step back and ask myself if this is really worth worrying about. Do it the straightforward way first and see if performance really matters. If you can measure and show that a few extra instructions really makes the difference, then revisit it.

2 Likes

Do it the old school way and let the optimizer tune it up for you. You might need to kick up the optimization level to something like 3 or use the -march=native flag. You can check the generated code but in general, this is something the optimizer is well able to catch and improve.

1 Like

Thanks guys. I’m probably just over thinking things.

Hey, we all get sucked into that from time to time…

I agree with the recommendations to profile to see if you need it, but Godbolt is a great tool for exploring how good compiler optimizers are. In this case, GCC generates exactly the same code for the naive shift and or as for the builtin. MSVC’s optimizer is not as good, and does generate different code for the naive implementation and the equivalent compiler intrinsic. https://godbolt.org/z/Pebfi2

Yes, there’s room for improvement here in MSVC. If you post on the Microsoft developer forum under C++ (https://developercommunity.visualstudio.com/spaces/62/index.html) with your example the back-end team will definitely respond.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.