GlasgowEmbedded / glasgow

Scots Army Knife for electronics
BSD Zero Clause License
1.92k stars 189 forks source link

firmware: Optimize code size by using goto, saving 59 bytes #651

Closed purdeaandrei closed 3 months ago

purdeaandrei commented 3 months ago

Saves 59 bytes

whitequark commented 3 months ago

I think you could probably get the same benefit by converting that giant conditional to be an if..else one and adding explicit return to all paths that do not stall, leaving the stalling paths to fall through.

purdeaandrei commented 3 months ago

When you mean giant conditional, are you referring to this?:

if(glasgow_config.revision < GLASGOW_REV_C0 ||
         !iobuf_get_pull(arg_selector,
                         (__xdata uint8_t *)EP0BUF + 0,
                         (__xdata uint8_t *)EP0BUF + 1)) {
whitequark commented 3 months ago

Yeah. The duplication of STALL_EP0() could be eliminated by reordering it. (This is something I've been thinking about for a while already; I'm not principally opposed to goto, it just seems like the restructuring would obviate the need for it.)

purdeaandrei commented 3 months ago

hmm, the two stalls in the while loop: I don't think those can be optimized without goto, unless I add another check to see if the loop exited early or not

purdeaandrei commented 3 months ago

I tried without goto (except for those two stalls in the while loop), and the code size increased!

purdeaandrei commented 3 months ago

This is what I tried: https://github.com/purdeaandrei/glasgow/commit/85deafc2264b8a7cc7ad7a716e248e1f1467d84f

And it increased code size by 37 bytes :( . I don't know what the explanation is

whitequark commented 3 months ago

Oh no ;; sdcc is like this sometimes. Thank you for trying!

In this case the goto solution is the obvious winner, let me review this.