Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
A Type Conversion Question
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Kevin Bracey, post: 2407392"] In message <xxpis425bxi.fsf@usrts005.corpusers.net> I think you may have a valid point there. Given that it was signed arithmetic, then technically speaking you invoke undefined behaviour by overflow. So the compiler is free to do what it wants. However, for unsigned arithmetic, the standard does require results to be reduced modulo (TYPE_MAX+1). Also, nearly all implementations document signed arithmetic overflow as being handled in a modulo fashion, effectively "defining" the undefined behaviour. So your compiler may still be violating its documentation. For what it's worth, assuming you do want a 16x16->32 multiply, the way I'd write it in C would be: acc += (INT32_T) x[n] * y[n]; That promotes x[n] to INT32_T, and hence y[n] as well (because the operands must match), so you invoke a 32x32->32 multiply. C does not have the concept of an operation that produces a result wider than its operands. However, hopefully a compiler will spot when a semantic 32x32->32 multiply is actually of the form: (16-bit signed quantity widened to 32 bits) x (16-bit signed quantity widened to 32 bits) -> 32 bits and reduce that into a signed 16x16->32 multiply operation, if that's sensible for the target architecture. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
A Type Conversion Question
Top