Thread: Function Compression

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    Function Compression

    Hello folks,

    I normally wouldn't bother you guys with silly things but I figured if y'all are
    wasting time answering Ryanc's questions, maybe you're having a slow weekend
    and won't mind the distraction.

    I Need to shave two lines off this function, to bring it down to the required 25
    line limit I have to follow. Normally I would just cut some statements and create
    a helper function but I happen to be at the function limit for this translation
    unit -five, in case you're wondering- and I rather not create another source file
    just for this function. Besides, I need the function to be "inlineable"
    and I can't include another definition in a header, so it *must* be
    in this source file.

    It's been a long day and I can't see any other way to shorten the code. Do you see
    something obvious I'm missing?

    Thanks!

    PS: Can't initialize variables during declaration and can't change the coding
    style. Can't use multiline macros either, and the max line limit is 80 columns.
    Also, I rather not compress everything into one loop as that slows down
    the encoding significantly. The compiler can't unroll the loop since it doesn't
    know the value of size.
    Code:
    #define SHIFT_BYTES 3
    #define READ_BUFFER SHIFT_BYTES
    #define FIRST_SEXTET 26
    #define SECOND_SEXTET 20
    #define THIRD_SEXTET 14
    #define FOURTH_SEXTET 8
    #define SEXTET_MASK 0x3f
    #define SEXTET 6
    
    static inline void    encode64
        (char *encoded, const char *stream, unsigned int size, unsigned int padding)
    {
        unsigned int    shift_count;
        uint32_t        word;
    
        while (size >= sizeof(word))
        {
            word = *(uint32_t*)(void*)stream;
            word = reverse_byte_order(word);
            *encoded++ = b64_alphabet[(word >> FIRST_SEXTET)];
            *encoded++ = b64_alphabet[(word >> SECOND_SEXTET) & SEXTET_MASK];
            *encoded++ = b64_alphabet[(word >> THIRD_SEXTET) & SEXTET_MASK];
            *encoded++ = b64_alphabet[(word >> FOURTH_SEXTET) & SEXTET_MASK];
            stream += READ_BUFFER;
            size -= READ_BUFFER;
        }
        word = 0;
        shift_count = SHIFT_BYTES;
        while (size--)
            word |= *stream++ << (shift_count-- << 3);
        size = (shift_count != SHIFT_BYTES ? 1 + (SHIFT_BYTES - shift_count) : 0);
        shift_count = FIRST_SEXTET;
        while (size--)
        {
            *encoded++ = b64_alphabet[(word >> shift_count) & SEXTET_MASK];
            shift_count -= SEXTET;
        }
        while (padding--)
            *encoded++ = '=';
    }
    Last edited by Dren; 02-04-2019 at 12:33 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
            *encoded++ = b64_alphabet[(word >> FIRST_SEXTET)];
            *encoded++ = b64_alphabet[(word >> SECOND_SEXTET) & SEXTET_MASK];
            *encoded++ = b64_alphabet[(word >> THIRD_SEXTET) & SEXTET_MASK];
            *encoded++ = b64_alphabet[(word >> FOURTH_SEXTET) & SEXTET_MASK];
    With
    Code:
    for ( int i = 3 ; i >= 0 ; i-- )
            *encoded++ = b64_alphabet[(word >> (8+SEXTET*i)) & SEXTET_MASK];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    104
    At first sight I was going to accuse you of not reading my PS, but then I thought
    "Come on, my man Salem is better than that" and on second look I see a compile
    time constant, loop unrolling achievement unlocked!
    Seriously, how long did it take you to conjure that up?

    Although I'm not allowed to use "for" loops, I can always define away that whole
    line and guard the definition with with an #if clause -little trick someone named
    after a witchy town taught me, wink, wink- and the school bot will be none the wiser.
    Stupid rules deserve no respect.

    XoXo
    Last edited by Dren; 02-04-2019 at 02:32 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Seriously, how long did it take you to conjure that up?
    Couple of minutes - tops.

    > Although I'm not allowed to use "for" loops
    for loops are just while loops with some decoration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. better compression alg
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-06-2008, 04:53 PM
  2. Compression
    By Yartch in forum Game Programming
    Replies: 2
    Last Post: 07-03-2007, 01:45 PM
  3. Compression
    By ~Kyo~ in forum Game Programming
    Replies: 9
    Last Post: 10-24-2004, 12:41 AM
  4. IDEA: Huffman compression function
    By confuted in forum Contests Board
    Replies: 4
    Last Post: 07-01-2003, 07:05 PM
  5. mp3 compression
    By Eibro in forum Tech Board
    Replies: 14
    Last Post: 01-21-2003, 07:02 AM

Tags for this Thread