Answered by:
Using unsigned short arrays in AMP
Question

Hello all. For my image processing software I need to be able to pass in byte and unsigned short arrays into AMP kernels (from C#). Thanks to some help from the people on this forum I've got the byte portion working well but now I'm stuck on the unsigned short functionality. To operate on bytes in AMP I'm using the code from the Steve Deitz essay here: http://blogs.msdn.com/b/nativeconcurrency/archive/2012/01/17/campitsgotcharacterbutnochar.aspx
I just need to read/write my byte and unsigned short arrays. From Steve's unsigned char example he first casts the input unsigned char array as an unsigned int:
array_view<unsigned int> d_data((size+3)/4, reinterpret_cast<unsigned int*>(data.data()));
Then he provides us with 2 helper functions to read/write a byte at some location in the array:
// Read:
template <typename T>
unsigned int read_uchar(T& arr, int idx) restrict(amp)
{
return (arr[idx >> 2] & (0xFF << ((idx & 0x3) << 3))) >> ((idx & 0x3) << 3);
}// Write:
template<typename T>
void write_uchar(T& arr, int idx, unsigned int val) restrict(amp)
{
atomic_fetch_xor(&arr[idx >> 2], arr[idx >> 2] & (0xFF << ((idx & 0x3) << 3)));
atomic_fetch_xor(&arr[idx >> 2], (val & 0xFF) << ((idx & 0x3) << 3));
}Even with his diagram I'm having a heck of a time figuring out how to modify these bitwise operations for unsigned short. Could someone please show how the cast and the two helper functions can be altered to allow read/write capability for 16bit unsigned ints? Thank you very much in advance.
LKeene
Saturday, September 29, 2012 10:26 PM
Answers

// Read: template <typename T> unsigned int read_ushort(T& arr, int idx) restrict(amp) { return (arr[idx >> 1] & (0xFFFF << ((idx & 0x1) << 4))) >> ((idx & 0x1) << 4); } // Write: template<typename T> void write_ushort(T& arr, int idx, unsigned int val) restrict(amp) { atomic_fetch_xor(&arr[idx >> 1], arr[idx >> 1] & (0xFFFF << ((idx & 0x1) << 4))); atomic_fetch_xor(&arr[idx >> 1], (val & 0xFFFF) << ((idx & 0x1) << 4)); }
Saturday, September 29, 2012 11:46 PM
All replies

// Read: template <typename T> unsigned int read_ushort(T& arr, int idx) restrict(amp) { return (arr[idx >> 1] & (0xFFFF << ((idx & 0x1) << 4))) >> ((idx & 0x1) << 4); } // Write: template<typename T> void write_ushort(T& arr, int idx, unsigned int val) restrict(amp) { atomic_fetch_xor(&arr[idx >> 1], arr[idx >> 1] & (0xFFFF << ((idx & 0x1) << 4))); atomic_fetch_xor(&arr[idx >> 1], (val & 0xFFFF) << ((idx & 0x1) << 4)); }
Saturday, September 29, 2012 11:46 PM 
Works perfectly. Thank you!
Lkeene
Sunday, September 30, 2012 5:18 PM