简体   繁体   中英

How to convert an integer to the shortest valid C99 function name string?

I am implementing a code generator that generates specific code for each linear algebra expression of the type x = y + w + z or A = B*C + D or whatever. Each expression is assigned with a unique unsigned long id, and at some point i need to wrap this id to a unique function name.

I have found a similar question but was unable to convert the solution to my problem, as I also have to write this code in C++, and as the valid url name are different from the valid C99function name. I don't really know where to start!

Edit : I indeed forgot to mention that a function can be assigned multiple 64bit IDs, so converting the ID to string and concatenating the strings may result in a function name too big to be handled by all compilers

Edit 2 : Okay I was probably not precise enough. I am generating OpenCL code, the generation has the purpose to remove temporaries and reduce kernel launch time. For some reason opencl is based on C99 but some compiles cannot handle very long kernels name ( i had some build errors because of that.) Considering all possibilities (scalartype, operators, inlined functions, etc...), 64bit for the ID is barely enough, but I think it's still enough

vec0 = vec1+vec2 has for example ID 1912142123
vec1 = vec0-vec2 has for example ID 3312098234
vec2 = vec0*scal has for example ID 329084089

These three operations can be put in the same kernel. I want to generate the kernel name for the generated code, and _1912142123_3312098234_329084089 might be too long for some compilers.

Hope it's more clear now

The following algorithm converts a number using n digits where n is the number of valid characters:

char *digs = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabzdefghijklmnopqrstuvwxyz_";
int n = strlen(digs);

long id = 1912142123;
char buffer[16];
int i = 0;
do
    buffer[i++] = digs[id%n];
while((id /= n)>0);
buffer[i] = 0;

the digs string should contain all valid characters. In this example '1912142123' will be converted to 'gZEzm1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM