diff options
author | Tom Stellard <thomas.stellard@amd.com> | 2013-06-26 18:21:13 +0000 |
---|---|---|
committer | Tom Stellard <thomas.stellard@amd.com> | 2013-06-26 18:21:13 +0000 |
commit | 0bb381eaecfe3d99646b73665610a2a5699cb727 (patch) | |
tree | 0755840cf3fe60cf80348c070963ea5403cfc2a2 /libclc/generic/lib/integer | |
parent | cb133c932262acc2e98fd42e7873dc4e317e1094 (diff) | |
download | bcm5719-llvm-0bb381eaecfe3d99646b73665610a2a5699cb727.tar.gz bcm5719-llvm-0bb381eaecfe3d99646b73665610a2a5699cb727.zip |
libclc: implement rotate builtin
This implementation does a lot of bit shifting and masking. Suffice to say,
this is somewhat suboptimal... but it does look to produce correct results
(after the piglit tests were corrected for sign extension issues).
Someone who knows LLVM better than I could re-write this more efficiently.
Patch by: Aaron Watry
llvm-svn: 184996
Diffstat (limited to 'libclc/generic/lib/integer')
-rw-r--r-- | libclc/generic/lib/integer/rotate.cl | 4 | ||||
-rw-r--r-- | libclc/generic/lib/integer/rotate.inc | 35 |
2 files changed, 39 insertions, 0 deletions
diff --git a/libclc/generic/lib/integer/rotate.cl b/libclc/generic/lib/integer/rotate.cl new file mode 100644 index 00000000000..d7eff2b1c84 --- /dev/null +++ b/libclc/generic/lib/integer/rotate.cl @@ -0,0 +1,4 @@ +#include <clc/clc.h> + +#define BODY <rotate.inc> +#include <clc/integer/gentype.inc> diff --git a/libclc/generic/lib/integer/rotate.inc b/libclc/generic/lib/integer/rotate.inc new file mode 100644 index 00000000000..e83dd51a832 --- /dev/null +++ b/libclc/generic/lib/integer/rotate.inc @@ -0,0 +1,35 @@ +/** + * Not necessarily optimal... but it produces correct results (at least for int) + * If we're lucky, LLVM will recognize the pattern and produce rotate + * instructions: + * http://llvm.1065342.n5.nabble.com/rotate-td47679.html + * + * Eventually, someone should feel free to implement an llvm-specific version + */ + +_CLC_OVERLOAD _CLC_DEF GENTYPE rotate(GENTYPE x, GENTYPE n){ + //Try to avoid extra work if someone's spinning the value through multiple + //full rotations + n = n % (GENTYPE)GENSIZE; + + //Determine if we're doing a right or left shift on each component + //The actual shift algorithm is based on a rotate right + //e.g. a rotate of int by 5 bits becomes rotate right by 26 bits + // and a rotate of int by -4 bits becomes rotate right by 4 + GENTYPE amt = (n > (GENTYPE)0 ? (GENTYPE)GENSIZE - n : (GENTYPE)0 - n ); + + //Calculate the bits that will wrap + GENTYPE mask = ( (GENTYPE)1 << amt ) - (GENTYPE)1; + GENTYPE wrapped_bits = x & mask; + + //Shift the input value right and then AND a mask that eliminates + //sign-extension interference + //if the rotate amount is 0, just use ~0 for a mask + GENTYPE se_mask = !amt ? ~((GENTYPE)0) : + ( ( (GENTYPE)1 << ((GENTYPE)GENSIZE - amt) ) - (GENTYPE)1 ); + GENTYPE unwrapped_bits = x >> amt; + unwrapped_bits &= se_mask; + + //Finally shift the input right after moving the wrapped bits into position + return unwrapped_bits | (wrapped_bits << ( (GENTYPE)GENSIZE - amt ) ); +}
\ No newline at end of file |