快速乘法code用C [英] Fast Multiplication Code in C

查看:111
本文介绍了快速乘法code用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习乘法源$ C ​​$ C和我不明白这个功能相乘 X * A 。请帮我与任何数字的例子明白这一点吗?

I am studying multiplication source code and I don't understand this function for multiplying x*A. Please help me to understand this with any numeric example please?

#define BITS_PER_LONG (8 * sizeof (unsigned long))

typedef struct matrix{
int rown;//number of rows.
int coln;//number of columns.
int rwdcnt;//number of words in a row
int alloc_size;//number of allocated bytes
unsigned long *elem;//row index.
}*binmat_t;

void mat_vec_mul(unsigned long *cR, unsigned char *x, binmat_t A)
{
    int i,j;
    unsigned long *pt;

    memset(cR, 0, A->rwdcnt*sizeof(long));
    pt = A->elem;
    for(i=0; i<A->rown; i++)
    {
        if ((x[i/8] >> (i%8)) & 1)
            for (j = 0;  j < A->rwdcnt;  ++j)
                cR[j] ^= *pt++;
        else
            pt += A->rwdcnt;
    }
}


推荐答案

x是比特,它们被存储在字符数组的向量。在x比特的数目是A-> rown。这位前pression (X [I / 8] GT;&GT;(I%8))及1 选择I x的位。

x is a vector of bits, which are stored in an array of char. The number of bits in x is A->rown. The expression (x[i/8] >> (i%8)) & 1 selects the ith bit of x.

一个是无符号长的二维数组,A-> rown行和A-> rwdcnt列。

A is a two-dimensional array of unsigned long, with A->rown rows and A->rwdcnt columns.

CR是无符号长与A-> rwdcnt元素的向量。 CR在进入这个程序清除。

cR is a vector of unsigned long with A->rwdcnt elements. cR is cleared upon entering this routine.

如果语句确定x中的I 位是否已打开。如果该位为上,A,那么第i 列被添加到CR。如果该位是关闭的,那么其他子句跳过A的本专栏由于一点可以有1或0仅值,该如 - 其他相当于由位A的列相乘并添加产品CR。

The if statement determines whether the ith bit in x is on. If the bit is on, then the ith column of A is "added" to cR. If the bit is off, then the else clause skips this column of A. Since a bit can have only values of 1 or 0, this if-else is equivalent to multiplying the column of A by the bit and adding the product to cR.

加法是由XOR完成。跨$ P $这里ptation还不清楚。这可能是因为各Cr是比特的单个阵列(和A的各列),或者它可以是在CR每个无符号长(在A)中再presents多项式在整数模2。 (这是在密码学一常见的类型。)

The "addition" is done by XOR. The interpretation here is unclear. It may be that each cR is a single array of bits (and each column of A), or it may be that each unsigned long in cR (and in A) represents a polynomial over the integers modulo two. (This is a common type in cryptography.)

我怀疑是CR位的单一阵列,而A-> rwdcnt = A->科隆/ BITS_PER_LONG。

I suspect that cR is a single array of bits, and that A->rwdcnt = A->coln / BITS_PER_LONG.

实际上,这是一个标准阵列乘法以上整数模2

Effectively, this is a standard array multiplication over the integers modulo two.

这篇关于快速乘法code用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