在看《你必须知道的496个C语言问题》一书中,提到"达夫设备"这个东西,主要是下面的代码:
register n = (count + 7) / 8;
switch (count % 8){
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
我能看明白是用switch来“扁平化”内层循环。不明白的是,为啥一定要把switch和do混在一起写?按下面的方式写,不是更清晰吗?
register n = (count + 7) / 8;
do {
switch (count % 8){
case 0: *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
}
} while (--n > 0);
哪位能帮忙指点一下?
|