PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ hashlittle2()

void hashlittle2 ( const void *  key,
size_t  length,
uint32_t *  pc,
uint32_t *  pb 
)

Definition at line 476 of file lookup3.c.

481{
482 uint32_t a,b,c; /* internal state */
483 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
484
485 /* Set up the internal state */
486 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
487 c += *pb;
488
489 u.ptr = key;
490 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
491 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
492 // const uint8_t *k8;
493
494 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
495 while (length > 12)
496 {
497 a += k[0];
498 b += k[1];
499 c += k[2];
500 mix(a,b,c);
501 length -= 12;
502 k += 3;
503 }
504
505 /*----------------------------- handle the last (probably partial) block */
506 /*
507 * "k[2]&0xffffff" actually reads beyond the end of the string, but
508 * then masks off the part it's not allowed to read. Because the
509 * string is aligned, the masked-off tail is in the same word as the
510 * rest of the string. Every machine with memory protection I've seen
511 * does it on word boundaries, so is OK with this. But VALGRIND will
512 * still catch it and complain. The masking trick does make the hash
513 * noticably faster for short strings (like English words).
514 */
515#ifndef VALGRIND
516
517 switch(length)
518 {
519 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
520 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
521 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
522 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
523 case 8 : b+=k[1]; a+=k[0]; break;
524 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
525 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
526 case 5 : b+=k[1]&0xff; a+=k[0]; break;
527 case 4 : a+=k[0]; break;
528 case 3 : a+=k[0]&0xffffff; break;
529 case 2 : a+=k[0]&0xffff; break;
530 case 1 : a+=k[0]&0xff; break;
531 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
532 }
533
534#else /* make valgrind happy */
535
536 k8 = (const uint8_t *)k;
537 switch(length)
538 {
539 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
540 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
541 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
542 case 9 : c+=k8[8]; /* fall through */
543 case 8 : b+=k[1]; a+=k[0]; break;
544 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
545 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
546 case 5 : b+=k8[4]; /* fall through */
547 case 4 : a+=k[0]; break;
548 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
549 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
550 case 1 : a+=k8[0]; break;
551 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
552 }
553
554#endif /* !valgrind */
555
556 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
557 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
558 const uint8_t *k8;
559
560 /*--------------- all but last block: aligned reads and different mixing */
561 while (length > 12)
562 {
563 a += k[0] + (((uint32_t)k[1])<<16);
564 b += k[2] + (((uint32_t)k[3])<<16);
565 c += k[4] + (((uint32_t)k[5])<<16);
566 mix(a,b,c);
567 length -= 12;
568 k += 6;
569 }
570
571 /*----------------------------- handle the last (probably partial) block */
572 k8 = (const uint8_t *)k;
573 switch(length)
574 {
575 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
576 b+=k[2]+(((uint32_t)k[3])<<16);
577 a+=k[0]+(((uint32_t)k[1])<<16);
578 break;
579 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
580 case 10: c+=k[4];
581 b+=k[2]+(((uint32_t)k[3])<<16);
582 a+=k[0]+(((uint32_t)k[1])<<16);
583 break;
584 case 9 : c+=k8[8]; /* fall through */
585 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
586 a+=k[0]+(((uint32_t)k[1])<<16);
587 break;
588 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
589 case 6 : b+=k[2];
590 a+=k[0]+(((uint32_t)k[1])<<16);
591 break;
592 case 5 : b+=k8[4]; /* fall through */
593 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
594 break;
595 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
596 case 2 : a+=k[0];
597 break;
598 case 1 : a+=k8[0];
599 break;
600 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
601 }
602
603 } else { /* need to read the key one byte at a time */
604 const uint8_t *k = (const uint8_t *)key;
605
606 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
607 while (length > 12)
608 {
609 a += k[0];
610 a += ((uint32_t)k[1])<<8;
611 a += ((uint32_t)k[2])<<16;
612 a += ((uint32_t)k[3])<<24;
613 b += k[4];
614 b += ((uint32_t)k[5])<<8;
615 b += ((uint32_t)k[6])<<16;
616 b += ((uint32_t)k[7])<<24;
617 c += k[8];
618 c += ((uint32_t)k[9])<<8;
619 c += ((uint32_t)k[10])<<16;
620 c += ((uint32_t)k[11])<<24;
621 mix(a,b,c);
622 length -= 12;
623 k += 12;
624 }
625
626 /*-------------------------------- last block: affect all 32 bits of (c) */
627 switch(length) /* all the case statements fall through */
628 {
629 case 12: c+=((uint32_t)k[11])<<24; /* fall through */
630 case 11: c+=((uint32_t)k[10])<<16; /* fall through */
631 case 10: c+=((uint32_t)k[9])<<8; /* fall through */
632 case 9 : c+=k[8]; /* fall through */
633 case 8 : b+=((uint32_t)k[7])<<24; /* fall through */
634 case 7 : b+=((uint32_t)k[6])<<16; /* fall through */
635 case 6 : b+=((uint32_t)k[5])<<8; /* fall through */
636 case 5 : b+=k[4]; /* fall through */
637 case 4 : a+=((uint32_t)k[3])<<24; /* fall through */
638 case 3 : a+=((uint32_t)k[2])<<16; /* fall through */
639 case 2 : a+=((uint32_t)k[1])<<8; /* fall through */
640 case 1 : a+=k[0];
641 break;
642 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
643 }
644 }
645
646 final(a,b,c);
647 *pc=c; *pb=b;
648}
#define HASH_LITTLE_ENDIAN
Definition lookup3.c:65
#define mix(a, b, c)
Definition lookup3.c:126

References HASH_LITTLE_ENDIAN, and mix.

Referenced by gserialized1_hash(), and gserialized2_hash().

Here is the caller graph for this function: