Click here to Skip to main content
16,020,249 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Intergalactic cephalopods Pin
Super Lloyd7-Jan-22 11:57
Super Lloyd7-Jan-22 11:57 
AnswerRe: Intergalactic cephalopods Pin
Eddy Vluggen7-Jan-22 13:26
professionalEddy Vluggen7-Jan-22 13:26 
GeneralRe: Intergalactic cephalopods Pin
Rob Grainger1-Feb-22 3:20
Rob Grainger1-Feb-22 3:20 
GeneralCuter by the day.... Pin
Peter_in_278028-Dec-21 17:05
professionalPeter_in_278028-Dec-21 17:05 
GeneralRe: Cuter by the day.... Pin
Super Lloyd7-Jan-22 12:08
Super Lloyd7-Jan-22 12:08 
GeneralRe: Cuter by the day.... Pin
Peter_in_27807-Jan-22 18:35
professionalPeter_in_27807-Jan-22 18:35 
GeneralRe: Cuter by the day.... Pin
Super Lloyd7-Jan-22 20:32
Super Lloyd7-Jan-22 20:32 
GeneralThis is actually cleaned up! Pin
honey the codewitch14-Dec-21 17:58
mvahoney the codewitch14-Dec-21 17:58 
Laugh | :laugh:

The original code was a bunch of preprocessor macros.

This is compile time if statements and lots of const folding.

It's deliberately unrolled so it's as inline as possible - this is very timing sensitive.

If you think this is bad, you should have seen the original code.

C++
if(has_data_low_pins && has_data_high_pins) {
    
    // 3x for bus access stabilization
    uint32_t pins_l = gpio_input_get();
    pins_l = gpio_input_get();
    pins_l = gpio_input_get();
    uint32_t pins_h = gpio_input_get_high();
    if(pin_d0>31) {
        b = (((pins_h>>((pin_d0-32)&31))&1)<<0);
    } else if(pin_d0>-1) {
        b = (((pins_l>>(pin_d0))&1)<<0);
    } else {
        b=0;
    }
    if(pin_d1>31) {
        b |= (((pins_h>>((pin_d1-32)&31))&1)<<1);
    } else if(pin_d1>-1) {
        b |= (((pins_l>>(pin_d1))&1)<<1);
    }
    if(pin_d2>31) {
        b |= (((pins_h>>((pin_d2-32)&31))&1)<<2);
    } else if(pin_d2>-1) {
        b |= (((pins_l>>(pin_d2))&1)<<2);
    }
    if(pin_d3>31) {
        b |= (((pins_h>>((pin_d3-32)&31))&1)<<3);
    } else if(pin_d3>-1) {
        b |= (((pins_l>>(pin_d3))&1)<<3);
    }
    if(pin_d4>31) {
        b |= (((pins_h>>((pin_d4-32)&31))&1)<<4);
    } else if(pin_d4>-1) {
        b |= (((pins_l>>((pin_d4)&31))&1)<<4);
    }
    if(pin_d5>31) {
        b |= (((pins_h>>((pin_d5-32)&31))&1)<<5);
    } else if(pin_d5>-1) {
        b |= (((pins_l>>(pin_d5))&1)<<5);
    }
    if(pin_d6>31) {
        b |= (((pins_h>>((pin_d6-32)&31))&1)<<6);
    } else if(pin_d6>-1) {
        b |= (((pins_l>>(pin_d6))&1)<<6);
    }
    if(pin_d7>31) {
        b |= (((pins_h>>((pin_d7-32)&31))&1)<<7);
    } else if(pin_d7>-1) {
        b |= (((pins_l>>(pin_d7))&1)<<7);
    }
} else if(has_data_low_pins) {
    uint32_t pins_l = gpio_input_get();
    pins_l = gpio_input_get();
    pins_l = gpio_input_get();
    if(pin_d0>-1) {
        b = (((pins_l>>(pin_d0))&1)<<0);
    } else {
        b=0;
    }
    if(pin_d1>-1) {
        b |= (((pins_l>>(pin_d1))&1)<<1);
    }
    if(pin_d2>-1) {
        b |= (((pins_l>>(pin_d2))&1)<<2);
    }
    if(pin_d3>-1) {
        b |= (((pins_l>>(pin_d3))&1)<<3);
    }
    if(pin_d4>-1) {
        b |= (((pins_l>>(pin_d4))&1)<<4);
    }
    if(pin_d5>-1) {
        b |= (((pins_l>>(pin_d5))&1)<<5);
    }
    if(pin_d6>-1) {
        b |= (((pins_l>>(pin_d6))&1)<<6);
    }
    if(pin_d7>-1) {
        b |= (((pins_l>>(pin_d7))&1)<<7);
    }
} else {
    uint32_t pins_h = gpio_input_get_high();
    pins_h = gpio_input_get_high();
    pins_h = gpio_input_get_high();
    if(pin_d0>-1) {
        b = (((pins_h>>((pin_d0-32)&31))&1)<<0);
    } else {
        b=0;
    }
    if(pin_d1>-1) {
        b |= (((pins_h>>((pin_d1-32)&31))&1)<<1);
    }
    if(pin_d2>-1) {
        b |= (((pins_h>>((pin_d2-32)&31))&1)<<2);
    }
    if(pin_d3>-1) {
        b |= (((pins_h>>((pin_d3-32)&31))&1)<<3);
    }
    if(pin_d4>-1) {
        b |= (((pins_h>>((pin_d4-32)&31))&1)<<4);
    }
    if(pin_d5>-1) {
        b |= (((pins_h>>((pin_d5-32)&31))&1)<<5);
    }
    if(pin_d6>-1) {
        b |= (((pins_h>>((pin_d6-32)&31))&1)<<6);
    }
    if(pin_d7>-1) {
        b |= (((pins_h>>((pin_d7-32)&31))&1)<<7);
    }
}

