Skip to content

Instantly share code, notes, and snippets.

@astocko
Created November 21, 2013 17:00
Show Gist options
  • Save astocko/7585496 to your computer and use it in GitHub Desktop.
Save astocko/7585496 to your computer and use it in GitHub Desktop.
Absolute value and sign flip demo with bitmask and SSE instructions.
#include <iostream>
static constexpr int kAbsMask = 0x7FFFFFFF;
static constexpr int kFlipMask = 0x80000000;
int main(int argc, char const *argv[])
{
float y = -30.234;
float z = 40.023;
// abs value
__asm {
movss xmm9, kAbsMask
movss xmm0, y
andps xmm0, xmm9
movss y, xmm0
}
// flip sign
__asm {
movss xmm9, kFlipMask
movss xmm0, z
xorps xmm0, xmm9
movss z, xmm0
}
std::cout << "Abs Value: " << y << std::endl;
std::cout << "Flipped: " << z << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment