如何在Verilog中执行两个Q15值的除法,而无需使用'/'(除法)运算符?



As除法运算(/)在FPGA的情况下是昂贵的?是否可以用基本移位操作对两个Q15格式编号(16位定点编号)进行除法?

有人能帮我举个例子吗?

提前感谢!

不动点算术只是一种引入了一点缩放的整数算术。Q15是一种纯分数格式,存储为带符号的16位整数,缩放因子为215,能够表示区间[-1,1)中的值。显然,只有当除数的大小超过被除数的幅度时,除法在Q15中才有意义,否则商的大小超过了可表示的范围。

在开始定制Verilog定点除法实现之前,您需要检查FPGA供应商的库产品,因为包括流水线除法在内的定点库经常可用。还有一些开源项目可能是相关的,比如这个项目。

当使用整数除法运算符进行定点除法时,我们需要调整除法将去除比例因子的事实,即(a*2比例尺)/(b*2比例尺=(a/b),而正确的定点结果是(a/b*2比例尺)。这很容易通过将被除数预乘以2规模来解决,如在以下C实现中:

int16_t div_q15 (int16_t dividend, int16_t divisor)
{
return (int16_t)(((int32_t)dividend << 15) / (int32_t)divisor);
}

维基百科对如何使用加法、减法和移位操作逐位实现二进制除法给出了合理的概述。这些方法与小学教授的长手除法密切相关。对于FPGA,如本文所指出的,如果经常首选非恢复方法,例如:

Nikolay Sorokin,"在FPGA上实现高速定点除法器">计算机科学杂志;技术,第6卷,第1期,2006年4月,第8-11页。

以下是C代码,它显示了如何使用非还原方法对16位二进制补码操作数进行除法:

/* bit-wise non-restoring two's complement division */
void int16_div (int16_t dividend, int16_t divisor, int16_t *quot, int16_t *rem)
{
const int operand_bits = (int) (sizeof (int16_t) * CHAR_BIT);
uint16_t d = (uint16_t)divisor;
uint16_t nd = 0 - d; /* -divisor */
uint16_t r, q = 0; /* remainder, quotient */
uint32_t dd = (uint32_t)d << operand_bits; /* expanded divisor */
uint32_t pp = dividend; /* partial remainder */
int i;
for (i = operand_bits - 1; i >= 0; i--) {
if ((int32_t)(pp ^ dd) < 0) {
q = (q << 1) + 0;   /* record quotient bit -1 (as 0) */
pp = (pp << 1) + dd;
} else {
q = (q << 1) + 1;   /* record quotient bit +1 (as 1) */
pp = (pp << 1) - dd;
}
}
/* convert quotient from digit set {-1,1} to plain two's complement */
q = (q << 1) + 1;
/* remainder is upper half of partial remainder */
r = (uint16_t)(pp >> operand_bits);
/* fix up cases where we worked past a partial remainder of zero */
if (r == d) { /* remainder equal to divisor */
q = q + 1;
r = 0;
} else if (r == nd) { /* remainder equal to -divisor */
q = q - 1;
r = 0;
}
/* for truncating division, remainder must have same sign as dividend */
if (r && ((int16_t)(dividend ^ r) < 0)) {
if ((int16_t)q < 0) {
q = q + 1;
r = r - d;
} else {
q = q - 1;
r = r + d;
}
}
*quot = (int16_t)q;
*rem  = (int16_t)r;
}

请注意,有多种方法可以处理非恢复性分裂中出现的各种特殊情况。例如,在这种情况下,经常看到检测到零部分余数pp并在商比特上提前退出循环的代码。在这里,我假设FPGA实现将完全展开循环以创建流水线实现,在这种情况下,提前终止是没有帮助的。相反,对那些因忽略零的部分余数而受到影响的商进行最终校正。

为了在上面的基础上创建Q15部门,我们只需要做一个改变:增加股息。代替:

uint32_t pp = dividend; /* partial remainder */

我们现在使用这个:

uint32_t pp = dividend << 15; /* partial remainder; incorporate Q15 scaling */

包括测试框架在内的最终C代码(对不起,我不会提供使用Verilog代码的阅读)是:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>
/* bit-wise non-restoring two's complement division */
void q15_div (int16_t dividend, int16_t divisor, int16_t *quot, int16_t *rem)
{
const int operand_bits = (int) (sizeof (int16_t) * CHAR_BIT);
uint16_t d = (uint16_t)divisor;
uint16_t nd = 0 - d; /* -divisor */
uint16_t r, q = 0; /* remainder, quotient */
uint32_t dd = (uint32_t)d << operand_bits; /* expanded divisor */
uint32_t pp = dividend << 15; /* partial remainder, incorporate Q15 scaling */
int i;
for (i = operand_bits - 1; i >= 0; i--) {
if ((int32_t)(pp ^ dd) < 0) {
q = (q << 1) + 0;   /* record quotient bit -1 (as 0) */
pp = (pp << 1) + dd;
} else {
q = (q << 1) + 1;   /* record quotient bit +1 (as 1) */
pp = (pp << 1) - dd;
}
}
/* convert quotient from digit set {-1,1} to plain two's complement */
q = (q << 1) + 1;
/* remainder is upper half of partial remainder */
r = (uint16_t)(pp >> operand_bits);
/* fix up cases where we worked past a partial remainder of zero */
if (r == d) { /* remainder equal to divisor */
q = q + 1;
r = 0;
} else if (r == nd) { /* remainder equal to -divisor */
q = q - 1;
r = 0;
}
/* for truncating division, remainder must have same sign as dividend */
if (r && ((int16_t)(dividend ^ r) < 0)) {
if ((int16_t)q < 0) {
q = q + 1;
r = r - d;
} else {
q = q - 1;
r = r + d;
}
}
*quot = (int16_t)q;
*rem  = (int16_t)r;
}
int main (void)
{
uint16_t dividend, divisor, ref_q, res_q, res_r;
double quot, fxscale = (1 << 15);
dividend = 0;
do {
printf ("r%04x", dividend);
divisor = 1;
do {
quot = trunc (fxscale * (int16_t)dividend / (int16_t)divisor);
/* Q15 can only represent numbers in [-1, 1) */
if ((quot >= -1.0) && (quot < 1.0)) {
ref_q = (int16_t)((((int32_t)(int16_t)dividend) << 15) / 
((int32_t)(int16_t)divisor));
q15_div ((int16_t)dividend, (int16_t)divisor, 
(int16_t *)&res_q, (int16_t *)&res_r);
if (res_q != ref_q) {
printf ("!r dividend=%04x (%f) divisor=%04x (%f)  res=%04x (%f)  ref=%04x (%f)n", 
dividend, (int16_t)dividend / fxscale, 
divisor, (int16_t)divisor / fxscale, 
res_q, (int16_t)res_q / fxscale, 
ref_q, (int16_t)ref_q / fxscale);
}
}
divisor++;
} while (divisor);
dividend++;
} while (dividend);
return EXIT_SUCCESS;
}

最新更新