C/C++ trick: static string hash generation

I am always interested in having the compiler do more things for me, without giving away code clarity or performance for the convenience. Today a colleague linked me to Pope Kim's Compile-Time Hash String Generation article which is a perfect example of the things I like: hidden syntactic sugar that does useful things.

Inline hash function

The goal: for a given hash function, write something like HASH_STRING("funny_bone") in the code, and have the compiler directly replace it with the result, 0xf1c6fd7f.

The solution: inline the function and hope that the compiler will be clever enough.

#include <string.h>
#define HASH(str) generateHash(str, strlen(str))

inline unsigned int generateHash(const char *string, size_t len)
    unsigned int hash = 0;
    for(size_t i = 0; i < len; ++i)
        hash = 65599 * hash + string[i];
    return hash ^ (hash >> 16);

Unfortunately Pope ran into several very problematic issues:

  • requires heavy optimisation flags (/O2 with Visual Studio, -O3 with g++)
  • limited to 10-character strings with Visual Studio
  • limited to 17-character strings with g++

I could personally reproduce the g++ limitations. I believe they are more related to loop unrolling limits than to the actual string size, but they indeed make the technique unusable in practice.

Macro-based hash function

If you read my previous article about C/C++ preprocessor LUT generation, you may remember that it used preprocessor tricks to do loop unrolling.

Hence the following implementation:

#include <string.h>
#include <stdint.h>
#include <stdio.h>

#define H1(s,i,x)   (x*65599u+(uint8_t)s[(i)<strlen(s)?strlen(s)-1-(i):strlen(s)])
#define H4(s,i,x)   H1(s,i,H1(s,i+1,H1(s,i+2,H1(s,i+3,x))))
#define H16(s,i,x)  H4(s,i,H4(s,i+4,H4(s,i+8,H4(s,i+12,x))))
#define H64(s,i,x)  H16(s,i,H16(s,i+16,H16(s,i+32,H16(s,i+48,x))))
#define H256(s,i,x) H64(s,i,H64(s,i+64,H64(s,i+128,H64(s,i+192,x))))

#define HASH(s)    ((uint32_t)(H256(s,0,0)^(H256(s,0,0)>>16)))

It has the following properties:

  • works in C in addition to C++
  • strings are always optimised away by gcc or g++ (but not always the computation itself)
  • hash computation is optimised away by gcc or g++ even with -O, -O1 or -Os
  • string size limit is 256 characters (probably more than enough for most uses) and can be manually increased or decreased

The following code:

int main(void)
    printf("%08x\n", HASH("funny_bone"));
    printf("%08x\n", HASH("incredibly_large_string_that_gcc_groks_easily"));

Is (correctly) optimised to this with gcc -Os:

  movl    $-238617217, %esi
  movl    $.LC0, %edi
  xorl    %eax, %eax
  call    printf
  movl    $-453669173, %esi
  movl    $.LC0, %edi
  xorl    %eax, %eax
  call    printf

I haven't tested it with Visual Studio. Feedback from this compiler would be very appreciated!

  • Posted: 2012-01-12 18:05 (Updated: 2012-01-12 18:07)
  • Author: sam
  • Categories: code tip


1. Pope -- 2012-01-12 18:53

Sorry i stole your thunder. But I am jealous you got a very cool blog address.. :D Check this out too. (see the history to see humus' way to make it work for even longer chars.. I tested up to 64 chars: all works great both with VS2010 and g++)


There are few usability issues especially a buffer like this: const char constBuffer[65] = "only3"; I'm trying to see if there's a walkaround :)

Also VS 2010 is not smart enough to know these two are same, thus not flattening down the first one with the Humus way(g++ is smart enough :D):


const char * const constBuffer = "rawr";
unsigned int hashValue = HASH_STRING_CONST(constBuffer);


unsigned int hashValue = HASH_STRING_CONST("rawr");
2. jj@notsharingmy.info -- 2012-01-12 19:20

I'm always enjoying your articles. You may have already seen this, but if not, this will be of interest to you. http://altdevblogaday.com/2011/10/27/quasi-compile-time-string-hashing/

4. pope -- 2012-01-12 19:50

sorry it did double posting.. i dunno why. it said the capcha thinks i'm spam so i did it.... and apparently the first one was there.. sorry delete once please :)

