This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Patch for locale/programs/3level.h


Paul Eggert writes:
> Assuming a twos-complement platform, (~0) evaluates to -1, and hence
> ((uint32_t) ~0) evaluates to 0xFFFFFFFF regardless of int width.

Right, I got this wrong.

> Conversly, ~((uint32_t) 0) does not always evaluate to 0xFFFFFFF.  For
> example, if int is 64 bits, it evaluates to -1, which is
> 0xFFFFFFFFFFFFFFFF (again, assuming twos-complement).

Ah, you are thinking at larger 'int' than usual, whereas I was
thinking at smaller 'int'.

> (uint32_t) -1 is the usual way to write an all-one's pattern.

I have learned to avoid negative numbers in all expression that
involve unsigned integer types.


Here is the complete table.

int size                           16            32              64

~ (uint32_t) 0                 0xFFFFFFFF   0xFFFFFFFF   0xFFFFFFFFFFFFFFFF

(uint32_t) ~0                  0xFFFFFFFF   0xFFFFFFFF   0xFFFFFFFF

(uint32_t) ~ (uint32_t) 0      0xFFFFFFFF   0xFFFFFFFF   0xFFFFFFFF


So here is a patch to use the second expression.


2001-08-07  Andreas Jaeger  <aj@suse.de>
            Andreas Schwab  <schwab@suse.de>
            Bruno Haible  <haible@clisp.cons.org>

	* locale/programs/3level.h (EMPTY): New macro.
	(*_get, *_add, *_iterate, *_finalize): Use it instead of ~(uint32_t)0.

*** glibc-cvs/locale/programs/3level.h.orig	Tue Jul 10 22:59:06 2001
--- glibc-cvs/locale/programs/3level.h	Tue Aug  7 16:13:59 2001
***************
*** 1,4 ****
! /* Copyright (C) 2000 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
     Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
  
--- 1,4 ----
! /* Copyright (C) 2000-2001 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
     Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
  
***************
*** 74,79 ****
--- 74,83 ----
    t->level3_alloc = t->level3_size = 0;
  }
  
+ /* Marker for an empty slot.  This has the value 0xFFFFFFFF, regardless
+    whether 'int' is 16 bit, 32 bit, or 64 bit.  */
+ #define EMPTY ((uint32_t) ~0)
+ 
  /* Retrieve an entry.  */
  static inline ELEMENT
  CONCAT(TABLE,_get) (struct TABLE *t, uint32_t wc)
