7

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:

Community
  • 1
  • 1
William Entriken
  • 37,208
  • 23
  • 149
  • 195
  • With an inlined comparator and a compiler that is not completely braindead there should be no noticeable performance decrease. The only way to be sure is of course to try it and measure. – ComicSansMS Sep 19 '13 at 14:09
  • As one uses `struct u128t` beyond simple compares, it may be useful to order the fields `uint64_t hi` and `uint64_t lo` with the machine's endian-ness. Makes other 128 bit operations easier. – chux - Reinstate Monica Sep 19 '13 at 14:52

1 Answers1

5

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
user694733
  • 15,208
  • 2
  • 42
  • 68
  • 2
    +1 for COMPILER_WHICH_SUPPORTS_128_BIT_TYPE. This solution is both readable and (I guess) ideal for performance. – johan d Sep 19 '13 at 14:01