What's the intrinsic of interlocked subtract if there is add

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
devc1
Member
Member
Posts: 436
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

What's the intrinsic of interlocked subtract if there is add

Post by devc1 »

What's the intrinsic of atomic subtract if there is _interlockedadd
thewrongchristian
Member
Member
Posts: 406
Joined: Tue Apr 03, 2018 2:44 am

Re: What's the intrinsic of interlocked subtract if there is

Post by thewrongchristian »

devc1 wrote:What's the intrinsic of atomic subtract if there is _interlockedadd
If you add a negative number, that will be the same as subtraction:

Code: Select all

_InterlockedAdd(counter, -valueToSubtract);
devc1
Member
Member
Posts: 436
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: What's the intrinsic of interlocked subtract if there is

Post by devc1 »

What about unsigned values ?

Probably 8 + (-1) which is 8 + 0xFFFFFFFFFFFFFFFF will lead to 7 ?? or an overflow ?
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: What's the intrinsic of interlocked subtract if there is

Post by Minoto »

devc1 wrote:What about unsigned values ?

Probably 8 + (-1) which is 8 + 0xFFFFFFFFFFFFFFFF will lead to 7 ?? or an overflow ?
What happened when you wrote the code and ran it?
Those who understand Unix are doomed to copy it, poorly.
davmac314
Member
Member
Posts: 118
Joined: Mon Jul 05, 2021 6:57 pm

Re: What's the intrinsic of interlocked subtract if there is

Post by davmac314 »

Minoto wrote:What happened when you wrote the code and ran it?
Signed integer overflow normally has undefined behaviour in C. The only documentation I've seen for InterlockedAdd doesn't say that there's any exception for it in regards to this, so I would strongly suggest not using any observed behaviour as a reliable indicator of general behaviour.

Edit: in general an unsigned integer value can be safely converted to a signed integer value of the same size (the result is implementation-defined in C99; all common compilers for common architectures that I know of will just reinterpret the bitwise value as a signed value, effectively wrapping since the representation is 2's complement). So, for this particular example it should be perfectly fine to use unsigned values to effectively subtract some amount from some integer, so long as the result when interpreted as an operation on signed integers doesn't overflow. Specifically:
Probably 8 + (-1) which is 8 + 0xFFFFFFFFFFFFFFFF will lead to 7 ?? or an overflow ?
The -1 will be treated as a signed value because _InterlockedAdd takes signed arguments, i.e. -1u becomes -1 (again: subject to implementation-defined behaviour). Hence there is no overflow for this example (assuming the usual implementation-defined behaviour).

However, my point stands: if you believe that an operation may involve overflow, do not attempt to determine the behaviour by observing it. If the behaviour is undefined, it will not necessarily behave consistently.
Last edited by davmac314 on Thu Aug 03, 2023 9:52 pm, edited 2 times in total.
Octocontrabass
Member
Member
Posts: 5218
Joined: Mon Mar 25, 2013 7:01 pm

Re: What's the intrinsic of interlocked subtract if there is

Post by Octocontrabass »

If the type promotion rules result in an unsigned type, overflow wraps around according to twos-complement arithmetic. If they result in a signed type, overflow results in undefined behavior. You need to be pretty familiar with basic arithmetic in your chosen programming language before you try to write an operating system in it.
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: What's the intrinsic of interlocked subtract if there is

Post by Minoto »

davmac314 wrote:However, my point stands: if you believe that an operation may involve overflow, do not attempt to determine the behaviour by observing it. If the behaviour is undefined, it will not necessarily behave consistently.
Point taken. My intention was to encourage trying something to find out for oneself, but that's not necessarily always the best approach.
Those who understand Unix are doomed to copy it, poorly.
devc1
Member
Member
Posts: 436
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: What's the intrinsic of interlocked subtract if there is

Post by devc1 »

Looking at the intrinsic function, it does say that the value is signed. There is no 64 Bit address space yet so I won't need an unsigned integer for most of the time.
But the tip from thewrongchristian works perfectly for me, thanks.
Post Reply