How does Duff's device work?

前端 未结 11 1672
日久生厌
日久生厌 2020-11-22 04:56

I\'ve read the article on Wikipedia on the Duff\'s device, and I don\'t get it. I am really interested, but I\'ve read the explanation there a couple of times and I still do

11条回答
  •  一生所求
    2020-11-22 05:33

    The explanation in Dr. Dobb's Journal is the best that I found on the topic.

    This being my AHA moment:

    for (i = 0; i < len; ++i) {
        HAL_IO_PORT = *pSource++;
    }
    

    becomes:

    int n = len / 8;
    for (i = 0; i < n; ++i) {
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
        HAL_IO_PORT = *pSource++;
    }
    
    n = len % 8;
    for (i = 0; i < n; ++i) {
        HAL_IO_PORT = *pSource++;
    }
    

    becomes:

    int n = (len + 8 - 1) / 8;
    switch (len % 8) {
        case 0: do { HAL_IO_PORT = *pSource++;
        case 7: HAL_IO_PORT = *pSource++;
        case 6: HAL_IO_PORT = *pSource++;
        case 5: HAL_IO_PORT = *pSource++;
        case 4: HAL_IO_PORT = *pSource++;
        case 3: HAL_IO_PORT = *pSource++;
        case 2: HAL_IO_PORT = *pSource++;
        case 1: HAL_IO_PORT = *pSource++;
                   } while (--n > 0);
    }
    

提交回复
热议问题