seanhalle@25: /* seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: lookup3.c, by Bob Jenkins, May 2006, Public Domain. seanhalle@25: seanhalle@25: These are functions for producing 32-bit hashes for hash table lookup. seanhalle@25: hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() seanhalle@25: are externally useful functions. Routines to test the hash are included seanhalle@25: if SELF_TEST is defined. You can use this free for any purpose. It's in seanhalle@25: the public domain. It has no warranty. seanhalle@25: seanhalle@25: You probably want to use hashlittle(). hashlittle() and hashbig() seanhalle@25: hash byte arrays. hashlittle() is is faster than hashbig() on seanhalle@25: little-endian machines. Intel and AMD are little-endian machines. seanhalle@25: On second thought, you probably want hashlittle2(), which is identical to seanhalle@25: hashlittle() except it returns two 32-bit hashes for the price of one. seanhalle@25: You could implement hashbig2() if you wanted but I haven't bothered here. seanhalle@25: seanhalle@25: If you want to find a hash of, say, exactly 7 integers, do seanhalle@25: a = i1; b = i2; c = i3; seanhalle@25: mix(a,b,c); seanhalle@25: a += i4; b += i5; c += i6; seanhalle@25: mix(a,b,c); seanhalle@25: a += i7; seanhalle@25: final(a,b,c); seanhalle@25: then use c as the hash value. If you have a variable length array of seanhalle@25: 4-byte integers to hash, use hashword(). If you have a byte array (like seanhalle@25: a character string), use hashlittle(). If you have several byte arrays, or seanhalle@25: a mix of things, see the comments above hashlittle(). seanhalle@25: seanhalle@25: Why is this so big? I read 12 bytes at a time into 3 4-byte integers, seanhalle@25: then mix those integers. This is fast (you can do a lot more thorough seanhalle@25: mixing with 12*3 instructions on 3 integers than you can with 3 instructions seanhalle@25: on 1 byte), but shoehorning those bytes into integers efficiently is messy. seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: */ seanhalle@25: //#define SELF_TEST 1 seanhalle@25: seanhalle@25: #include /* defines printf for tests */ seanhalle@25: #include /* defines time_t for timings in the test */ seanhalle@25: #include /* defines uint32_t etc */ seanhalle@25: #include /* attempt to define endianness */ seanhalle@25: #ifdef linux seanhalle@25: # include /* attempt to define endianness */ seanhalle@25: #endif seanhalle@25: seanhalle@25: /* seanhalle@25: * My best guess at if you are big-endian or little-endian. This may seanhalle@25: * need adjustment. seanhalle@25: */ seanhalle@25: #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ seanhalle@25: __BYTE_ORDER == __LITTLE_ENDIAN) || \ seanhalle@25: (defined(i386) || defined(__i386__) || defined(__i486__) || \ seanhalle@25: defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) seanhalle@25: # define HASH_LITTLE_ENDIAN 1 seanhalle@25: # define HASH_BIG_ENDIAN 0 seanhalle@25: #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ seanhalle@25: __BYTE_ORDER == __BIG_ENDIAN) || \ seanhalle@25: (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) seanhalle@25: # define HASH_LITTLE_ENDIAN 0 seanhalle@25: # define HASH_BIG_ENDIAN 1 seanhalle@25: #else seanhalle@25: # define HASH_LITTLE_ENDIAN 0 seanhalle@25: # define HASH_BIG_ENDIAN 0 seanhalle@25: #endif seanhalle@25: seanhalle@25: #define hashsize(n) ((uint32_t)1<<(n)) seanhalle@25: #define hashmask(n) (hashsize(n)-1) seanhalle@25: #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) seanhalle@25: seanhalle@25: /* seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: mix -- mix 3 32-bit values reversibly. seanhalle@25: seanhalle@25: This is reversible, so any information in (a,b,c) before mix() is seanhalle@25: still in (a,b,c) after mix(). seanhalle@25: seanhalle@25: If four pairs of (a,b,c) inputs are run through mix(), or through seanhalle@25: mix() in reverse, there are at least 32 bits of the output that seanhalle@25: are sometimes the same for one pair and different for another pair. seanhalle@25: This was tested for: seanhalle@25: * pairs that differed by one bit, by two bits, in any combination seanhalle@25: of top bits of (a,b,c), or in any combination of bottom bits of seanhalle@25: (a,b,c). seanhalle@25: * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed seanhalle@25: the output delta to a Gray code (a^(a>>1)) so a string of 1's (as seanhalle@25: is commonly produced by subtraction) look like a single 1-bit seanhalle@25: difference. seanhalle@25: * the base values were pseudorandom, all zero but one bit set, or seanhalle@25: all zero plus a counter that starts at zero. seanhalle@25: seanhalle@25: Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that seanhalle@25: satisfy this are seanhalle@25: 4 6 8 16 19 4 seanhalle@25: 9 15 3 18 27 15 seanhalle@25: 14 9 3 7 17 3 seanhalle@25: Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing seanhalle@25: for "differ" defined as + with a one-bit base and a two-bit delta. I seanhalle@25: used http://burtleburtle.net/bob/hash/avalanche.html to choose seanhalle@25: the operations, constants, and arrangements of the variables. seanhalle@25: seanhalle@25: This does not achieve avalanche. There are input bits of (a,b,c) seanhalle@25: that fail to affect some output bits of (a,b,c), especially of a. The seanhalle@25: most thoroughly mixed value is c, but it doesn't really even achieve seanhalle@25: avalanche in c. seanhalle@25: seanhalle@25: This allows some parallelism. Read-after-writes are good at doubling seanhalle@25: the number of bits affected, so the goal of mixing pulls in the opposite seanhalle@25: direction as the goal of parallelism. I did what I could. Rotates seanhalle@25: seem to cost as much as shifts on every machine I could lay my hands seanhalle@25: on, and rotates are much kinder to the top and bottom bits, so I used seanhalle@25: rotates. seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: */ seanhalle@25: #define mix(a,b,c) \ seanhalle@25: { \ seanhalle@25: a -= c; a ^= rot(c, 4); c += b; \ seanhalle@25: b -= a; b ^= rot(a, 6); a += c; \ seanhalle@25: c -= b; c ^= rot(b, 8); b += a; \ seanhalle@25: a -= c; a ^= rot(c,16); c += b; \ seanhalle@25: b -= a; b ^= rot(a,19); a += c; \ seanhalle@25: c -= b; c ^= rot(b, 4); b += a; \ seanhalle@25: } seanhalle@25: seanhalle@25: /* seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: final -- final mixing of 3 32-bit values (a,b,c) into c seanhalle@25: seanhalle@25: Pairs of (a,b,c) values differing in only a few bits will usually seanhalle@25: produce values of c that look totally different. This was tested for seanhalle@25: * pairs that differed by one bit, by two bits, in any combination seanhalle@25: of top bits of (a,b,c), or in any combination of bottom bits of seanhalle@25: (a,b,c). seanhalle@25: * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed seanhalle@25: the output delta to a Gray code (a^(a>>1)) so a string of 1's (as seanhalle@25: is commonly produced by subtraction) look like a single 1-bit seanhalle@25: difference. seanhalle@25: * the base values were pseudorandom, all zero but one bit set, or seanhalle@25: all zero plus a counter that starts at zero. seanhalle@25: seanhalle@25: These constants passed: seanhalle@25: 14 11 25 16 4 14 24 seanhalle@25: 12 14 25 16 4 14 24 seanhalle@25: and these came close: seanhalle@25: 4 8 15 26 3 22 24 seanhalle@25: 10 8 15 26 3 22 24 seanhalle@25: 11 8 15 26 3 22 24 seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: */ seanhalle@25: #define final(a,b,c) \ seanhalle@25: { \ seanhalle@25: c ^= b; c -= rot(b,14); \ seanhalle@25: a ^= c; a -= rot(c,11); \ seanhalle@25: b ^= a; b -= rot(a,25); \ seanhalle@25: c ^= b; c -= rot(b,16); \ seanhalle@25: a ^= c; a -= rot(c,4); \ seanhalle@25: b ^= a; b -= rot(a,14); \ seanhalle@25: c ^= b; c -= rot(b,24); \ seanhalle@25: } seanhalle@25: seanhalle@25: /* seanhalle@25: -------------------------------------------------------------------- seanhalle@25: This works on all machines. To be useful, it requires seanhalle@25: -- that the key be an array of uint32_t's, and seanhalle@25: -- that the length be the number of uint32_t's in the key seanhalle@25: seanhalle@25: The function hashword() is identical to hashlittle() on little-endian seanhalle@25: machines, and identical to hashbig() on big-endian machines, seanhalle@25: except that the length has to be measured in uint32_ts rather than in seanhalle@25: bytes. hashlittle() is more complicated than hashword() only because seanhalle@25: hashlittle() has to dance around fitting the key bytes into registers. seanhalle@25: -------------------------------------------------------------------- seanhalle@25: */ seanhalle@25: inline uint32_t seanhalle@25: jenkHash32( seanhalle@25: const uint32_t *k, /* the key, an array of uint32_t values */ seanhalle@25: size_t length) /* the length of the key, in uint32_ts */ seanhalle@25: { seanhalle@25: uint32_t a,b,c; seanhalle@25: seanhalle@25: /* Set up the internal state */ seanhalle@25: a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + 0x7291f8a3;//arbitrary seanhalle@25: seanhalle@25: /*------------------------------------------------- handle most of the key */ seanhalle@25: while (length > 3) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: b += k[1]; seanhalle@25: c += k[2]; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 3; seanhalle@25: k += 3; seanhalle@25: } seanhalle@25: seanhalle@25: /*------------------------------------------- handle the last 3 uint32_t's */ seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 3 : c+=k[2]; seanhalle@25: case 2 : b+=k[1]; seanhalle@25: case 1 : a+=k[0]; seanhalle@25: final(a,b,c); seanhalle@25: case 0: /* case 0: nothing left to add */ seanhalle@25: break; seanhalle@25: } seanhalle@25: /*------------------------------------------------------ report the result */ seanhalle@25: return c; seanhalle@25: } seanhalle@25: seanhalle@25: seanhalle@25: /* seanhalle@25: -------------------------------------------------------------------- seanhalle@25: hashword2() -- same as hashword(), but take two seeds and return two seanhalle@25: 32-bit values. pc and pb must both be nonnull, and *pc and *pb must seanhalle@25: both be initialized with seeds. If you pass in (*pb)==0, the output seanhalle@25: (*pc) will be the same as the return value from hashword(). seanhalle@25: -------------------------------------------------------------------- seanhalle@25: */ seanhalle@25: void hashword2 ( seanhalle@25: const uint32_t *k, /* the key, an array of uint32_t values */ seanhalle@25: size_t length, /* the length of the key, in uint32_ts */ seanhalle@25: uint32_t *pc, /* IN: seed OUT: primary hash value */ seanhalle@25: uint32_t *pb) /* IN: more seed OUT: secondary hash value */ seanhalle@25: { seanhalle@25: uint32_t a,b,c; seanhalle@25: seanhalle@25: /* Set up the internal state */ seanhalle@25: a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc; seanhalle@25: c += *pb; seanhalle@25: seanhalle@25: /*------------------------------------------------- handle most of the key */ seanhalle@25: while (length > 3) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: b += k[1]; seanhalle@25: c += k[2]; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 3; seanhalle@25: k += 3; seanhalle@25: } seanhalle@25: seanhalle@25: /*------------------------------------------- handle the last 3 uint32_t's */ seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 3 : c+=k[2]; seanhalle@25: case 2 : b+=k[1]; seanhalle@25: case 1 : a+=k[0]; seanhalle@25: final(a,b,c); seanhalle@25: case 0: /* case 0: nothing left to add */ seanhalle@25: break; seanhalle@25: } seanhalle@25: /*------------------------------------------------------ report the result */ seanhalle@25: *pc=c; *pb=b; seanhalle@25: } seanhalle@25: seanhalle@25: seanhalle@25: /* seanhalle@25: ------------------------------------------------------------------------------- seanhalle@25: hashlittle() -- hash a variable-length key into a 32-bit value seanhalle@25: k : the key (the unaligned variable-length array of bytes) seanhalle@25: length : the length of the key, counting by bytes seanhalle@25: initval : can be any 4-byte value seanhalle@25: Returns a 32-bit value. Every bit of the key affects every bit of seanhalle@25: the return value. Two keys differing by one or two bits will have seanhalle@25: totally different hash values. seanhalle@25: seanhalle@25: The best hash table sizes are powers of 2. There is no need to do seanhalle@25: mod a prime (mod is sooo slow!). If you need less than 32 bits, seanhalle@25: use a bitmask. For example, if you need only 10 bits, do seanhalle@25: h = (h & hashmask(10)); seanhalle@25: In which case, the hash table should have hashsize(10) elements. seanhalle@25: seanhalle@25: If you are hashing n strings (uint8_t **)k, do it like this: seanhalle@25: for (i=0, h=0; i 12) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: b += k[1]; seanhalle@25: c += k[2]; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 3; seanhalle@25: } seanhalle@25: seanhalle@25: /*----------------------------- handle the last (probably partial) block */ seanhalle@25: /* seanhalle@25: * "k[2]&0xffffff" actually reads beyond the end of the string, but seanhalle@25: * then masks off the part it's not allowed to read. Because the seanhalle@25: * string is aligned, the masked-off tail is in the same word as the seanhalle@25: * rest of the string. Every machine with memory protection I've seen seanhalle@25: * does it on word boundaries, so is OK with this. But VALGRIND will seanhalle@25: * still catch it and complain. The masking trick does make the hash seanhalle@25: * noticably faster for short strings (like English words). seanhalle@25: */ seanhalle@25: #ifndef VALGRIND seanhalle@25: seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=k[1]&0xffffff; a+=k[0]; break; seanhalle@25: case 6 : b+=k[1]&0xffff; a+=k[0]; break; seanhalle@25: case 5 : b+=k[1]&0xff; a+=k[0]; break; seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=k[0]&0xffffff; break; seanhalle@25: case 2 : a+=k[0]&0xffff; break; seanhalle@25: case 1 : a+=k[0]&0xff; break; seanhalle@25: case 0 : return c; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: #else /* make valgrind happy */ seanhalle@25: seanhalle@25: k8 = (const uint8_t *)k; seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ seanhalle@25: case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ seanhalle@25: case 9 : c+=k8[8]; /* fall through */ seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ seanhalle@25: case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ seanhalle@25: case 5 : b+=k8[4]; /* fall through */ seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ seanhalle@25: case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ seanhalle@25: case 1 : a+=k8[0]; break; seanhalle@25: case 0 : return c; seanhalle@25: } seanhalle@25: seanhalle@25: #endif /* !valgrind */ seanhalle@25: seanhalle@25: } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { seanhalle@25: const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ seanhalle@25: const uint8_t *k8; seanhalle@25: seanhalle@25: /*--------------- all but last block: aligned reads and different mixing */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0] + (((uint32_t)k[1])<<16); seanhalle@25: b += k[2] + (((uint32_t)k[3])<<16); seanhalle@25: c += k[4] + (((uint32_t)k[5])<<16); seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 6; seanhalle@25: } seanhalle@25: seanhalle@25: /*----------------------------- handle the last (probably partial) block */ seanhalle@25: k8 = (const uint8_t *)k; seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[4]+(((uint32_t)k[5])<<16); seanhalle@25: b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ seanhalle@25: case 10: c+=k[4]; seanhalle@25: b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 9 : c+=k8[8]; /* fall through */ seanhalle@25: case 8 : b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ seanhalle@25: case 6 : b+=k[2]; seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 5 : b+=k8[4]; /* fall through */ seanhalle@25: case 4 : a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ seanhalle@25: case 2 : a+=k[0]; seanhalle@25: break; seanhalle@25: case 1 : a+=k8[0]; seanhalle@25: break; seanhalle@25: case 0 : return c; /* zero length requires no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: } else { /* need to read the key one byte at a time */ seanhalle@25: const uint8_t *k = (const uint8_t *)key; seanhalle@25: seanhalle@25: /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: a += ((uint32_t)k[1])<<8; seanhalle@25: a += ((uint32_t)k[2])<<16; seanhalle@25: a += ((uint32_t)k[3])<<24; seanhalle@25: b += k[4]; seanhalle@25: b += ((uint32_t)k[5])<<8; seanhalle@25: b += ((uint32_t)k[6])<<16; seanhalle@25: b += ((uint32_t)k[7])<<24; seanhalle@25: c += k[8]; seanhalle@25: c += ((uint32_t)k[9])<<8; seanhalle@25: c += ((uint32_t)k[10])<<16; seanhalle@25: c += ((uint32_t)k[11])<<24; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 12; seanhalle@25: } seanhalle@25: seanhalle@25: /*-------------------------------- last block: affect all 32 bits of (c) */ seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 12: c+=((uint32_t)k[11])<<24; seanhalle@25: case 11: c+=((uint32_t)k[10])<<16; seanhalle@25: case 10: c+=((uint32_t)k[9])<<8; seanhalle@25: case 9 : c+=k[8]; seanhalle@25: case 8 : b+=((uint32_t)k[7])<<24; seanhalle@25: case 7 : b+=((uint32_t)k[6])<<16; seanhalle@25: case 6 : b+=((uint32_t)k[5])<<8; seanhalle@25: case 5 : b+=k[4]; seanhalle@25: case 4 : a+=((uint32_t)k[3])<<24; seanhalle@25: case 3 : a+=((uint32_t)k[2])<<16; seanhalle@25: case 2 : a+=((uint32_t)k[1])<<8; seanhalle@25: case 1 : a+=k[0]; seanhalle@25: break; seanhalle@25: case 0 : return c; seanhalle@25: } seanhalle@25: } seanhalle@25: seanhalle@25: final(a,b,c); seanhalle@25: return c; seanhalle@25: } seanhalle@25: seanhalle@25: seanhalle@25: /* seanhalle@25: * hashlittle2: return 2 32-bit hash values seanhalle@25: * seanhalle@25: * This is identical to hashlittle(), except it returns two 32-bit hash seanhalle@25: * values instead of just one. This is good enough for hash table seanhalle@25: * lookup with 2^^64 buckets, or if you want a second hash if you're not seanhalle@25: * happy with the first, or if you want a probably-unique 64-bit ID for seanhalle@25: * the key. *pc is better mixed than *pb, so use *pc first. If you want seanhalle@25: * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)". seanhalle@25: */ seanhalle@25: void hashlittle2( seanhalle@25: const void *key, /* the key to hash */ seanhalle@25: size_t length, /* length of the key */ seanhalle@25: uint32_t *pc, /* IN: primary initval, OUT: primary hash */ seanhalle@25: uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */ seanhalle@25: { seanhalle@25: uint32_t a,b,c; /* internal state */ seanhalle@25: union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ seanhalle@25: seanhalle@25: /* Set up the internal state */ seanhalle@25: a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc; seanhalle@25: c += *pb; seanhalle@25: seanhalle@25: u.ptr = key; seanhalle@25: if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { seanhalle@25: const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ seanhalle@25: const uint8_t *k8; seanhalle@25: seanhalle@25: /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: b += k[1]; seanhalle@25: c += k[2]; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 3; seanhalle@25: } seanhalle@25: seanhalle@25: /*----------------------------- handle the last (probably partial) block */ seanhalle@25: /* seanhalle@25: * "k[2]&0xffffff" actually reads beyond the end of the string, but seanhalle@25: * then masks off the part it's not allowed to read. Because the seanhalle@25: * string is aligned, the masked-off tail is in the same word as the seanhalle@25: * rest of the string. Every machine with memory protection I've seen seanhalle@25: * does it on word boundaries, so is OK with this. But VALGRIND will seanhalle@25: * still catch it and complain. The masking trick does make the hash seanhalle@25: * noticably faster for short strings (like English words). seanhalle@25: */ seanhalle@25: #ifndef VALGRIND seanhalle@25: seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=k[1]&0xffffff; a+=k[0]; break; seanhalle@25: case 6 : b+=k[1]&0xffff; a+=k[0]; break; seanhalle@25: case 5 : b+=k[1]&0xff; a+=k[0]; break; seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=k[0]&0xffffff; break; seanhalle@25: case 2 : a+=k[0]&0xffff; break; seanhalle@25: case 1 : a+=k[0]&0xff; break; seanhalle@25: case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: #else /* make valgrind happy */ seanhalle@25: seanhalle@25: k8 = (const uint8_t *)k; seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ seanhalle@25: case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ seanhalle@25: case 9 : c+=k8[8]; /* fall through */ seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ seanhalle@25: case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ seanhalle@25: case 5 : b+=k8[4]; /* fall through */ seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ seanhalle@25: case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ seanhalle@25: case 1 : a+=k8[0]; break; seanhalle@25: case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: #endif /* !valgrind */ seanhalle@25: seanhalle@25: } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { seanhalle@25: const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ seanhalle@25: const uint8_t *k8; seanhalle@25: seanhalle@25: /*--------------- all but last block: aligned reads and different mixing */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0] + (((uint32_t)k[1])<<16); seanhalle@25: b += k[2] + (((uint32_t)k[3])<<16); seanhalle@25: c += k[4] + (((uint32_t)k[5])<<16); seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 6; seanhalle@25: } seanhalle@25: seanhalle@25: /*----------------------------- handle the last (probably partial) block */ seanhalle@25: k8 = (const uint8_t *)k; seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[4]+(((uint32_t)k[5])<<16); seanhalle@25: b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ seanhalle@25: case 10: c+=k[4]; seanhalle@25: b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 9 : c+=k8[8]; /* fall through */ seanhalle@25: case 8 : b+=k[2]+(((uint32_t)k[3])<<16); seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ seanhalle@25: case 6 : b+=k[2]; seanhalle@25: a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 5 : b+=k8[4]; /* fall through */ seanhalle@25: case 4 : a+=k[0]+(((uint32_t)k[1])<<16); seanhalle@25: break; seanhalle@25: case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ seanhalle@25: case 2 : a+=k[0]; seanhalle@25: break; seanhalle@25: case 1 : a+=k8[0]; seanhalle@25: break; seanhalle@25: case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: } else { /* need to read the key one byte at a time */ seanhalle@25: const uint8_t *k = (const uint8_t *)key; seanhalle@25: seanhalle@25: /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: a += ((uint32_t)k[1])<<8; seanhalle@25: a += ((uint32_t)k[2])<<16; seanhalle@25: a += ((uint32_t)k[3])<<24; seanhalle@25: b += k[4]; seanhalle@25: b += ((uint32_t)k[5])<<8; seanhalle@25: b += ((uint32_t)k[6])<<16; seanhalle@25: b += ((uint32_t)k[7])<<24; seanhalle@25: c += k[8]; seanhalle@25: c += ((uint32_t)k[9])<<8; seanhalle@25: c += ((uint32_t)k[10])<<16; seanhalle@25: c += ((uint32_t)k[11])<<24; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 12; seanhalle@25: } seanhalle@25: seanhalle@25: /*-------------------------------- last block: affect all 32 bits of (c) */ seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 12: c+=((uint32_t)k[11])<<24; seanhalle@25: case 11: c+=((uint32_t)k[10])<<16; seanhalle@25: case 10: c+=((uint32_t)k[9])<<8; seanhalle@25: case 9 : c+=k[8]; seanhalle@25: case 8 : b+=((uint32_t)k[7])<<24; seanhalle@25: case 7 : b+=((uint32_t)k[6])<<16; seanhalle@25: case 6 : b+=((uint32_t)k[5])<<8; seanhalle@25: case 5 : b+=k[4]; seanhalle@25: case 4 : a+=((uint32_t)k[3])<<24; seanhalle@25: case 3 : a+=((uint32_t)k[2])<<16; seanhalle@25: case 2 : a+=((uint32_t)k[1])<<8; seanhalle@25: case 1 : a+=k[0]; seanhalle@25: break; seanhalle@25: case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: } seanhalle@25: seanhalle@25: final(a,b,c); seanhalle@25: *pc=c; *pb=b; seanhalle@25: } seanhalle@25: seanhalle@25: seanhalle@25: seanhalle@25: /* seanhalle@25: * hashbig(): seanhalle@25: * This is the same as hashword() on big-endian machines. It is different seanhalle@25: * from hashlittle() on all machines. hashbig() takes advantage of seanhalle@25: * big-endian byte ordering. seanhalle@25: */ seanhalle@25: uint32_t hashbig( const void *key, size_t length, uint32_t initval) seanhalle@25: { seanhalle@25: uint32_t a,b,c; seanhalle@25: union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */ seanhalle@25: seanhalle@25: /* Set up the internal state */ seanhalle@25: a = b = c = 0xdeadbeef + ((uint32_t)length) + initval; seanhalle@25: seanhalle@25: u.ptr = key; seanhalle@25: if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) { seanhalle@25: const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ seanhalle@25: const uint8_t *k8; seanhalle@25: seanhalle@25: /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += k[0]; seanhalle@25: b += k[1]; seanhalle@25: c += k[2]; seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 3; seanhalle@25: } seanhalle@25: seanhalle@25: /*----------------------------- handle the last (probably partial) block */ seanhalle@25: /* seanhalle@25: * "k[2]<<8" actually reads beyond the end of the string, but seanhalle@25: * then shifts out the part it's not allowed to read. Because the seanhalle@25: * string is aligned, the illegal read is in the same word as the seanhalle@25: * rest of the string. Every machine with memory protection I've seen seanhalle@25: * does it on word boundaries, so is OK with this. But VALGRIND will seanhalle@25: * still catch it and complain. The masking trick does make the hash seanhalle@25: * noticably faster for short strings (like English words). seanhalle@25: */ seanhalle@25: #ifndef VALGRIND seanhalle@25: seanhalle@25: switch(length) seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break; seanhalle@25: case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break; seanhalle@25: case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break; seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=k[1]&0xffffff00; a+=k[0]; break; seanhalle@25: case 6 : b+=k[1]&0xffff0000; a+=k[0]; break; seanhalle@25: case 5 : b+=k[1]&0xff000000; a+=k[0]; break; seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=k[0]&0xffffff00; break; seanhalle@25: case 2 : a+=k[0]&0xffff0000; break; seanhalle@25: case 1 : a+=k[0]&0xff000000; break; seanhalle@25: case 0 : return c; /* zero length strings require no mixing */ seanhalle@25: } seanhalle@25: seanhalle@25: #else /* make valgrind happy */ seanhalle@25: seanhalle@25: k8 = (const uint8_t *)k; seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; seanhalle@25: case 11: c+=((uint32_t)k8[10])<<8; /* fall through */ seanhalle@25: case 10: c+=((uint32_t)k8[9])<<16; /* fall through */ seanhalle@25: case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */ seanhalle@25: case 8 : b+=k[1]; a+=k[0]; break; seanhalle@25: case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */ seanhalle@25: case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */ seanhalle@25: case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */ seanhalle@25: case 4 : a+=k[0]; break; seanhalle@25: case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */ seanhalle@25: case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */ seanhalle@25: case 1 : a+=((uint32_t)k8[0])<<24; break; seanhalle@25: case 0 : return c; seanhalle@25: } seanhalle@25: seanhalle@25: #endif /* !VALGRIND */ seanhalle@25: seanhalle@25: } else { /* need to read the key one byte at a time */ seanhalle@25: const uint8_t *k = (const uint8_t *)key; seanhalle@25: seanhalle@25: /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ seanhalle@25: while (length > 12) seanhalle@25: { seanhalle@25: a += ((uint32_t)k[0])<<24; seanhalle@25: a += ((uint32_t)k[1])<<16; seanhalle@25: a += ((uint32_t)k[2])<<8; seanhalle@25: a += ((uint32_t)k[3]); seanhalle@25: b += ((uint32_t)k[4])<<24; seanhalle@25: b += ((uint32_t)k[5])<<16; seanhalle@25: b += ((uint32_t)k[6])<<8; seanhalle@25: b += ((uint32_t)k[7]); seanhalle@25: c += ((uint32_t)k[8])<<24; seanhalle@25: c += ((uint32_t)k[9])<<16; seanhalle@25: c += ((uint32_t)k[10])<<8; seanhalle@25: c += ((uint32_t)k[11]); seanhalle@25: mix(a,b,c); seanhalle@25: length -= 12; seanhalle@25: k += 12; seanhalle@25: } seanhalle@25: seanhalle@25: /*-------------------------------- last block: affect all 32 bits of (c) */ seanhalle@25: switch(length) /* all the case statements fall through */ seanhalle@25: { seanhalle@25: case 12: c+=k[11]; seanhalle@25: case 11: c+=((uint32_t)k[10])<<8; seanhalle@25: case 10: c+=((uint32_t)k[9])<<16; seanhalle@25: case 9 : c+=((uint32_t)k[8])<<24; seanhalle@25: case 8 : b+=k[7]; seanhalle@25: case 7 : b+=((uint32_t)k[6])<<8; seanhalle@25: case 6 : b+=((uint32_t)k[5])<<16; seanhalle@25: case 5 : b+=((uint32_t)k[4])<<24; seanhalle@25: case 4 : a+=k[3]; seanhalle@25: case 3 : a+=((uint32_t)k[2])<<8; seanhalle@25: case 2 : a+=((uint32_t)k[1])<<16; seanhalle@25: case 1 : a+=((uint32_t)k[0])<<24; seanhalle@25: break; seanhalle@25: case 0 : return c; seanhalle@25: } seanhalle@25: } seanhalle@25: seanhalle@25: final(a,b,c); seanhalle@25: return c; seanhalle@25: } seanhalle@25: seanhalle@25: seanhalle@25: #ifdef SELF_TEST seanhalle@25: seanhalle@25: /* used for timings */ seanhalle@25: void driver1() seanhalle@25: { seanhalle@25: uint8_t buf[256]; seanhalle@25: uint32_t i; seanhalle@25: uint32_t h=0; seanhalle@25: time_t a,z; seanhalle@25: seanhalle@25: time(&a); seanhalle@25: for (i=0; i<256; ++i) buf[i] = 'x'; seanhalle@25: for (i=0; i<1; ++i) seanhalle@25: { seanhalle@25: h = hashlittle(&buf[0],1,h); seanhalle@25: } seanhalle@25: time(&z); seanhalle@25: seanhalle@25: if (z-a > 0) printf("time %d %.8x\n", z-a, h); seanhalle@25: seanhalle@25: } seanhalle@25: seanhalle@25: /* check that every input bit changes every output bit half the time */ seanhalle@25: #define HASHSTATE 1 seanhalle@25: #define HASHLEN 1 seanhalle@25: #define MAXPAIR 60 seanhalle@25: #define MAXLEN 70 seanhalle@25: void driver2() seanhalle@25: { seanhalle@25: uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; seanhalle@25: uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z; seanhalle@25: uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; seanhalle@25: uint32_t x[HASHSTATE],y[HASHSTATE]; seanhalle@25: uint32_t hlen; seanhalle@25: seanhalle@25: printf("No more than %d trials should ever be needed \n",MAXPAIR/2); seanhalle@25: for (hlen=0; hlen < MAXLEN; ++hlen) seanhalle@25: { seanhalle@25: z=0; seanhalle@25: for (i=0; i>(8-j)); seanhalle@25: c[0] = hashlittle(a, hlen, m); seanhalle@25: b[i] ^= ((k+1)<>(8-j)); seanhalle@25: d[0] = hashlittle(b, hlen, m); seanhalle@25: /* check every bit is 1, 0, set, and not set at least once */ seanhalle@25: for (l=0; lz) z=k; seanhalle@25: if (k==MAXPAIR) seanhalle@25: { seanhalle@25: printf("Some bit didn't change: "); seanhalle@25: printf("%.8x %.8x %.8x %.8x %.8x %.8x ", seanhalle@25: e[0],f[0],g[0],h[0],x[0],y[0]); seanhalle@25: printf("i %d j %d m %d len %d\n", i, j, m, hlen); seanhalle@25: } seanhalle@25: if (z==MAXPAIR) goto done; seanhalle@25: } seanhalle@25: } seanhalle@25: } seanhalle@25: done: seanhalle@25: if (z < MAXPAIR) seanhalle@25: { seanhalle@25: printf("Mix success %2d bytes %2d initvals ",i,m); seanhalle@25: printf("required %d trials\n", z/2); seanhalle@25: } seanhalle@25: } seanhalle@25: printf("\n"); seanhalle@25: } seanhalle@25: seanhalle@25: /* Check for reading beyond the end of the buffer and alignment problems */ seanhalle@25: void driver3() seanhalle@25: { seanhalle@25: uint8_t buf[MAXLEN+20], *b; seanhalle@25: uint32_t len; seanhalle@25: uint8_t q[] = "This is the time for all good men to come to the aid of their country..."; seanhalle@25: uint32_t h; seanhalle@25: uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country..."; seanhalle@25: uint32_t i; seanhalle@25: uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country..."; seanhalle@25: uint32_t j; seanhalle@25: uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country..."; seanhalle@25: uint32_t ref,x,y; seanhalle@25: uint8_t *p; seanhalle@25: seanhalle@25: printf("Endianness. These lines should all be the same (for values filled in):\n"); seanhalle@25: printf("%.8x %.8x %.8x\n", seanhalle@25: jenkHash32((const uint32_t *)q, (sizeof(q)-1)/4, 13), seanhalle@25: jenkHash32((const uint32_t *)q, (sizeof(q)-5)/4, 13), seanhalle@25: jenkHash32((const uint32_t *)q, (sizeof(q)-9)/4, 13)); seanhalle@25: p = q; seanhalle@25: printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", seanhalle@25: hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), seanhalle@25: hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), seanhalle@25: hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), seanhalle@25: hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), seanhalle@25: hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), seanhalle@25: hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); seanhalle@25: p = &qq[1]; seanhalle@25: printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", seanhalle@25: hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), seanhalle@25: hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), seanhalle@25: hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), seanhalle@25: hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), seanhalle@25: hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), seanhalle@25: hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); seanhalle@25: p = &qqq[2]; seanhalle@25: printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", seanhalle@25: hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), seanhalle@25: hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), seanhalle@25: hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), seanhalle@25: hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), seanhalle@25: hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), seanhalle@25: hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); seanhalle@25: p = &qqqq[3]; seanhalle@25: printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", seanhalle@25: hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), seanhalle@25: hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), seanhalle@25: hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), seanhalle@25: hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), seanhalle@25: hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), seanhalle@25: hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); seanhalle@25: printf("\n"); seanhalle@25: seanhalle@25: /* check that hashlittle2 and hashlittle produce the same results */ seanhalle@25: i=47; j=0; seanhalle@25: hashlittle2(q, sizeof(q), &i, &j); seanhalle@25: if (hashlittle(q, sizeof(q), 47) != i) seanhalle@25: printf("hashlittle2 and hashlittle mismatch\n"); seanhalle@25: seanhalle@25: /* check that hashword2 and hashword produce the same results */ seanhalle@25: len = 0xdeadbeef; seanhalle@25: i=47, j=0; seanhalle@25: hashword2(&len, 1, &i, &j); seanhalle@25: if (jenkHash32(&len, 1, 47) != i) seanhalle@25: printf("hashword2 and hashword mismatch %x %x\n", seanhalle@25: i, jenkHash32(&len, 1, 47)); seanhalle@25: seanhalle@25: /* check hashlittle doesn't read before or after the ends of the string */ seanhalle@25: for (h=0, b=buf+1; h<8; ++h, ++b) seanhalle@25: { seanhalle@25: for (i=0; i