Real programmers use butterflies

GeneralRe: This is actually cleaned up! Pin
11917640 Member 14-Dec-21 21:49
11917640 Member 14-Dec-21 21:49 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 1:43
mvahoney the codewitch15-Dec-21 1:43 
GeneralRe: This is actually cleaned up! Pin
Slacker00715-Dec-21 7:58
professionalSlacker00715-Dec-21 7:58 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 9:17
mvahoney the codewitch15-Dec-21 9:17 
GeneralRe: This is actually cleaned up! Pin
Rick York15-Dec-21 6:30
mveRick York15-Dec-21 6:30 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 6:38
mvahoney the codewitch15-Dec-21 6:38 
GeneralRe: This is actually cleaned up! Pin
Rick York15-Dec-21 6:55
mveRick York15-Dec-21 6:55 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 6:58
mvahoney the codewitch15-Dec-21 6:58 
GeneralRe: This is actually cleaned up! Pin
jschell23-Jan-22 8:54
jschell23-Jan-22 8:54 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch24-Jan-22 1:00
mvahoney the codewitch24-Jan-22 1:00 
GeneralRe: This is actually cleaned up! Pin
jschell30-Jan-22 6:13
jschell30-Jan-22 6:13 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch30-Jan-22 9:02
mvahoney the codewitch30-Jan-22 9:02 
GeneralRe: This is actually cleaned up! Pin
jschell17-Feb-22 10:33
jschell17-Feb-22 10:33 
JokeRe: This is actually cleaned up! Pin
Nelek15-Dec-21 10:59
protectorNelek15-Dec-21 10:59 
GeneralRe: This is actually cleaned up! Pin
Fueled By Decaff15-Dec-21 6:30
Fueled By Decaff15-Dec-21 6:30 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 6:32
mvahoney the codewitch15-Dec-21 6:32 
GeneralRe: This is actually cleaned up! Pin
Al_Brown31-Dec-21 1:44
Al_Brown31-Dec-21 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.