/ Check-in [82cbebb8]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Make use of the __buildin_OP_overflow() functions from GCC when doing 64-bit signed integer arithmetic.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gnu-safe-math
Files: files | file ages | folders
SHA1: 82cbebb8ee4484f13e5f48d305e20e73063f273e
User & Date: drh 2017-01-03 17:33:43
Context
2017-01-03
18:05
Use the GCC built-in __sync_fetch_and_sub() to make the sqlite3StatusDown() routine atomic, and thereby avoid some mutexing. check-in: f69ce75b user: drh tags: gnu-safe-math
17:33
Make use of the __buildin_OP_overflow() functions from GCC when doing 64-bit signed integer arithmetic. check-in: 82cbebb8 user: drh tags: gnu-safe-math
14:45
Add the experimental affinity() SQL function when SQLITE_DEBUG is defined. check-in: bed0eaa5 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/util.c.

  1275   1275   /*
  1276   1276   ** Attempt to add, substract, or multiply the 64-bit signed value iB against
  1277   1277   ** the other 64-bit signed integer at *pA and store the result in *pA.
  1278   1278   ** Return 0 on success.  Or if the operation would have resulted in an
  1279   1279   ** overflow, leave *pA unchanged and return 1.
  1280   1280   */
  1281   1281   int sqlite3AddInt64(i64 *pA, i64 iB){
         1282  +#if !defined(SQLITE_DISABLE_INTRINSIC) \
         1283  +    && defined(__GNUC__) && GCC_VERSION>=5004000
         1284  +  return __builtin_add_overflow(*pA, iB, pA);
         1285  +#else
  1282   1286     i64 iA = *pA;
  1283   1287     testcase( iA==0 ); testcase( iA==1 );
  1284   1288     testcase( iB==-1 ); testcase( iB==0 );
  1285   1289     if( iB>=0 ){
  1286   1290       testcase( iA>0 && LARGEST_INT64 - iA == iB );
  1287   1291       testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
  1288   1292       if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
................................................................................
  1289   1293     }else{
  1290   1294       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
  1291   1295       testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
  1292   1296       if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
  1293   1297     }
  1294   1298     *pA += iB;
  1295   1299     return 0; 
         1300  +#endif
  1296   1301   }
  1297   1302   int sqlite3SubInt64(i64 *pA, i64 iB){
         1303  +#if !defined(SQLITE_DISABLE_INTRINSIC) \
         1304  +    && defined(__GNUC__) && GCC_VERSION>=5004000
         1305  +  return __builtin_sub_overflow(*pA, iB, pA);
         1306  +#else
  1298   1307     testcase( iB==SMALLEST_INT64+1 );
  1299   1308     if( iB==SMALLEST_INT64 ){
  1300   1309       testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
  1301   1310       if( (*pA)>=0 ) return 1;
  1302   1311       *pA -= iB;
  1303   1312       return 0;
  1304   1313     }else{
  1305   1314       return sqlite3AddInt64(pA, -iB);
  1306   1315     }
         1316  +#endif
  1307   1317   }
  1308   1318   int sqlite3MulInt64(i64 *pA, i64 iB){
         1319  +#if !defined(SQLITE_DISABLE_INTRINSIC) \
         1320  +    && defined(__GNUC__) && GCC_VERSION>=5004000
         1321  +  return __builtin_mul_overflow(*pA, iB, pA);
         1322  +#else
  1309   1323     i64 iA = *pA;
  1310   1324     if( iB>0 ){
  1311   1325       if( iA>LARGEST_INT64/iB ) return 1;
  1312   1326       if( iA<SMALLEST_INT64/iB ) return 1;
  1313   1327     }else if( iB<0 ){
  1314   1328       if( iA>0 ){
  1315   1329         if( iB<SMALLEST_INT64/iA ) return 1;
................................................................................
  1317   1331         if( iB==SMALLEST_INT64 ) return 1;
  1318   1332         if( iA==SMALLEST_INT64 ) return 1;
  1319   1333         if( -iA>LARGEST_INT64/-iB ) return 1;
  1320   1334       }
  1321   1335     }
  1322   1336     *pA = iA*iB;
  1323   1337     return 0;
         1338  +#endif
  1324   1339   }
  1325   1340   
  1326   1341   /*
  1327   1342   ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
  1328   1343   ** if the integer has a value of -2147483648, return +2147483647
  1329   1344   */
  1330   1345   int sqlite3AbsInt32(int x){