用C自定义数据类型 [英] custom data type in C

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

问题描述

我与加密工作,并需要使用一些真正大量涌现。我也使用carryless乘法需要它是由与发生在浮点数据作为其参数的函数加载它做m128i数据类型新的英特尔指令。

I am working with cryptography and need to use some really large numbers. I am also using the new Intel instruction for carryless multiplication that requires m128i data type which is done by loading it with a function that takes in floating point data as its arguments.

我需要存储2 ^ 1223整数,然后将其平方和存储价值。

I need to store 2^1223 integer and then square it and store that value as well.

我知道我可以使用GMP库,但我认为这将是更快地创建两个数据类型,这两个存储值如2 ^ 1224和2 ^ 2448。这将有较少overhead.I打算使用karatsuba乘以数字,所以我需要对数据类型进行唯一的操作就是另外我将打破数字向下,以适应m128i。

I know I can use the GMP library but I think it would be faster to create two data types that both store values like 2^1224 and 2^2448. It will have less overhead.I am going to using karatsuba to multiply the numbers so the only operation I need to perform on the data type is addition as I will be breaking the number down to fit m128i.

有人可以告诉我的方向朝着材料,可以帮助我创建整数的大小,我需要。

Can someone direct me in the direction towards material that can help me create the size of integer I need.

推荐答案

如果您需要自己的数据类型(无论它是数学等),你需要回落到结构和功能。例如:

If you need your own datatypes (regardless of whether it's for math, etc), you'll need to fall back to structures and functions. For example:

struct bignum_s {
    char bignum_data[1024];
}

(很明显,你想找到正确的尺寸,这只是一个例子)

大多数人最终typedefing它还有:

Most people end up typedefing it as well:

typedef struct bignum_s bignum;

然后创建一个需要两个(或其他)指针的数字功能,做你想做的:

And then create functions that take two (or whatever) pointers to the numbers to do what you want:

/* takes two bignums and ORs them together, putting the result back into a */
void
bignum_or(bignum *a, bignum *b) {
    int i;
    for(i = 0; i < sizeof(a->bignum_data); i++) {
        a->bignum_data[i] |= b->bignum_data[i];
    }
}

您真的要结束了界定近,你可能需要每一个功能,而这常常包括内存分配函数( bignum_new ),内存释放函数( bignum_free )和init程序( bignum_init )。即使你不需要他们现在提前做将设置你为当code需要成长,后来发展。

You really want to end up defining nearly every function you might need, and this frequently includes memory allocation functions (bignum_new), memory freeing functions (bignum_free) and init routines (bignum_init). Even if you don't need them now, doing it in advance will set you up for when the code needs to grow and develop later.

这篇关于用C自定义数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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