Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test if two #defines are the same with the C preprocessor

I have a C program which has platform-specific defines for access to low-level hardware. On some platforms, two macros point to the same variable, on others they are different:

 //Platform_One.h
 #define FOO_PORT   (io.portA)
 #define BAR_PORT   (io.portB)

 //Platform_Two.h
 #define FOO_PORT   (io.portC)
 #define BAR_PORT   (io.portC)  //same

I have some initializer code that is different based on whether the #defines are the same or not. Conceptually, I'd like code like this:

 callback_struct_t callbacks[] = {
 #if FOO_PORT == BAR_PORT           //unfortunately invalid
     {&FOO_PORT, handle_foo_bar_func},
 #else
     {&FOO_PORT, handle_foo_func},
     {&BAR_PORT, handle_bar_func},
 #endif          
      {0,0}
 };

Is there a reliable way to test at compile time if two arbitrary macros have the same definition?

like image 924
AShelly Avatar asked Jul 08 '14 14:07

AShelly


People also ask

How do you know if two means are equal?

The two-sample t-test (also known as the independent samples t-test) is a method used to test whether the unknown population means of two groups are equal or not.

How do you test if two sets of data are significantly different?

A t-test is an inferential statistic used to determine if there is a statistically significant difference between the means of two variables. The t-test is a test used for hypothesis testing in statistics.

How do you know if a test is two tailed or not?

A one-tailed test has the entire 5% of the alpha level in one tail (in either the left, or the right tail). A two-tailed test splits your alpha level in half (as in the image to the left). Let's say you're working with the standard alpha level of 0.5 (5%). A two tailed test will have half of this (2.5%) in each tail.

How do you test two distributions?

The simplest way to compare two distributions is via the Z-test. The error in the mean is calculated by dividing the dispersion by the square root of the number of data points. In the above diagram, there is some population mean that is the true intrinsic mean value for that population.


1 Answers

You cannot compare the preprocessor macros as strings. One possibility would be to put the hardware port address (e.g., via another macro in the platform-specific headers) into the #defines and then compare the addresses.

However, the easiest way might be to do the comparison of addresses in actual code, e.g.:

if (&FOO_PORT == &BAR_PORT) {
    // populate callbacks with handle_foo_bar_func
} else {
    // populate callbacks with handle_foo_func and handle_bar_func
}

While not done in pre-processor, the compiler may be able to optimise away the unused branch since the hardware addresses are likely compile-time constants.

like image 138
Arkku Avatar answered Nov 15 '22 17:11

Arkku