0

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

Community
  • 1
  • 1
Ragnar
  • 103
  • 6
  • 4
    Just use a prefix and the id as a string. – Mat Dec 30 '12 at 17:35
  • Actually, as far as I know, the maximum portable function length is 31 character. In my case multiple operations can be put in the same function, so that a function can be assigned multiple ID. If I use this method, I might go beyond the maximum function name too soon! – Ragnar Dec 30 '12 at 17:39
  • So an expression is assigned a unique ID, you need to wrap this ID to a unique function name, but a function can have multiple IDs? – Mat Dec 30 '12 at 17:42
  • An expression is assigned a unique ID, and a function can contain multiple expressions – Ragnar Dec 30 '12 at 17:43
  • Please give a short, concise and simple example. It isn't clear from your initial description what is mapped into what, when and why. For instance, what should be generated for `x`, `y` and `z=x+y`. – StoryTeller - Unslander Monica Dec 30 '12 at 17:46
  • 2
    Sounds like you're generating a nightmare :) Number the functions sequentially, and keep a map from that sequential number to your IDs. – Mat Dec 30 '12 at 17:46

1 Answers1

2

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'

CubeSchrauber
  • 1,199
  • 8
  • 9
  • 1
    This is unique at least for integers. The proof can be found here: http://www.proofwiki.org/wiki/Existence_of_Base-N_Representation – CubeSchrauber Dec 30 '12 at 18:13