LCOV - code coverage report
Current view: top level - ballet/bigint - fd_uint256.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 28 28 100.0 %
Date: 2025-07-05 04:47:39 Functions: 13 32 40.6 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_bigint_uint256_h
       2             : #define HEADER_fd_src_ballet_bigint_uint256_h
       3             : 
       4             : /* Implementation of uint256. */
       5             : 
       6             : #include "../fd_ballet_base.h"
       7             : 
       8             : /* Align at most at 32 bytes.
       9             :    This way a struct containing multiple fd_uint256_t doesn't waste space
      10             :    (e.g., on avx512 FD_ALIGNED would be 64, causing each fd_uint256_t to
      11             :    consume 64 bytes instead of 32).
      12             :    Note: FD_ALIGNED implies FD_UINT256_ALIGNED, so a struct containing 1+
      13             :    fd_uint256_t can be simply defined as FD_ALIGNED, and it's also implicitly
      14             :    FD_UINT256_ALIGNED. */
      15             : #if FD_ALIGN > 32
      16             : #define FD_UINT256_ALIGNED __attribute__((aligned(32)))
      17             : #else
      18             : #define FD_UINT256_ALIGNED FD_ALIGNED
      19             : #endif
      20             : 
      21             : /* fd_uint256_t represents a uint256 as a buffer of 32 bytes,
      22             :    or equivalently (on little endian platforms) an array of 4 ulong. */
      23             : union FD_UINT256_ALIGNED fd_uint256 {
      24             :   ulong limbs[4];
      25             :   uchar buf[32];
      26             : };
      27             : typedef union fd_uint256 fd_uint256_t;
      28             : 
      29             : /* fd_uint256_bswap swaps 32 bytes. Useful to convert from/to
      30             :    little and big endian. */
      31             : static inline fd_uint256_t *
      32             : fd_uint256_bswap( fd_uint256_t *       r,
      33     9525333 :                   fd_uint256_t const * a ) {
      34     9525333 :   ulong r3 = fd_ulong_bswap( a->limbs[0] );
      35     9525333 :   ulong r2 = fd_ulong_bswap( a->limbs[1] );
      36     9525333 :   ulong r1 = fd_ulong_bswap( a->limbs[2] );
      37     9525333 :   ulong r0 = fd_ulong_bswap( a->limbs[3] );
      38     9525333 :   r->limbs[3] = r3;
      39     9525333 :   r->limbs[2] = r2;
      40     9525333 :   r->limbs[1] = r1;
      41     9525333 :   r->limbs[0] = r0;
      42     9525333 :   return r;
      43     9525333 : }
      44             : 
      45             : /* fd_uint256_eq returns 1 is a == b, 0 otherwise. */
      46             : static inline int
      47             : fd_uint256_eq( fd_uint256_t const * a,
      48      240978 :                fd_uint256_t const * b ) {
      49      240978 :   return ( a->limbs[0] == b->limbs[0] )
      50      240978 :       && ( a->limbs[1] == b->limbs[1] )
      51      240978 :       && ( a->limbs[2] == b->limbs[2] )
      52      240978 :       && ( a->limbs[3] == b->limbs[3] );
      53      240978 : }
      54             : 
      55             : /* fd_uint256_cmp returns 0 is a == b, -1 if a < b, 1 if a > b. */
      56             : static inline int
      57             : fd_uint256_cmp( fd_uint256_t const * a,
      58   106061472 :                 fd_uint256_t const * b ) {
      59   106062393 :   for( int i=3; i>=0; i-- ) {
      60   106062390 :     if( a->limbs[i] != b->limbs[i] ) {
      61   106061469 :       return a->limbs[i] > b->limbs[i] ? 1 : -1;
      62   106061469 :     }
      63   106062390 :   }
      64           3 :   return 0;
      65   106061472 : }
      66             : 
      67             : /* fd_uint256_bit returns the i-th bit of a.
      68             :    Important: the return value is 0, non-zero, it's NOT 0, 1. */
      69             : static inline ulong
      70             : fd_uint256_bit( fd_uint256_t const * a,
      71    11235783 :                 int                  i ) {
      72    11235783 :   return a->limbs[i / 64] & (1UL << (i % 64));
      73    11235783 : }
      74             : 
      75             : #include "./fd_uint256_mul.h"
      76             : 
      77             : #endif /* HEADER_fd_src_ballet_bigint_uint256_h */

Generated by: LCOV version 1.14