custom data type in C

流过昼夜 提交于 2020-01-01 05:19:08

问题


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.

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

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.


回答1:


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];
}

(obviously you want to get the sizing right, this is just an example)

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];
    }
}

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.



来源:https://stackoverflow.com/questions/9654620/custom-data-type-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!