custom data type in C

后端 未结 1 1335
半阙折子戏
半阙折子戏 2021-02-14 22:59

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 wh

相关标签:
1条回答
  • 2021-02-14 23:37

    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.

    0 讨论(0)
提交回复
热议问题