Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:06:57+00:00 2026-06-10T19:06:57+00:00

I want to pack/unpack two signed 16 bit integers into a 32 bit integer.

  • 0

I want to pack/unpack two signed 16 bit integers into a 32 bit integer. However, I’m not getting it to quite work.

Any ideas as to what I might be doing wrong?

template <typename T>
int read_s16(T& arr, int idx) restrict(amp)
{
    return static_cast<int>((arr[idx/2] >> ((idx % 2) * 16)) << 16) >> 16;
}

template<typename T>
void write_s16(T& arr, int idx, int val) restrict(amp)
{
    // NOTE: arr is zero initialized
    concurrency::atomic_fetch_or(&arr[idx/2], (static_cast<unsigned int>(val) & 0xFFFF) << ((idx % 2) * 16));
}

The function return/arguments must be as I have defined. The lo and hi are written from different threads (thus the atomic_or), and the read must return a single 32 bit value.

16 bit integer arithmetics are not supported on the target platform.

Example:

array<int> ar(1); // Container

write_s16(ar, 0, -16);
write_s16(ar, 1, 5);

assert(read_s16(ar, 0) == -16);
assert(read_s16(ar, 1) == 5);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:06:59+00:00Added an answer on June 10, 2026 at 7:06 pm

    These atomic operations in C++ AMP also have the following limitations:

    • You should not mix atomic and normal (non-atomic) reads and writes.
      Normal reads may not see the results of atomic writes to the same
      memory location. Normal writes should not be mixed with atomic writes
      to the same memory location. If your program does not comply with
      these criteria then this will lead to an undefined result.
    • Atomic
      operations do not imply a memory fence of any sort. Atomic operations
      may be reor-dered. This differs from the behavior of interlocked
      operations in C++.

    It would seem like you are violating the first of these.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large array of booleans, and I want to pack/unpack them into
I want to pack the project into a zip-file and have the build date
This is the bit of Ruby I want to implement in Python: Base64.urlsafe_encode64([Digest::MD5.hexdigest(url).to_i(16)].pack(N)).sub(/==\n?$/, '')
I have some big code line which I want to pack into define for
In Perl, pack and unpack have two templates for converting bytes to/from hex: h
I'm implementing a specialized version of JTextField and want to pack it into an
I want to pack epoch milliseconds into 6 bytes but i have problem. Let
In Android I want to pack some files into Zip archive (using java.util.zip ).
I want to put a bunch of packed integers into a file, e.g.: for
I want to pack a MIDI message into an NSData object. int messageType =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.