Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Rounding during division

Reply
Thread Tools

Rounding during division

 
 
Charlie Gordon
Guest
Posts: n/a
 
      09-18-2007
"Christopher Key" <(E-Mail Removed)> a écrit dans le message de news:
46efb320$0$21085$(E-Mail Removed)...
> Charlie Gordon wrote:
>> "Justin Spahr-Summers" <(E-Mail Removed)> a écrit dans le
>> message de news: (E-Mail Removed) om...
>>> On Sep 17, 4:18 am, Christopher Key <(E-Mail Removed)> wrote:
>>>> Is there any clean, portable way of acheiving signed integer division
>>>> by
>>>> a power of 2 with rounding towards -inf (under C99). gcc guarantees
>>>> that right-shifts on a signed integer will always give this behaviour,
>>>> and I'm guessing that pretty much any code compiled for a target with
>>>> an
>>>> ASR instruction will do the same, but I really don't want the code to
>>>> silently break for compilers that don't.
>>>>
>>>> The options seem to be:
>>>>
>>>> 1) Discover how right shift of negative values is implemented using
>>>> preprocessor macros and issue an error at compile time if required. I
>>>> don't think this is possible, although a useful extension to the C
>>>> standard might be to force the provision of macros that allow code to
>>>> determine how implementation defined behaviour has been implemented.
>>> typedef int assertion_dummy_array[(-value >> shiftbits == othervalue)
>>> * 2 - 1];
>>>
>>> If the condition given in the parentheses evaluates to false (0), then
>>> the array typedef will have a negative size, which should issue a
>>> compiler diagnostic.

>>
>> Of course you can test implementation defined behaviour at run time and
>> at compile time in this case (call it static_assert).
>> But you want to do this at the preprocessing stage in order to select the
>> proper code for the implementation. There is no guarantee that the
>> preprocessor use the same arithmetics as the target, so you cannot rely
>> on #if (-1 >> 1) == -1 for instance. One would need explicit macros
>> along the lines of INT_MAX to detect what type of integer representation
>> is used, what type of arithmetics, and so on.
>>

>
> Presumably, all that INT_MAX and INT_MIN will tell you is whether you're
> using a two's complement machine or not, and there's no guarantee that a
> specific shift behaviour has been implemented.
>
> I guess that the safest way is to simply perform a runtime check.


Except c99 6.5.7 specifies that sight shifting a signed type with negative
value results in an implementation defined result but does not give an
exhaustive list of possible implementations. To correctly detect at runtime
a given 'expected' behaviour (such as ARS or LSR) one would need to conduct
an exhaustive test of all possible shift operands. Furthermore,
implementation defined behaviour includes terminating the program (a known
characteristic of the DS9K)

Definitely, a few macros in stdint specifying INT_RIGHT_SHIFT_IS_SIGNED or
similar could help. The same goes for macros telling about integer
representation and other signed arithmetic specifics.

--
Chqrlie.


 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-18-2007
Charlie Gordon wrote:
>

.... snip ...
>
> Except c99 6.5.7 specifies that sight shifting a signed type with
> negative value results in an implementation defined result but
> does not give an exhaustive list of possible implementations. To
> correctly detect at runtime a given 'expected' behaviour (such as
> ARS or LSR) one would need to conduct an exhaustive test of all
> possible shift operands. Furthermore, implementation defined
> behaviour includes terminating the program (a known characteristic
> of the DS9K)


Check your supplier. Your DS9K lacks the latest improvements. Mine,
in the same circumstances, submits a fake tax return to the IRS,
with flags that ensure the IRS pulls it for examination. The fake
return has material that proves you hid 10,000,000 USD (or more)
income last year, and makes you liable for the tax, penalties,
etc.

After this action the machine terminates, after wiping out all
traces of actions taken since the (attempted) right shift of a
negative value.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning during generating classes through WSIMPORT & Error during xmlvalidation traveller Java 0 01-08-2008 07:00 AM
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
rounding to integer valentin tihomirov VHDL 2 02-16-2004 10:07 AM
Datagrid not updated during delete, but updated during insert and update Dmitry Korolyov ASP .Net Datagrid Control 0 09-22-2003 10:57 AM
will Synposys Design Compiler support division by two's power and integer rounding? walala VHDL 12 09-14-2003 03:49 PM



Advertisments