Re: Comparing Arbitrary-Precision Integers

Discussion in 'C Programming' started by Eric Sosman, Jul 26, 2012.

  1. Eric Sosman

    Eric Sosman Guest

    On 7/26/2012 4:06 PM, David T. Ashley wrote:
    > Hi,
    >
    > I'm using a 2's complement machine (and I'm comfortable baking this
    > assumption into the code). I've implemented a large number of
    > functions on signed integers that are longer than the machine
    > supports.
    >
    > Does the code below for comparison look correct?


    Looks plausible.

    > typedef union
    > {
    > UNSIGNED32_T uw[3];
    > SIGNED32_T sw[3];
    > } APP_BIGINT_S96_T;


    You may need to be vigilant about the type-punning here.
    I think you're all right as long as you deal in pointers to the
    union type itself, but it'd be a good idea to avoid passing
    around pointers to the uw and sw members independently. Some
    compiler might reason "Storing through this `unsigned int*'
    cannot affect the value I've already loaded from this other
    `signed int*' ..." (Disclaimer: Aliasing rules are slipperier
    than my grasp is firm, and I may be worrying about nothing. But
    if I were you I'd put a Bar-certified language lawyer on retainer.)

    > //REENTRANCY
    > // This function has not been evaluated for re-entrancy or thread
    > safety. This function is
    > // designed to be called from background (non-ISR) software only.


    There cannot be reentrancy problems, since the function cannot
    be re-entered. I see no concurrency issues in the function itself;
    you need only ensure that the bigints aren't being changed while
    you're in the act of inspecting them. The ISR restriction seems
    irrelevant.

    > //UNIT TEST HISTORY
    > // 20120713: Not yet unit tested.


    I trust you've remedied this in the last two weeks ...

    --
    Eric Sosman
    d
    Eric Sosman, Jul 26, 2012
    #1
    1. Advertising

  2. Eric Sosman

    Tim Rentsch Guest

    Eric Sosman <> writes:

    > On 7/26/2012 4:06 PM, David T. Ashley wrote:
    >> Hi,
    >>
    >> I'm using a 2's complement machine (and I'm comfortable baking this
    >> assumption into the code). I've implemented a large number of
    >> functions on signed integers that are longer than the machine
    >> supports.
    >>
    >> Does the code below for comparison look correct?

    >
    > Looks plausible.
    >
    >> typedef union
    >> {
    >> UNSIGNED32_T uw[3];
    >> SIGNED32_T sw[3];
    >> } APP_BIGINT_S96_T;

    >
    > You may need to be vigilant about the type-punning here.
    > I think you're all right as long as you deal in pointers to the
    > union type itself, but it'd be a good idea to avoid passing
    > around pointers to the uw and sw members independently. Some
    > compiler might reason "Storing through this `unsigned int*'
    > cannot affect the value I've already loaded from this other
    > `signed int*' ..." (Disclaimer: Aliasing rules are slipperier
    > than my grasp is firm, [snip elaboration]


    The types signed/unsigned int are always allowed to alias, as are
    all other corresponding pairs of signed/unsigned integer types.

    However, it would be worth checking to see if gcc's 'strict-aliasing'
    option may violate this rule (and possibly generate non-conforming
    code as a result).
    Tim Rentsch, Sep 7, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins

    Diggins PDP #3 : arbitrary precision integers

    christopher diggins, May 24, 2005, in forum: C++
    Replies:
    0
    Views:
    366
    christopher diggins
    May 24, 2005
  2. BartC
    Replies:
    2
    Views:
    356
    BartC
    Jul 26, 2012
  3. Michael Angelo Ravera

    Re: Comparing Arbitrary-Precision Integers

    Michael Angelo Ravera, Jul 27, 2012, in forum: C Programming
    Replies:
    0
    Views:
    357
    Michael Angelo Ravera
    Jul 27, 2012
  4. Ike Naar

    Re: Comparing Arbitrary-Precision Integers

    Ike Naar, Jul 27, 2012, in forum: C Programming
    Replies:
    1
    Views:
    346
    Keith Thompson
    Jul 28, 2012
  5. Noob
    Replies:
    0
    Views:
    342
Loading...

Share This Page