【发布时间】:2016-07-11 05:47:27
【问题描述】:
我目前正在编写一个 Intel 8042 驱动程序,并编写了两个循环来等待一些缓冲区准备好使用:
/* Waits until the Intel 8042's input buffer is empty, i.e., until the
* controller has processed the input. */
i8042_waitin:
pause
in $i8042_STATUS, %al
and $i8042_STAT_INEMPTY, %al
jz i8042_waitin
ret
/* Waits until the Intel 8042's output buffer is full, i.e., data to read is
* available.
* ATTENTION: this here is the polling variant but there is also a way with
* interrupts! By setting bit 0 in the command byte you can enable an interrupt
* to be fired when the output buffer is full. */
i8042_waitout:
pause
in $i8042_STATUS, %al
and $i8042_STAT_OUTFULL, %al
jz i8042_waitout
ret
如您所见,我在循环中插入了pause 指令。我最近才知道它,自然想尝试一下。
由于%al 的内容是不可预测的,因为它是 I/O 读取,因此分支预测器将用循环指令填充管道:经过一些迭代后,它会注意到始终采用一个分支,类似于 the case here。
如果分支预测器确实在其预测中包含 I/O 指令,则上述内容是正确的,我不确定。
那么分支预测器是否会使用 I/O 指令的结果进行自我调整,就像不可预测的内存读取一样?还是这里发生了其他事情?pause 在这里有意义吗?
【问题讨论】:
标签: performance loops assembly x86 pause