calculations in the preprocessing stage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro Graca

    calculations in the preprocessing stage

    Is there a way (preprocessor magic, perhaps ???) to use a type with
    twice as many bits as `unsigned` (or `int`) with no changes to the
    source code?

    ====
    #define SINGLE_TYPE unsigned
    #define DOUBLE_TYPE ???
    ====


    I tried the following, and it worked in 2 different computers, with 2
    different results.


    #include <limits.h>
    #include <stdio.h>

    #define SINGLE_TYPE unsigned
    #if UINT_MAX < ULONG_MAX / UINT_MAX
    # define DOUBLE_TYPE unsigned long
    # define DOUBLE_TYPE_STR ING "unsigned long"
    #else
    # if UINT_MAX < ULLONG_MAX / UINT_MAX
    # define DOUBLE_TYPE unsigned long long
    # define DOUBLE_TYPE_STR ING "unsigned long long"
    # else
    # error No available "DOUBLE_TYP E"
    # endif
    #endif

    int main(void) {
    printf("sizeof( SINGLE_TYPE) is %d; sizeof(DOUBLE_T YPE) is %d\n",
    (int)sizeof(SIN GLE_TYPE), (int)sizeof(DOU BLE_TYPE));
    printf("DOUBLE_ TYPE is \"%s\"\n", DOUBLE_TYPE_STR ING);
    return 0;
    }


    Is the above guaranteed to work, provided one of `unsigned long` or
    `unsigned long long` is at least twice as large as `unsigned`?
    It worked for me in two different compilers, but I'm not confident on
    the preprocessing calculations.

    (And, yes!, I realize `unsigned long long` is not defined by C89, but
    I couldn't find the option to use only C89 in the compiler where I
    needed that type)
  • Michael Mair

    #2
    Re: calculations in the preprocessing stage

    Pedro Graca wrote:
    Is there a way (preprocessor magic, perhaps ???) to use a type with
    twice as many bits as `unsigned` (or `int`) with no changes to the
    source code?
    >
    ====
    #define SINGLE_TYPE unsigned
    #define DOUBLE_TYPE ???
    ====
    >
    >
    I tried the following, and it worked in 2 different computers, with 2
    different results.
    >
    >
    #include <limits.h>
    #include <stdio.h>
    >
    #define SINGLE_TYPE unsigned
    #if UINT_MAX < ULONG_MAX / UINT_MAX
    # define DOUBLE_TYPE unsigned long
    # define DOUBLE_TYPE_STR ING "unsigned long"
    #else
    # if UINT_MAX < ULLONG_MAX / UINT_MAX
    # define DOUBLE_TYPE unsigned long long
    # define DOUBLE_TYPE_STR ING "unsigned long long"
    # else
    # error No available "DOUBLE_TYP E"
    # endif
    #endif
    Consider using typedef instead of #define for producing
    identifiers that are meant for types.
    >
    int main(void) {
    printf("sizeof( SINGLE_TYPE) is %d; sizeof(DOUBLE_T YPE) is %d\n",
    (int)sizeof(SIN GLE_TYPE), (int)sizeof(DOU BLE_TYPE));
    printf("DOUBLE_ TYPE is \"%s\"\n", DOUBLE_TYPE_STR ING);
    return 0;
    }
    >
    Is the above guaranteed to work, provided one of `unsigned long` or
    `unsigned long long` is at least twice as large as `unsigned`?
    It worked for me in two different compilers, but I'm not confident on
    the preprocessing calculations.
    >
    (And, yes!, I realize `unsigned long long` is not defined by C89, but
    I couldn't find the option to use only C89 in the compiler where I
    needed that type)
    What is it exactly you are trying to do?
    Just demanding "has at least twice as many bits as" (as your code
    above does) seems strange to me.

    The C99 header <stdint.hcan be used to determine whether exact
    width two's complement integer types are available; if int<N>_t
    is available as well as int<2N>_t (or uint<N>_t and uint<2N>_t,
    respectively), then you have a valid pairing.
    There are freely available <stdint.himplem entations / wrappers
    for non-C99 compilers, see e.g.



    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Flash Gordon

      #3
      Re: calculations in the preprocessing stage

      Michael Mair wrote, On 18/02/08 20:34:
      Pedro Graca wrote:
      >Is there a way (preprocessor magic, perhaps ???) to use a type with
      >twice as many bits as `unsigned` (or `int`) with no changes to the
      >source code?
      <snip>
      What is it exactly you are trying to do?
      Just demanding "has at least twice as many bits as" (as your code
      above does) seems strange to me.
      Seems strange to me as well. Twice the range is more likely to be useful
      (remember padding bits) but still seems unlikely to me.
      The C99 header <stdint.hcan be used to determine whether exact
      width two's complement integer types are available; if int<N>_t
      is available as well as int<2N>_t (or uint<N>_t and uint<2N>_t,
      respectively), then you have a valid pairing.
      There are freely available <stdint.himplem entations / wrappers
      for non-C99 compilers, see e.g.
      http://clc-wiki.net/wiki/stdint.h
      There are also the least and fast types in stdint.h which may be more
      appropriate depending on the exact requirements.
      --
      Flash Gordon

      Comment

      • Pedro Graca

        #4
        Re: calculations in the preprocessing stage

        Ben Bacarisse wrote:
        One key will be to loose the 1000000000.
        LOL, I'm too lazy.
        To print one of my bignums I just do

        printf("%u", bignum[0]);
        for (k=1; k<bignum_size; ++k) printf("%09u", bignum[k]);

        or the other way around if I store it little-endian

        Comment

        • Ben Bacarisse

          #5
          Re: calculations in the preprocessing stage

          Pedro Graca <hexkid@gmail.c omwrites:
          Ben Bacarisse wrote:
          >One key will be to loose the 1000000000.
          >
          LOL, I'm too lazy.
          To print one of my bignums I just do
          >
          printf("%u", bignum[0]);
          for (k=1; k<bignum_size; ++k) printf("%09u", bignum[k]);
          >
          or the other way around if I store it little-endian
          OK, but most people would buy compute speed at the expense of slightly
          more complex printing!

          --
          Ben.

          Comment

          Working...