Click here to Skip to main content
15,513,571 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 12:57
Super Lloyd7-Jan-22 12:57 
AnswerRe: Intergalactic cephalopods Pin
Eddy Vluggen7-Jan-22 14:26
professionalEddy Vluggen7-Jan-22 14:26 
GeneralRe: Intergalactic cephalopods Pin
Rob Grainger1-Feb-22 4:20
Rob Grainger1-Feb-22 4:20 
GeneralCuter by the day.... PinPopular
Peter_in_278028-Dec-21 18:05
professionalPeter_in_278028-Dec-21 18:05 
GeneralRe: Cuter by the day.... Pin
Super Lloyd7-Jan-22 13:08
Super Lloyd7-Jan-22 13:08 
GeneralRe: Cuter by the day.... Pin
Peter_in_27807-Jan-22 19:35
professionalPeter_in_27807-Jan-22 19:35 
GeneralRe: Cuter by the day.... Pin
Super Lloyd7-Jan-22 21:32
Super Lloyd7-Jan-22 21:32 
GeneralThis is actually cleaned up! Pin
honey the codewitch14-Dec-21 18:58
mvahoney the codewitch14-Dec-21 18: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 22:49
11917640 Member 14-Dec-21 22:49 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 2:43
mvahoney the codewitch15-Dec-21 2:43 
GeneralRe: This is actually cleaned up! Pin
Slacker00715-Dec-21 8:58
professionalSlacker00715-Dec-21 8:58 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 10:17
mvahoney the codewitch15-Dec-21 10:17 
GeneralRe: This is actually cleaned up! Pin
Rick York15-Dec-21 7:30
mveRick York15-Dec-21 7:30 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 7:38
mvahoney the codewitch15-Dec-21 7:38 
GeneralRe: This is actually cleaned up! Pin
Rick York15-Dec-21 7:55
mveRick York15-Dec-21 7:55 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 7:58
mvahoney the codewitch15-Dec-21 7:58 
GeneralRe: This is actually cleaned up! Pin
jschell23-Jan-22 9:54
jschell23-Jan-22 9:54 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch24-Jan-22 2:00
mvahoney the codewitch24-Jan-22 2:00 
GeneralRe: This is actually cleaned up! Pin
jschell30-Jan-22 7:13
jschell30-Jan-22 7:13 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch30-Jan-22 10:02
mvahoney the codewitch30-Jan-22 10:02 
GeneralRe: This is actually cleaned up! Pin
jschell17-Feb-22 11:33
jschell17-Feb-22 11:33 
JokeRe: This is actually cleaned up! Pin
Nelek15-Dec-21 11:59
protectorNelek15-Dec-21 11:59 
GeneralRe: This is actually cleaned up! Pin
Fueled By Decaff15-Dec-21 7:30
Fueled By Decaff15-Dec-21 7:30 
GeneralRe: This is actually cleaned up! Pin
honey the codewitch15-Dec-21 7:32
mvahoney the codewitch15-Dec-21 7:32 
GeneralRe: This is actually cleaned up! Pin
Al_Brown31-Dec-21 2:44
Al_Brown31-Dec-21 2: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.