c - Number and its logarithm as compile constants without manually changing both? -
i have constant width
power of 2, e.g. #define width 1024
.
i have associated #define width_binary_log 10
must change every time modify width
, binary logarithm of width
. undesirable forget so, , things consequently go haywire. unfortunately pow()
not available in macros.
usage
*z = zx >> width_binary_log
.
my plan fix this, given maximum possible value of width_binary_log
16
, following usage:
*z = zx >> binary_log(width)
with following defines:
#define binary_log_1 0 #define binary_log_2 1 #define binary_log_4 2 #define binary_log_8 3 #define binary_log_16 4 #define binary_log_32 5 #define binary_log_64 6 #define binary_log_128 7 #define binary_log_256 8 #define binary_log_512 9 #define binary_log_1024 10 #define binary_log_2048 11 #define binary_log_4096 12 #define binary_log_8192 13 #define binary_log_16384 14 #define binary_log_32768 15 #define binary_log_65536 16 #define binary_log(n) binary_log_##n
or
#define binary_log(n) binary_log_#n
however, gcc complains bitterly in each case. doing wrong? how others tackle problem, assuming way above unworthy?
it can done macros. used this answer in code:
#define width 2048 #define binary_log_1 0 #define binary_log_2 1 #define binary_log_4 2 #define binary_log_8 3 #define binary_log_16 4 #define binary_log_32 5 #define binary_log_64 6 #define binary_log_128 7 #define binary_log_256 8 #define binary_log_512 9 #define binary_log_1024 10 #define binary_log_2048 11 #define binary_log_4096 12 #define binary_log_8192 13 #define binary_log_16384 14 #define binary_log_32768 15 #define binary_log_65536 16 #define ppcat_nx(a, b) ## b #define binary_log(b) ppcat_nx(binary_log_, b) binary_log(width)
output of gcc -e test.c
(only macro substitution phase):
>gcc -e test.c # 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" # 24 "test.c" 11
Comments
Post a Comment