假设您插入一系列值,其中前一个端点是新的起点:
(B-A)/(x/255)+A
听起来是个坏主意。如果您使用以 255 为底的定点表示,您将获得两次相同的插值。当 x=255 时得到 B,当 x=0 时得到 B 作为新的 A。
使用 256 作为定点系统。除法变为移位,但您需要 16 位算术和 16 位结果的 8x8 乘法。前面的问题可以通过简单地忽略高字节中的任何位x mod 256
变为 0 来解决。这个建议使用 16 位乘法,但不能溢出。并且您不会在相同的 x 上插值两次。
interp = (a*(256 - x) + b*x) >> 8
256 - x
当你得到0 - x
.
PIC 在其指令集中缺少这些操作:
您可以通过使用 rotate-right 来实现右移,然后使用 bitwise-and 屏蔽左侧的额外位。用 16 位结果进行 8x8 乘法的直接方法:
void mul16(
unsigned char* hi, /* in: operand1, out: the most significant byte */
unsigned char* lo /* in: operand2, out: the least significant byte */
)
{
unsigned char a,b;
/* loop over the smallest value */
a = (*hi <= *lo) ? *hi : *lo;
b = (*hi <= *lo) ? *lo : *hi;
*hi = *lo = 0;
while(a){
*lo+=b;
if(*lo < b) /* unsigned overflow. Use the carry flag instead.*/
*hi++;
--a;
}
}