Proposal for Optimized Character Type Function

The Cuthour cuthour@gmail.com
Sat Mar 1 21:16:56 GMT 2025



On 2025/03/02 5:15, Florian Weimer wrote:
> * The Cuthour:
> 
>> I am writing to propose an enhancement to the character type functions
>> such as isalpha(), isupper(), and islower() through the use of optimized
>> lookup tables. By using a single lookup table with bitwise operations,
>> we can achieve efficient and concise implementations for these
>> functions. Below is a brief overview of the proposed implementation:
> 
> Isn't this basically what the current implementation does, via
> __ctype_b_loc?  Per-thread state is required to support uselocale.

I don't know. My plan is this.

--------------------------------

static const unsigned char type_table[256] = {
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
     0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x02,
     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
     0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01,
     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
     0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const bool
my_islower (const unsigned char ch)
{
     return type_table[ch] & 0x01;
}

const bool
my_isupper (const unsigned char ch)
{
     return type_table[ch] & 0x02;
}

const bool
my_isalpha (const unsigned char ch)
{
     return type_table[ch] & 0x03;
}

const bool
my_isdigit (const unsigned char ch)
{
     return type_table[ch] & 0x04;
}

const bool
my_isalnum (const unsigned char ch)
{
     return type_table[ch] & 0x07;
}

const bool
my_isxdigit (const unsigned char ch)
{
     return type_table[ch] & 0x08;
}




More information about the Libc-alpha mailing list