Packing 5-bit words into size_t-aligned bit-array

Per Nordlöw per.nordlow at gmail.com
Fri Jan 11 09:20:05 UTC 2019


I want to pack a letters from a char[] into an array of 5-bit 
encoded English lower letters in a size_t[2].

How do I accomplish that with as few instructions as possible?

I currently have

         size_t[2] words;
         enum charBits = 5;
         foreach (const ch_index, const char ch; expr)
         {
             ubyte ub = 0;
             if (ch >= 'a' && ch <= 'z') // English lower letter
             {
                 ub = cast(ubyte)(ch - 'a');
             }
             else if (ch == ' ') // space
             {
                 ub = cast(ubyte)(ch - 'z' + 1);
             }
             else
             {
                 dln("Invalid char ", ch);
                 assert(0);
             }
             assert(ub < (1 << charBits), "Too large ubyte code");
             const bit_index = 8 + charBits*ch_index; // first 8 
bits reserved for length
             // TODO store first `charBits` bits of `ub` at 
`bit_index` in `words`
         }



More information about the Digitalmars-d-learn mailing list