Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

128-bit struct or 2 64-bit records for performance and readibility

I am sorting 128-bit records in a large file (10s of GB). Algorithm works fine and currently using uint64_t with two of these equal to one record. This is on a modern 64-bit system.

In the interest of code readability, am wondering if create a struct like typedef struct u128t {uint64_t hi, uint64_t} u128t can be used in place of the two 64-bit records. This would make array indexing and assignment cleaner but I would have to implement a comparator function.

Is this a portable solution and should I expect this to run at the same speed as the original implementation?

Further reading:

  • http://gcc.gnu.org/onlinedocs/gcc/_005f_005fint128.html - for 128-bit systems
  • http://en.wikipedia.org/wiki/C_data_types - c types
  • Copy struct to struct in C - copy structs
like image 638
William Entriken Avatar asked Sep 19 '13 13:09

William Entriken


1 Answers

Using struct is fine.

I would do something like this:

#if COMPILER_WHICH_SUPPORTS_128_BIT_TYPE
    typedef __uint128 u128t;
    #define COMPARE(a, b) ((a) == (b))
#else
    typedef struct {
        uint64_t hi;
        uint64_t lo;
    } u128t;
    #define COMPARE(a, b)   MyCompareFunction((a), (b))
#endif
like image 138
user694733 Avatar answered Nov 15 '22 10:11

user694733