473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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_STRING "unsigned long"
#else
# if UINT_MAX < ULLONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long long
# define DOUBLE_TYPE_STRING "unsigned long long"
# else
# error No available "DOUBLE_TYPE"
# endif
#endif

int main(void) {
printf("sizeof(SINGLE_TYPE) is %d; sizeof(DOUBLE_TYPE) is %d\n",
(int)sizeof(SINGLE_TYPE), (int)sizeof(DOUBLE_TYPE));
printf("DOUBLE_TYPE is \"%s\"\n", DOUBLE_TYPE_STRING);
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)
Feb 18 '08 #1
4 2256
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_STRING "unsigned long"
#else
# if UINT_MAX < ULLONG_MAX / UINT_MAX
# define DOUBLE_TYPE unsigned long long
# define DOUBLE_TYPE_STRING "unsigned long long"
# else
# error No available "DOUBLE_TYPE"
# 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_TYPE) is %d\n",
(int)sizeof(SINGLE_TYPE), (int)sizeof(DOUBLE_TYPE));
printf("DOUBLE_TYPE is \"%s\"\n", DOUBLE_TYPE_STRING);
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.himplementations / wrappers
for non-C99 compilers, see e.g.
http://clc-wiki.net/wiki/stdint.h
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 18 '08 #2
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.himplementations / 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
Feb 18 '08 #3
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
Feb 18 '08 #4
Pedro Graca <he****@gmail.comwrites:
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.
Feb 19 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ron | last post by:
The C++ standard ISO/IEC 14882 specifies pp-numbers as follow: pp-number: digit .digit pp-number digit pp-number nondigit pp-number e sign pp-number E sign pp-number .
3
by: Ron | last post by:
Please, consider the following code: const int a = 1; #if a == 1 #define VAR 200 #else #define VAR 100 #endif int main() {
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
1
by: Alex Sedow | last post by:
Where to get/buy tool for partial preprocessing? 1. What I mean by partial macro preprocessing. This is mode in which almost sources (>99%) is not preprocessed. But some macros (setted by...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
4
by: Henrik Goldman | last post by:
I have an application which compiles on a number of different platforms. Now I'm trying to incorporate an external library which is only available to a subset of the platforms that my application...
11
by: andreyvul | last post by:
What I'm trying to do is have the preprocessor parse one file twice. The file has three parts, and each is dependent on the previous. Example (file name is foo.c): #ifndef ONCE /* first part */...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
40
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.