LCOV - differential code coverage report
Current view: top level - src/test/modules/test_int128 - test_int128.c (source / functions) Coverage Total Hit UNC GNC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 62.6 % 123 77 46 77
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 2 2 2
Baseline: lcov-20250906-005545-baseline Branches: 54.3 % 46 25 21 25
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 62.7 % 67 42 25 42
(30,360] days: 7.7 % 13 1 12 1
(360..) days: 79.1 % 43 34 9 34
Function coverage date bins:
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(7,30] days: 50.0 % 22 11 11 11
(360..) days: 58.3 % 24 14 10 14

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * test_int128.c
                                  4                 :                :  *    Testbed for roll-our-own 128-bit integer arithmetic.
                                  5                 :                :  *
                                  6                 :                :  * This is a standalone test program that compares the behavior of an
                                  7                 :                :  * implementation in int128.h to an (assumed correct) int128 native type.
                                  8                 :                :  *
                                  9                 :                :  * Copyright (c) 2017-2025, PostgreSQL Global Development Group
                                 10                 :                :  *
                                 11                 :                :  *
                                 12                 :                :  * IDENTIFICATION
                                 13                 :                :  *    src/test/modules/test_int128/test_int128.c
                                 14                 :                :  *
                                 15                 :                :  *-------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres_fe.h"
                                 19                 :                : 
                                 20                 :                : #include <time.h>
                                 21                 :                : 
                                 22                 :                : /* Require a native int128 type */
                                 23                 :                : #ifdef HAVE_INT128
                                 24                 :                : 
                                 25                 :                : /*
                                 26                 :                :  * By default, we test the non-native implementation in int128.h; but
                                 27                 :                :  * by predefining USE_NATIVE_INT128 to 1, you can test the native
                                 28                 :                :  * implementation, just to be sure.
                                 29                 :                :  */
                                 30                 :                : #ifndef USE_NATIVE_INT128
                                 31                 :                : #define USE_NATIVE_INT128 0
                                 32                 :                : #endif
                                 33                 :                : 
                                 34                 :                : #include "common/int128.h"
                                 35                 :                : #include "common/pg_prng.h"
                                 36                 :                : 
                                 37                 :                : /*
                                 38                 :                :  * We assume the parts of this union are laid out compatibly.
                                 39                 :                :  */
                                 40                 :                : typedef union
                                 41                 :                : {
                                 42                 :                :     int128      i128;
                                 43                 :                :     INT128      I128;
                                 44                 :                :     struct
                                 45                 :                :     {
                                 46                 :                : #ifdef WORDS_BIGENDIAN
                                 47                 :                :         int64       hi;
                                 48                 :                :         uint64      lo;
                                 49                 :                : #else
                                 50                 :                :         uint64      lo;
                                 51                 :                :         int64       hi;
                                 52                 :                : #endif
                                 53                 :                :     }           hl;
                                 54                 :                : }           test128;
                                 55                 :                : 
                                 56                 :                : #define INT128_HEX_FORMAT   "%016" PRIx64 "%016" PRIx64
                                 57                 :                : 
                                 58                 :                : /*
                                 59                 :                :  * Control version of comparator.
                                 60                 :                :  */
                                 61                 :                : static inline int
 3076 tgl@sss.pgh.pa.us          62                 :GNC     2000000 : my_int128_compare(int128 x, int128 y)
                                 63                 :                : {
                                 64         [ +  + ]:        2000000 :     if (x < y)
                                 65                 :         999299 :         return -1;
                                 66         [ +  - ]:        1000701 :     if (x > y)
                                 67                 :        1000701 :         return 1;
 3076 tgl@sss.pgh.pa.us          68                 :UNC           0 :     return 0;
                                 69                 :                : }
                                 70                 :                : 
                                 71                 :                : /*
                                 72                 :                :  * Main program.
                                 73                 :                :  *
                                 74                 :                :  * Generates a lot of random numbers and tests the implementation for each.
                                 75                 :                :  * The results should be reproducible, since we use a fixed PRNG seed.
                                 76                 :                :  *
                                 77                 :                :  * You can give a loop count if you don't like the default 1B iterations.
                                 78                 :                :  */
                                 79                 :                : int
 3076 tgl@sss.pgh.pa.us          80                 :GNC           1 : main(int argc, char **argv)
                                 81                 :                : {
                                 82                 :                :     long        count;
                                 83                 :                : 
   31 dean.a.rasheed@gmail       84                 :              1 :     pg_prng_seed(&pg_global_prng_state, (uint64) time(NULL));
                                 85                 :                : 
 3076 tgl@sss.pgh.pa.us          86         [ +  - ]:              1 :     if (argc >= 2)
                                 87                 :              1 :         count = strtol(argv[1], NULL, 0);
                                 88                 :                :     else
 3076 tgl@sss.pgh.pa.us          89                 :UNC           0 :         count = 1000000000;
                                 90                 :                : 
 3076 tgl@sss.pgh.pa.us          91         [ +  + ]:GNC     1000001 :     while (count-- > 0)
                                 92                 :                :     {
 1378                            93                 :        1000000 :         int64       x = pg_prng_uint64(&pg_global_prng_state);
                                 94                 :        1000000 :         int64       y = pg_prng_uint64(&pg_global_prng_state);
                                 95                 :        1000000 :         int64       z = pg_prng_uint64(&pg_global_prng_state);
   30 dean.a.rasheed@gmail       96                 :        1000000 :         int64       w = pg_prng_uint64(&pg_global_prng_state);
                                 97                 :        1000000 :         int32       z32 = (int32) z;
                                 98                 :                :         test128     t1;
                                 99                 :                :         test128     t2;
                                100                 :                :         test128     t3;
                                101                 :                :         int32       r1;
                                102                 :                :         int32       r2;
                                103                 :                : 
                                104                 :                :         /* check unsigned addition */
 3076 tgl@sss.pgh.pa.us         105                 :        1000000 :         t1.hl.hi = x;
                                106                 :        1000000 :         t1.hl.lo = y;
                                107                 :        1000000 :         t2 = t1;
                                108                 :        1000000 :         t1.i128 += (int128) (uint64) z;
                                109                 :        1000000 :         int128_add_uint64(&t2.I128, (uint64) z);
                                110                 :                : 
                                111   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                112                 :                :         {
   31 dean.a.rasheed@gmail      113                 :UNC           0 :             printf(INT128_HEX_FORMAT " + unsigned %016" PRIx64 "\n", x, y, z);
                                114                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                115                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
 3076 tgl@sss.pgh.pa.us         116                 :              0 :             return 1;
                                117                 :                :         }
                                118                 :                : 
                                119                 :                :         /* check signed addition */
 3076 tgl@sss.pgh.pa.us         120                 :GNC     1000000 :         t1.hl.hi = x;
                                121                 :        1000000 :         t1.hl.lo = y;
                                122                 :        1000000 :         t2 = t1;
                                123                 :        1000000 :         t1.i128 += (int128) z;
                                124                 :        1000000 :         int128_add_int64(&t2.I128, z);
                                125                 :                : 
                                126   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                127                 :                :         {
   31 dean.a.rasheed@gmail      128                 :UNC           0 :             printf(INT128_HEX_FORMAT " + signed %016" PRIx64 "\n", x, y, z);
                                129                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                130                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
 3076 tgl@sss.pgh.pa.us         131                 :              0 :             return 1;
                                132                 :                :         }
                                133                 :                : 
                                134                 :                :         /* check 128-bit signed addition */
   30 dean.a.rasheed@gmail      135                 :GNC     1000000 :         t1.hl.hi = x;
                                136                 :        1000000 :         t1.hl.lo = y;
                                137                 :        1000000 :         t2 = t1;
                                138                 :        1000000 :         t3.hl.hi = z;
                                139                 :        1000000 :         t3.hl.lo = w;
                                140                 :        1000000 :         t1.i128 += t3.i128;
                                141                 :        1000000 :         int128_add_int128(&t2.I128, t3.I128);
                                142                 :                : 
                                143   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                144                 :                :         {
   30 dean.a.rasheed@gmail      145                 :UNC           0 :             printf(INT128_HEX_FORMAT " + " INT128_HEX_FORMAT "\n", x, y, z, w);
                                146                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                147                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
                                148                 :              0 :             return 1;
                                149                 :                :         }
                                150                 :                : 
                                151                 :                :         /* check unsigned subtraction */
   30 dean.a.rasheed@gmail      152                 :GNC     1000000 :         t1.hl.hi = x;
                                153                 :        1000000 :         t1.hl.lo = y;
                                154                 :        1000000 :         t2 = t1;
                                155                 :        1000000 :         t1.i128 -= (int128) (uint64) z;
                                156                 :        1000000 :         int128_sub_uint64(&t2.I128, (uint64) z);
                                157                 :                : 
 3076 tgl@sss.pgh.pa.us         158   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                159                 :                :         {
   30 dean.a.rasheed@gmail      160                 :UNC           0 :             printf(INT128_HEX_FORMAT " - unsigned %016" PRIx64 "\n", x, y, z);
   31                           161                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                162                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
 3076 tgl@sss.pgh.pa.us         163                 :              0 :             return 1;
                                164                 :                :         }
                                165                 :                : 
                                166                 :                :         /* check signed subtraction */
   30 dean.a.rasheed@gmail      167                 :GNC     1000000 :         t1.hl.hi = x;
                                168                 :        1000000 :         t1.hl.lo = y;
                                169                 :        1000000 :         t2 = t1;
                                170                 :        1000000 :         t1.i128 -= (int128) z;
                                171                 :        1000000 :         int128_sub_int64(&t2.I128, z);
                                172                 :                : 
                                173   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                174                 :                :         {
   30 dean.a.rasheed@gmail      175                 :UNC           0 :             printf(INT128_HEX_FORMAT " - signed %016" PRIx64 "\n", x, y, z);
                                176                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                177                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
                                178                 :              0 :             return 1;
                                179                 :                :         }
                                180                 :                : 
                                181                 :                :         /* check 64x64-bit multiply-add */
   30 dean.a.rasheed@gmail      182                 :GNC     1000000 :         t1.hl.hi = x;
                                183                 :        1000000 :         t1.hl.lo = y;
                                184                 :        1000000 :         t2 = t1;
                                185                 :        1000000 :         t1.i128 += (int128) z * (int128) w;
                                186                 :        1000000 :         int128_add_int64_mul_int64(&t2.I128, z, w);
                                187                 :                : 
                                188   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                189                 :                :         {
   30 dean.a.rasheed@gmail      190                 :UNC           0 :             printf(INT128_HEX_FORMAT " + %016" PRIx64 " * %016" PRIx64 "\n", x, y, z, w);
                                191                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                192                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
                                193                 :              0 :             return 1;
                                194                 :                :         }
                                195                 :                : 
                                196                 :                :         /* check 64x64-bit multiply-subtract */
   30 dean.a.rasheed@gmail      197                 :GNC     1000000 :         t1.hl.hi = x;
                                198                 :        1000000 :         t1.hl.lo = y;
                                199                 :        1000000 :         t2 = t1;
                                200                 :        1000000 :         t1.i128 -= (int128) z * (int128) w;
                                201                 :        1000000 :         int128_sub_int64_mul_int64(&t2.I128, z, w);
                                202                 :                : 
                                203   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                204                 :                :         {
   30 dean.a.rasheed@gmail      205                 :UNC           0 :             printf(INT128_HEX_FORMAT " - %016" PRIx64 " * %016" PRIx64 "\n", x, y, z, w);
                                206                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                207                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
                                208                 :              0 :             return 1;
                                209                 :                :         }
                                210                 :                : 
                                211                 :                :         /* check 128/32-bit division */
   30 dean.a.rasheed@gmail      212                 :GNC     1000000 :         t3.hl.hi = x;
                                213                 :        1000000 :         t3.hl.lo = y;
                                214                 :        1000000 :         t1.i128 = t3.i128 / z32;
                                215                 :        1000000 :         r1 = (int32) (t3.i128 % z32);
                                216                 :        1000000 :         t2 = t3;
                                217                 :        1000000 :         int128_div_mod_int32(&t2.I128, z32, &r2);
                                218                 :                : 
                                219   [ +  -  -  + ]:        1000000 :         if (t1.hl.hi != t2.hl.hi || t1.hl.lo != t2.hl.lo)
                                220                 :                :         {
   30 dean.a.rasheed@gmail      221                 :UNC           0 :             printf(INT128_HEX_FORMAT " / signed %08X\n", t3.hl.hi, t3.hl.lo, z32);
                                222                 :              0 :             printf("native = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                223                 :              0 :             printf("result = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
                                224                 :              0 :             return 1;
                                225                 :                :         }
   30 dean.a.rasheed@gmail      226         [ -  + ]:GNC     1000000 :         if (r1 != r2)
                                227                 :                :         {
   30 dean.a.rasheed@gmail      228                 :UNC           0 :             printf(INT128_HEX_FORMAT " %% signed %08X\n", t3.hl.hi, t3.hl.lo, z32);
                                229                 :              0 :             printf("native = %08X\n", r1);
                                230                 :              0 :             printf("result = %08X\n", r2);
                                231                 :              0 :             return 1;
                                232                 :                :         }
                                233                 :                : 
                                234                 :                :         /* check comparison */
 3076 tgl@sss.pgh.pa.us         235                 :GNC     1000000 :         t1.hl.hi = x;
                                236                 :        1000000 :         t1.hl.lo = y;
                                237                 :        1000000 :         t2.hl.hi = z;
   30 dean.a.rasheed@gmail      238                 :        1000000 :         t2.hl.lo = w;
                                239                 :                : 
 3076 tgl@sss.pgh.pa.us         240         [ -  + ]:        2000000 :         if (my_int128_compare(t1.i128, t2.i128) !=
                                241                 :        1000000 :             int128_compare(t1.I128, t2.I128))
                                242                 :                :         {
 3076 tgl@sss.pgh.pa.us         243                 :UNC           0 :             printf("comparison failure: %d vs %d\n",
                                244                 :                :                    my_int128_compare(t1.i128, t2.i128),
                                245                 :                :                    int128_compare(t1.I128, t2.I128));
   31 dean.a.rasheed@gmail      246                 :              0 :             printf("arg1 = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                247                 :              0 :             printf("arg2 = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
 3076 tgl@sss.pgh.pa.us         248                 :              0 :             return 1;
                                249                 :                :         }
                                250                 :                : 
                                251                 :                :         /* check case with identical hi parts; above will hardly ever hit it */
 3076 tgl@sss.pgh.pa.us         252                 :GNC     1000000 :         t2.hl.hi = x;
                                253                 :                : 
                                254         [ -  + ]:        2000000 :         if (my_int128_compare(t1.i128, t2.i128) !=
                                255                 :        1000000 :             int128_compare(t1.I128, t2.I128))
                                256                 :                :         {
 3076 tgl@sss.pgh.pa.us         257                 :UNC           0 :             printf("comparison failure: %d vs %d\n",
                                258                 :                :                    my_int128_compare(t1.i128, t2.i128),
                                259                 :                :                    int128_compare(t1.I128, t2.I128));
   31 dean.a.rasheed@gmail      260                 :              0 :             printf("arg1 = " INT128_HEX_FORMAT "\n", t1.hl.hi, t1.hl.lo);
                                261                 :              0 :             printf("arg2 = " INT128_HEX_FORMAT "\n", t2.hl.hi, t2.hl.lo);
 3076 tgl@sss.pgh.pa.us         262                 :              0 :             return 1;
                                263                 :                :         }
                                264                 :                :     }
                                265                 :                : 
 3076 tgl@sss.pgh.pa.us         266                 :GNC           1 :     return 0;
                                267                 :                : }
                                268                 :                : 
                                269                 :                : #else                           /* ! HAVE_INT128 */
                                270                 :                : 
                                271                 :                : /*
                                272                 :                :  * For now, do nothing if we don't have a native int128 type.
                                273                 :                :  */
                                274                 :                : int
                                275                 :                : main(int argc, char **argv)
                                276                 :                : {
                                277                 :                :     printf("skipping tests: no native int128 type\n");
                                278                 :                :     return 0;
                                279                 :                : }
                                280                 :                : 
                                281                 :                : #endif
        

Generated by: LCOV version 2.4-beta