Created
April 23, 2019 08:40
-
-
Save spot62/30d9ffc23a2993ea301072fbcabf9fb2 to your computer and use it in GitHub Desktop.
Byte clamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static unsigned char byte_clamp(int c) | |
{ | |
unsigned char rc = { static_cast<unsigned char>(c), 255, 0 }; | |
unsigned uc = static_cast<unsigned>(c); | |
// +1 if greater 255 | |
// and +1 if bit of sign 1 | |
return rc[!!(uc & ~0xff) + !!(uc & ~(~0u >> 1))]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment