mojoshader_preprocessor.c
changeset 729 92cc053fbf75
parent 727 6978fac0fa31
child 746 6b368eeaa3fc
equal deleted inserted replaced
728:c37993f6f474 729:92cc053fbf75
   381 IMPLEMENT_POOL(Define, define)
   381 IMPLEMENT_POOL(Define, define)
   382 
   382 
   383 
   383 
   384 // Preprocessor define hashtable stuff...
   384 // Preprocessor define hashtable stuff...
   385 
   385 
   386 static unsigned char hash_define(const char *sym)
   386 // this is djb's xor hashing function.
   387 {
   387 static inline uint32 hash_string_djbxor(const char *sym)
   388     unsigned char retval = 0;
   388 {
       
   389     register uint32 hash = 5381;
   389     while (*sym)
   390     while (*sym)
   390         retval += *(sym++);
   391         hash = ((hash << 5) + hash) ^ *(sym++);
   391     return retval;
   392     return hash;
       
   393 } // hash_string_djbxor
       
   394 
       
   395 static inline uint8 hash_define(const char *sym)
       
   396 {
       
   397     return (uint8) hash_string_djbxor(sym);
   392 } // hash_define
   398 } // hash_define
   393 
   399 
   394 
   400 
   395 static int add_define(Context *ctx, const char *sym, const char *val,
   401 static int add_define(Context *ctx, const char *sym, const char *val,
   396                       char **parameters, unsigned int paramcount)
   402                       char **parameters, unsigned int paramcount)
   397 {
   403 {
   398     const unsigned char hash = hash_define(sym);
   404     const uint8 hash = hash_define(sym);
   399     Define *bucket = ctx->define_hashtable[hash];
   405     Define *bucket = ctx->define_hashtable[hash];
   400     while (bucket)
   406     while (bucket)
   401     {
   407     {
   402         if (strcmp(bucket->identifier, sym) == 0)
   408         if (strcmp(bucket->identifier, sym) == 0)
   403         {
   409         {
   433 } // free_define
   439 } // free_define
   434 
   440 
   435 
   441 
   436 static int remove_define(Context *ctx, const char *sym)
   442 static int remove_define(Context *ctx, const char *sym)
   437 {
   443 {
   438     const unsigned char hash = hash_define(sym);
   444     const uint8 hash = hash_define(sym);
   439     Define *bucket = ctx->define_hashtable[hash];
   445     Define *bucket = ctx->define_hashtable[hash];
   440     Define *prev = NULL;
   446     Define *prev = NULL;
   441     while (bucket)
   447     while (bucket)
   442     {
   448     {
   443         if (strcmp(bucket->identifier, sym) == 0)
   449         if (strcmp(bucket->identifier, sym) == 0)
   457 } // remove_define
   463 } // remove_define
   458 
   464 
   459 
   465 
   460 static const Define *find_define(Context *ctx, const char *sym)
   466 static const Define *find_define(Context *ctx, const char *sym)
   461 {
   467 {
   462     const unsigned char hash = hash_define(sym);
   468     const uint8 hash = hash_define(sym);
   463     Define *bucket = ctx->define_hashtable[hash];
   469     Define *bucket = ctx->define_hashtable[hash];
   464     while (bucket)
   470     while (bucket)
   465     {
   471     {
   466         if (strcmp(bucket->identifier, sym) == 0)
   472         if (strcmp(bucket->identifier, sym) == 0)
   467             return bucket;
   473             return bucket;