5. sam -- 2012-01-12 21:08

@Pope: that’s because I manually accepted your answer in the meantime; we must have been only a few seconds away :)

Thank you for the very interesting link. Using the fact that char foo[5] and char foo[6] are actually different arguments is very clever.

As for the const char constBuffer[65] = "only3"; case, I think it can be solved by slightly modifying the hash function so that it ignores characters after the first \0.

@jj: thanks! That’s a really superior implementation. If one doesn’t care about C support, that’s really the way to go.

6. yzk0370@gmail.com -- 2013-06-17 19:13

Hi, I am working on the similar things on vs2012. I have made the msvc compiler to put a hashed number directly as an asm operand just as your example. However, I still could find the plain text in the executable binary. I suppose the plain text should no longer be useful because it has already been hashed and replaced. Do you have any idea to omit the plain text from the binary totally?

7. Anonymous -- 2015-03-13 18:32

Nice write up. I'm having trouble using the HASH to initialize a static look up table. I looked over your LUT article, and this should be able to handle it.

uint32_t hashes[] = {



results in errors: initializer element is not constant ... for all.

I've tried with gcc 4.8 & 4.6 with all three -O options.

Any thing I'm doing wrong? Thoughts? Changes to gcc that might have broken this?

Thanks in advance.

8. Lukas -- 2015-11-05 10:00

I take your idea as an inspiration for creating a fully static HASH function (by taking out the string length functions to). Have a look at http://www.heeden.nl/statichashc.htm This even allow you to use the HASH value in a switch-case construction on a strict ANSI C compiler.

9. anonymous -- 2016-07-07 20:42

If the string are same, but one was constant and generated in compile time, another generated via a variable. they have different value. Why?

296. subanal -- 2016-11-27 02:08

yEDI5X I truly appreciate this post. I have been looking all over for this! Thank God I found it on Google. You have made my day! Thanks again..

337. mike tyson -- 2017-03-04 14:50

5fjnQy wow, awesome article post.Really looking forward to read more. Great.

338. anonymous -- 2017-11-11 06:28

Thanks a lot for the post. http://mysnaphack.com

339. mike -- 2018-01-15 14:00

You have posted very nice tutorial which i like reading. I must say you have made a great effort in making this post. I will come back to read more tutorials. Buy rubber matting from http://rubbermattingexperts.co.uk/rubber-matting.html

340. Eleston -- 2018-01-17 12:22

I am very happy to visit in the post. I see in the post some C/C++ trick: static string hash generation working for him. I am appreciate in the work. keep it up. Click here http://rubbertiles-uk.co.uk/

341. <a href="https://www.gulfdegreeonline.org/mba-in-dubai/">MBA In Dubai</a> -- 2018-01-19 12:21

Usually! C and C++ are easy to remember rather than to dot net and IOS because these syntaxes is not to remember learner have to do more practice than remembering it, if he/she does not connect the current trend so they will not stay in the market because the everyday trend has been changed in designing and development too!

342. anonymous -- 2018-01-22 04:46

The purpose of getting support in computer education as it works for ideal support for needy acts. The matter of support is for good and ideal needy support. This seen in site https://www.essayuniverse.net/ for good use.

343. davidmiky -- 2018-03-17 08:13

What a amazing post shared here. I really love this website, I would like to say thanks for sharing such great post. I would like to say bundle of thanks for sharing. test4actual 210-260

344. anonymous -- 2018-03-25 13:20

After reading this my takeaway is https://paperwritingservice.reviews/penmypaper-review/ you have to stand up to life and it’s many obstacles that are personal to you. We all have challenges and struggles but fighting back and seeing a different perspective is all we need to find the resolve to overcome.

345. anonymous -- 2018-04-28 08:17

I appreciate your efforts in preparing this post. I really like your blog articles. https://caseanswers.xyz/product-red-a-case-study-solution-%26-analysis/ Amazing article thanks or sharing.. (PRODUCT) RED (A) Case Study Solution & Analysis

346. anonymous -- 2018-04-28 08:35

Dissertation Guidance Provides quality Online Dissertation Help for students. This a good way to appreciate the teacher as they put their efforts to train students. https://casegurus.com/hubspot-inbound-marketing-and-web-20-166 UK dissertation Writers appreciates the teachers. HubSpot: Inbound Marketing and Web 2.0 Case Study Solution

347. anonymous -- 2018-04-28 11:31

Dissertation Guidance Provides quality Online Dissertation Help for students. https://www.casequiz.com/helicopter-investigation-24333/ Amazing article thanks or sharing.. Helicopter Investigation Case Study Solution & Analysis

348. anonymous -- 2018-04-28 12:16

I personally like your post; you have shared good insights and experiences. https://www.examonlinehelp.xyz/take-online-english-quiz-21338 Keep it up. Do My Online English Quiz

349. anonymous -- 2018-04-28 12:53

Really i appreciate the effort you made to share the knowledge. This is really a great stuff for sharing. https://www.finance-assignments.com/decision-making-3072 Keep it up . Thanks for sharing. Decision Making Assignment Help

350. anonymous -- 2018-04-28 13:27

This was a great and interesting article to read. I have really enjoyed all of this very cool information https://www.homeworkaustralia.com/dental-assignment-help-21522 Dental Homework Help Australia

351. anonymous -- 2018-04-28 13:58

Things are very open and intensely clear explanation of issues. was truly information. http://phphelponline.com/php-mysql-assignment-help-12779 Your website is very beneficial. Php MySQL Project Help

352. anonymous -- 2018-04-28 14:24

Such a nice post, keep up the fantastic work https://structuredsettlementscompanies.xyz/ This site and the resources you provide is really nice keep it up. structured settlement companies boca raton

353. Assignment Help -- 2018-05-11 10:49

When you must switch to the World Wide Web to search for some Assignment help. Our high expert's professionals who have extensive experience in writing assignment will not only make you avail with the quality assignment, but you will also get plagiarism free assignment within the given time. https://www.allassignmenthelp.com/

354. anonymous -- 2018-06-09 07:44
355. oberoi.aishaa@gmail.com -- 2018-06-13 12:01

Commendable job with the article! It was really informative and enriching. Looking forward to more such posts in the future. Keep us updated with what’s in store! Will surely keep frequenting this website. <a href=https://www.makemyassignments.com/nz/accounting-assignment-help/ >Accounting Assignment Help</a>

356. Growcode -- 2018-06-27 13:28

It is very important that what you write.


357. Gloria -- 2018-07-05 09:24

Thanks for sharing this useful information https://grammarchecker.net/, https://www.mp3park.com/

358. Myra -- 2018-07-20 08:33

Not educating girls or creating barriers in their school education globally costs between USD 15 trillion to USD 30 trillion, the World Bank said https://getschoolsupplieslist.com/major-school-problems

359. Cucumber Juice -- 2018-07-25 14:01

It is the intent to provide valuable information and best practices, including an understanding of the regulatory process. Cucumber Juice

360. Aloe Vera Juice -- 2018-07-25 14:09

What a really awesome post this is. Truly, one of the best posts I've ever witnessed to see in my whole life. Wow, just keep it up. Aloe Vera Juice

361. Orange Juice -- 2018-07-25 14:11

I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. Orange Juice

362. Garlic Juice -- 2018-07-25 14:12

Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well. Garlic Juice

363. Banana Juice -- 2018-07-25 14:13

Thank you for the post. I will definitely comeback. Banana Juice

364. Lemon Juice -- 2018-07-25 14:14

I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. Lemon Juice

365. Lemon Water -- 2018-07-25 14:15

this blog was really great, never seen a great blog like this before. i think im gonna share this to my friends.. Lemon Water

366. Lemon Balm -- 2018-07-25 14:17

Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning Lemon Balm

367. anonymous -- 2018-07-26 10:30

Je vous remercie de l'information! Je cherchais et ne pouvait pas trouver. Vous me aidé! <a href="https://19216801.mobi">192.168 0.1 login</a>

368. anonymous -- 2018-07-26 10:30
369. anonymous -- 2018-07-29 14:16

<a href="https://vakilmodafe24.ir/">vakilmodafe24</a>

370. anonymous -- 2018-07-30 11:02

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! un curso de milagros https://un-curso-en-milagros.org/

371. anonymous -- 2018-07-30 17:32

Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot. lessons online https://acim.biz/

372. anonymous -- 2018-07-31 12:23

This is actually the kind of information I have been trying to find. Thank you for writing this information. non duality teachers http://www.nonduality.xyz/

373. anonymous -- 2018-07-31 19:33

Find another content, try to learn something different. Why don't learn about photography? http://leoabreu.com/

374. anonymous -- 2018-07-31 22:57

Which is headquartered in Atlanta Store Support Center, Cobb Country, Georgia, United States and the Home Depot is the largest retailer in the United States. http://customersurvey.xyz/homedepot-survey/

375. Neha Baluni -- 2018-08-01 16:05

Good news. Appreciate this post. Thank you for compiling and sharing it. Get the latest news headlines on recent changes of google, google update, google device and all latest google news https://www.mobileappdaily.com/news/google

376. anonymous -- 2018-08-01 16:12

You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing. www.cinderconeclaycenter.com http://www.cinderconeclaycenter.com

377. anonymous -- 2018-08-07 08:30

Nice post I like this post:- https://a-to-z-sms.blogspot.com

378. anonymous -- 2018-08-07 18:49

I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information. suzuki coin https://suzukicoin.net/

379. Lisa -- 2018-08-19 10:25

Perowskit-Solarzellen sind die Hoffnungsträger der Photovoltaik. Sie sind günstig, leicht herzustellen und äußerst effizient. Aber erst im https://creditcardsbill.com/best-credit-cards https://diephotovoltaik.de/solarthermie-anlage

380. Grammarexpert12 -- 2018-08-22 12:25

I am looking for some codes for my project for solution. I got this code and my problem solved visit this website for grammar check https://www.grammarlookup.com

381. anonymous -- 2018-08-31 18:34
382. Tom -- 2018-09-26 08:59

When you must switch to the World Wide Web to search for some Assignment help. I like this post that amazing and interesting. Thanks for share! Visit my home: http://2quotes.net/life-quotes/quotes-about-time-passing-too-fast-dont-waste-time.html

383. anonymous -- 2018-10-15 13:08

Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. Enjoy Mexico top desinations https://enjoymexico.net/top-mexico-destinations/

384. anonymous -- 2018-10-16 09:56

The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. Enjoy Mexico Villa Rentals in Punta Mita https://enjoymexico.net/punta-mita-villa-rentals/

385. anonymous -- 2018-10-17 10:10

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work Enjoy Mexico Villa Rentals in Cabo San Lucas https://enjoymexico.net/villa-rentals-cabo-san-lucas/

386. anonymous -- 2018-10-17 20:08

Thanks for posting!

-- https://www.opwindowashing.com

387. anonymous -- 2018-10-18 08:37

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. iphoneattunlock http://www.iphoneattunlock.com/

388. anonymous -- 2018-10-20 09:45

My Essay author is the cross-to source for the final contact of academic assignments. If you are screaming out “Write my essay for me,” you’ve come to the maximum elite essay writing provider. I am definitely enjoying your website. You actually have a few brilliant perception and terrific recollections. <a href="https://sherrika08.wixsite.com/webdevelopment">Shape Globe software house</a>

389. anonymous -- 2018-10-20 09:46

I am definitely enjoying your website. You actually have a few brilliant perceptions and terrific recollections. If you are screaming out “Write my essay for me,” you’ve come to the maximum elite essay writing provider.[url=https://sherrika08.wixsite.com/webdevelopment]Shape Globe software houseurl

390. anonymous -- 2018-10-20 13:37

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information... The Hyde http://thehydeofficial.bravesites.com/

391. anonymous -- 2018-10-31 16:17

Thank you for sharing <a href="https://vakil.biz/%d8%b7%d9%84%d8%a7%d9%82/">vakil.biz</a> |

392. anonymous -- 2018-11-05 11:02

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. Nicole Bray https://www.nicolebrayrealestate.com

393. anonymous -- 2018-11-08 11:19

Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Home Health Care Boca Raton http://homehealthcarebocaraton.com

394. anonymous -- 2018-11-08 19:39

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! https://writemyessay.info/

395. anonymous -- 2018-11-10 17:56

Download Happy Glass 2.0 https://happy-glass.com/

396. anonymous -- 2018-11-12 10:30

Nice to read your article! I am looking forward to sharing your adventures and experiences. Bücher für plastische Chirurgie https://www.books-plastic-surgery.com/

Add New Comment