Re: Comparing Arbitrary-Precision Integers

Discussion in 'C Programming' started by Michael Angelo Ravera, Jul 27, 2012.

  1. On Thursday, July 26, 2012 1:06:44 PM UTC-7, 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?
    >
    > I think the right approach is to compare the most significant limb in
    > a signed sense then to compare the least significant limbs in an
    > unsigned sense. Is that right?
    >
    > Code below.
    >
    > Thanks, Dave A.
    >
    > ---------
    >
    > //Signed 96-bit synthetic type, stored least significant words first
    > (similar to the
    > //way the GMP treats limbs).
    > //
    > //The dual representation is used because this way the compiler can be
    > coerced to
    > //choose appropriate instructions in different situations.
    > typedef union
    > {
    > UNSIGNED32_T uw[3];
    > SIGNED32_T sw[3];
    > } APP_BIGINT_S96_T;
    >
    >
    > //----------------------------------------------------------------------------------------------------
    > //DESCRIPTION
    > // Compares two signed 96-bit integers and returns -1 if a < b, 0 if
    > a == b, and 1 if a > b.
    > //
    > //INPUTS
    > // *in_a, *in_b:
    > // Two signed 96-bit integers to compare.
    > //
    > //OUTPUTS
    > // <--
    > // -1 if *in_a < *in_b, 0 if *in_a == *in_b, and 1 if *in_a >
    > *in_b.
    > //
    > //EXCEPTION CONDITIONS
    > // Passing a NULL or invalid pointer for in_a or in_b will result in
    > undefined behavior.
    > //
    > //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.
    > //
    > //UNIT TEST HISTORY
    > // 20120713: Not yet unit tested.
    > //----------------------------------------------------------------------------------------------------
    > SIGNED32_T APP_S96_Cmp(const APP_BIGINT_S96_T *in_a, const
    > APP_BIGINT_S96_T *in_b)
    > {
    > if (in_a->sw[2] < in_b->sw[2])
    > {
    > return(-1);
    > }
    > else if (in_a->sw[2] > in_b->sw[2])
    > {
    > return(1);
    > }
    > else if (in_a->uw[1] < in_b->uw[1])
    > {
    > return(-1);
    > }
    > else if (in_a->uw[1] > in_b->uw[1])
    > {
    > return(1);
    > }
    > else if (in_a->uw[0] < in_b->uw[0])
    > {
    > return(-1);
    > }
    > else if (in_a->uw[0] > in_b->uw[0])
    > {
    > return(1);
    > }
    > else
    > {
    > return(0);
    > }
    > }


    Your code is reentrant because you aren't storing off any results in the middle of the calculation into static variables and you aren't making changesto the underlying code. Whether your code is thread safe or not depends upon the attributes applied to the various instances of APP_BIGINT_S96Ts thatyou declare.
     
    Michael Angelo Ravera, Jul 27, 2012
    #1
    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:
    384
    christopher diggins
    May 24, 2005
  2. Eric Sosman

    Re: Comparing Arbitrary-Precision Integers

    Eric Sosman, Jul 26, 2012, in forum: C Programming
    Replies:
    1
    Views:
    373
    Tim Rentsch
    Sep 7, 2012
  3. BartC
    Replies:
    2
    Views:
    373
    BartC
    Jul 26, 2012
  4. Ike Naar

    Re: Comparing Arbitrary-Precision Integers

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

Share This Page