***************
*** 82,93 ****
    if (index1 < t->level1_size)
      {
        uint32_t lookup1 = t->level1[index1];
!       if (lookup1 != ~((uint32_t) 0))
  	{
  	  uint32_t index2 = ((wc >> t->p) & ((1 << t->q) - 1))
  			    + (lookup1 << t->q);
  	  uint32_t lookup2 = t->level2[index2];
! 	  if (lookup2 != ~((uint32_t) 0))
  	    {
  	      uint32_t index3 = (wc & ((1 << t->p) - 1))
  				+ (lookup2 << t->p);
--- 86,97 ----
    if (index1 < t->level1_size)
      {
        uint32_t lookup1 = t->level1[index1];
!       if (lookup1 != EMPTY)
  	{
  	  uint32_t index2 = ((wc >> t->p) & ((1 << t->q) - 1))
  			    + (lookup1 << t->q);
  	  uint32_t lookup2 = t->level2[index2];
! 	  if (lookup2 != EMPTY)
  	    {
  	      uint32_t index3 = (wc & ((1 << t->p) - 1))
  				+ (lookup2 << t->p);
***************
*** 124,133 ****
  	  t->level1_alloc = alloc;
  	}
        while (index1 >= t->level1_size)
! 	t->level1[t->level1_size++] = ~((uint32_t) 0);
      }
  
!   if (t->level1[index1] == ~((uint32_t) 0))
      {
        if (t->level2_size == t->level2_alloc)
  	{
--- 128,137 ----
  	  t->level1_alloc = alloc;
  	}
        while (index1 >= t->level1_size)
! 	t->level1[t->level1_size++] = EMPTY;
      }
  
!   if (t->level1[index1] == EMPTY)
      {
        if (t->level2_size == t->level2_alloc)
  	{
***************
*** 139,151 ****
        i1 = t->level2_size << t->q;
        i2 = (t->level2_size + 1) << t->q;
        for (i = i1; i < i2; i++)
! 	t->level2[i] = ~((uint32_t) 0);
        t->level1[index1] = t->level2_size++;
      }
  
    index2 += t->level1[index1] << t->q;
  
!   if (t->level2[index2] == ~((uint32_t) 0))
      {
        if (t->level3_size == t->level3_alloc)
  	{
--- 143,155 ----
        i1 = t->level2_size << t->q;
        i2 = (t->level2_size + 1) << t->q;
        for (i = i1; i < i2; i++)
! 	t->level2[i] = EMPTY;
        t->level1[index1] = t->level2_size++;
      }
  
    index2 += t->level1[index1] << t->q;
  
!   if (t->level2[index2] == EMPTY)
      {
        if (t->level3_size == t->level3_alloc)
  	{
***************
*** 176,189 ****
    for (index1 = 0; index1 < t->level1_size; index1++)
      {
        uint32_t lookup1 = t->level1[index1];
!       if (lookup1 != ~((uint32_t) 0))
  	{
  	  uint32_t lookup1_shifted = lookup1 << t->q;
  	  uint32_t index2;
  	  for (index2 = 0; index2 < (1 << t->q); index2++)
  	    {
  	      uint32_t lookup2 = t->level2[index2 + lookup1_shifted];
! 	      if (lookup2 != ~((uint32_t) 0))
  		{
  		  uint32_t lookup2_shifted = lookup2 << t->p;
  		  uint32_t index3;
--- 180,193 ----
    for (index1 = 0; index1 < t->level1_size; index1++)
      {
        uint32_t lookup1 = t->level1[index1];
!       if (lookup1 != EMPTY)
  	{
  	  uint32_t lookup1_shifted = lookup1 << t->q;
  	  uint32_t index2;
  	  for (index2 = 0; index2 < (1 << t->q); index2++)
  	    {
  	      uint32_t lookup2 = t->level2[index2 + lookup1_shifted];
! 	      if (lookup2 != EMPTY)
  		{
  		  uint32_t lookup2_shifted = lookup2 << t->p;
  		  uint32_t index3;
***************
*** 232,238 ****
    t->level3_size = k;
  
    for (i = 0; i < (t->level2_size << t->q); i++)
!     if (t->level2[i] != ~((uint32_t) 0))
        t->level2[i] = reorder3[t->level2[i]];
  
    /* Uniquify level2 blocks.  */
--- 236,242 ----
    t->level3_size = k;
  
    for (i = 0; i < (t->level2_size << t->q); i++)
!     if (t->level2[i] != EMPTY)
        t->level2[i] = reorder3[t->level2[i]];
  
    /* Uniquify level2 blocks.  */
***************
*** 256,262 ****
    t->level2_size = k;
  
    for (i = 0; i < t->level1_size; i++)
!     if (t->level1[i] != ~((uint32_t) 0))
        t->level1[i] = reorder2[t->level1[i]];
  
    /* Create and fill the resulting compressed representation.  */
--- 260,266 ----
    t->level2_size = k;
  
    for (i = 0; i < t->level1_size; i++)
!     if (t->level1[i] != EMPTY)
        t->level1[i] = reorder2[t->level1[i]];
  
    /* Create and fill the resulting compressed representation.  */
***************
*** 286,298 ****
  
    for (i = 0; i < t->level1_size; i++)
      ((uint32_t *) (t->result + level1_offset))[i] =
!       (t->level1[i] == ~((uint32_t) 0)
         ? 0
         : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
  
    for (i = 0; i < (t->level2_size << t->q); i++)
      ((uint32_t *) (t->result + level2_offset))[i] =
!       (t->level2[i] == ~((uint32_t) 0)
         ? 0
         : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
  
--- 290,302 ----
  
    for (i = 0; i < t->level1_size; i++)
      ((uint32_t *) (t->result + level1_offset))[i] =
!       (t->level1[i] == EMPTY
         ? 0
         : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
  
    for (i = 0; i < (t->level2_size << t->q); i++)
      ((uint32_t *) (t->result + level2_offset))[i] =
!       (t->level2[i] == EMPTY
         ? 0
         : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
  
***************
*** 311,316 ****
--- 315,321 ----
  }
  #endif
  
+ #undef EMPTY
  #undef TABLE
  #undef ELEMENT
  #undef DEFAULT


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]