OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 28, 2024 6:23 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: What's the intrinsic of interlocked subtract if there is add
PostPosted: Thu Aug 03, 2023 3:00 pm 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
What's the intrinsic of atomic subtract if there is _interlockedadd


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 4:26 pm 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 403
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:
_InterlockedAdd(counter, -valueToSubtract);


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 5:31 pm 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
What about unsigned values ?

Probably 8 + (-1) which is 8 + 0xFFFFFFFFFFFFFFFF will lead to 7 ?? or an overflow ?


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 7:06 pm 
Offline
Member
Member
User avatar

Joined: Thu May 12, 2011 7:24 pm
Posts: 89
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.


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 8:15 pm 
Offline
Member
Member

Joined: Mon Jul 05, 2021 6:57 pm
Posts: 118
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:

Quote:
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.

Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 8:22 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
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.


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Thu Aug 03, 2023 11:22 pm 
Offline
Member
Member
User avatar

Joined: Thu May 12, 2011 7:24 pm
Posts: 89
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.


Top
 Profile  
 
 Post subject: Re: What's the intrinsic of interlocked subtract if there is
PostPosted: Fri Aug 04, 2023 11:25 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group