You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

852 lines
39 KiB

  1. diff --git a/liboath/gl/fflush.c b/liboath/gl/fflush.c
  2. index 3664842..a140b7a 100644
  3. --- a/liboath/gl/fflush.c
  4. +++ b/liboath/gl/fflush.c
  5. @@ -1,18 +1,18 @@
  6. /* fflush.c -- allow flushing input streams
  7. - Copyright (C) 2007-2016 Free Software Foundation, Inc.
  8. + Copyright (C) 2007-2018 Free Software Foundation, Inc.
  9. This program is free software: you can redistribute it and/or modify
  10. - it under the terms of the GNU Lesser General Public License as published by
  11. - the Free Software Foundation; either version 2.1 of the License, or
  12. + it under the terms of the GNU General Public License as published by
  13. + the Free Software Foundation; either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. - GNU Lesser General Public License for more details.
  19. + GNU General Public License for more details.
  20. - You should have received a copy of the GNU Lesser General Public License
  21. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  22. + You should have received a copy of the GNU General Public License
  23. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  24. /* Written by Eric Blake. */
  25. @@ -33,7 +33,8 @@
  26. #undef fflush
  27. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  28. +#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  29. +/* GNU libc, BeOS, Haiku, Linux libc5 */
  30. /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
  31. static void
  32. @@ -51,7 +52,7 @@ static void
  33. clear_ungetc_buffer (FILE *fp)
  34. {
  35. # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  36. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  37. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  38. if (HASUB (fp))
  39. {
  40. fp_->_p += fp_->_r;
  41. @@ -63,7 +64,7 @@ clear_ungetc_buffer (FILE *fp)
  42. fp->_ungetc_count = 0;
  43. fp->_rcount = - fp->_rcount;
  44. }
  45. -# elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  46. +# elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, MSVC, NonStop Kernel, OpenVMS */
  47. /* Nothing to do. */
  48. # else /* other implementations */
  49. fseeko (fp, 0, SEEK_CUR);
  50. @@ -72,10 +73,11 @@ clear_ungetc_buffer (FILE *fp)
  51. #endif
  52. -#if ! (defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */)
  53. +#if ! (defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1)
  54. +/* GNU libc, BeOS, Haiku, Linux libc5 */
  55. # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
  56. -/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  57. +/* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  58. static int
  59. disable_seek_optimization (FILE *fp)
  60. @@ -98,7 +100,7 @@ update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER,
  61. off_t pos _GL_UNUSED_PARAMETER)
  62. {
  63. # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  64. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  65. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  66. # if defined __CYGWIN__
  67. /* fp_->_offset is typed as an integer. */
  68. fp_->_offset = pos;
  69. @@ -148,7 +150,8 @@ rpl_fflush (FILE *stream)
  70. if (stream == NULL || ! freading (stream))
  71. return fflush (stream);
  72. -#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  73. +#if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1
  74. + /* GNU libc, BeOS, Haiku, Linux libc5 */
  75. clear_ungetc_buffer_preserving_position (stream);
  76. @@ -199,7 +202,7 @@ rpl_fflush (FILE *stream)
  77. }
  78. # if (defined __sferror || defined __DragonFly__ || defined __ANDROID__) && defined __SNPT
  79. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  80. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  81. {
  82. /* Disable seek optimization for the next fseeko call. This tells the
  83. diff --git a/liboath/gl/fpurge.c b/liboath/gl/fpurge.c
  84. index acf5905..f9c2d25 100644
  85. --- a/liboath/gl/fpurge.c
  86. +++ b/liboath/gl/fpurge.c
  87. @@ -62,7 +62,7 @@ fpurge (FILE *fp)
  88. /* Most systems provide FILE as a struct and the necessary bitmask in
  89. <stdio.h>, because they need it for implementing getc() and putc() as
  90. fast macros. */
  91. -# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  92. +# if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  93. fp->_IO_read_end = fp->_IO_read_ptr;
  94. fp->_IO_write_ptr = fp->_IO_write_base;
  95. /* Avoid memory leak when there is an active ungetc buffer. */
  96. diff --git a/liboath/gl/freading.c b/liboath/gl/freading.c
  97. index 8ab19fd..54c3d5a 100644
  98. --- a/liboath/gl/freading.c
  99. +++ b/liboath/gl/freading.c
  100. @@ -31,7 +31,7 @@ freading (FILE *fp)
  101. /* Most systems provide FILE as a struct and the necessary bitmask in
  102. <stdio.h>, because they need it for implementing getc() and putc() as
  103. fast macros. */
  104. -# if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  105. +# if defined _IO_ftrylockfile || __GNU_LIBRARY__ /* GNU libc, BeOS, Haiku, Linux libc5 */
  106. return ((fp->_flags & _IO_NO_WRITES) != 0
  107. || ((fp->_flags & (_IO_NO_READS | _IO_CURRENTLY_PUTTING)) == 0
  108. && fp->_IO_read_base != NULL));
  109. diff --git a/liboath/gl/stdio-impl.h b/liboath/gl/stdio-impl.h
  110. index 4c02c9f..393ef0c 100644
  111. --- a/liboath/gl/stdio-impl.h
  112. +++ b/liboath/gl/stdio-impl.h
  113. @@ -1,23 +1,29 @@
  114. /* Implementation details of FILE streams.
  115. - Copyright (C) 2007-2008, 2010-2016 Free Software Foundation, Inc.
  116. + Copyright (C) 2007-2008, 2010-2018 Free Software Foundation, Inc.
  117. This program is free software: you can redistribute it and/or modify
  118. - it under the terms of the GNU Lesser General Public License as published by
  119. - the Free Software Foundation; either version 2.1 of the License, or
  120. + it under the terms of the GNU General Public License as published by
  121. + the Free Software Foundation; either version 3 of the License, or
  122. (at your option) any later version.
  123. This program is distributed in the hope that it will be useful,
  124. but WITHOUT ANY WARRANTY; without even the implied warranty of
  125. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  126. - GNU Lesser General Public License for more details.
  127. + GNU General Public License for more details.
  128. - You should have received a copy of the GNU Lesser General Public License
  129. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  130. + You should have received a copy of the GNU General Public License
  131. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  132. /* Many stdio implementations have the same logic and therefore can share
  133. the same implementation of stdio extension API, except that some fields
  134. have different naming conventions, or their access requires some casts. */
  135. +/* Glibc 2.28 made _IO_IN_BACKUP private. For now, work around this
  136. + problem by defining it ourselves. FIXME: Do not rely on glibc
  137. + internals. */
  138. +#if !defined _IO_IN_BACKUP && defined _IO_EOF_SEEN
  139. +# define _IO_IN_BACKUP 0x100
  140. +#endif
  141. /* BSD stdio derived implementations. */
  142. @@ -29,10 +35,10 @@
  143. #include <errno.h> /* For detecting Plan9. */
  144. #if defined __sferror || defined __DragonFly__ || defined __ANDROID__
  145. - /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  146. + /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Minix 3, Android */
  147. # if defined __DragonFly__ /* DragonFly */
  148. - /* See <http://www.dragonflybsd.org/cvsweb/src/lib/libc/stdio/priv_stdio.h?rev=HEAD&content-type=text/x-cvsweb-markup>. */
  149. + /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/lib/libc/stdio/priv_stdio.h>. */
  150. # define fp_ ((struct { struct __FILE_public pub; \
  151. struct { unsigned char *_base; int _size; } _bf; \
  152. void *cookie; \
  153. @@ -49,30 +55,84 @@
  154. fpos_t _offset; \
  155. /* More fields, not relevant here. */ \
  156. } *) fp)
  157. - /* See <http://www.dragonflybsd.org/cvsweb/src/include/stdio.h?rev=HEAD&content-type=text/x-cvsweb-markup>. */
  158. + /* See <https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/include/stdio.h>. */
  159. # define _p pub._p
  160. # define _flags pub._flags
  161. # define _r pub._r
  162. # define _w pub._w
  163. +# elif defined __ANDROID__ /* Android */
  164. + /* Up to this commit from 2015-10-12
  165. + <https://android.googlesource.com/platform/bionic.git/+/f0141dfab10a4b332769d52fa76631a64741297a>
  166. + the innards of FILE were public, and fp_ub could be defined like for OpenBSD,
  167. + see <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/fileext.h>
  168. + and <https://android.googlesource.com/platform/bionic.git/+/e78392637d5086384a5631ddfdfa8d7ec8326ee3/libc/stdio/local.h>.
  169. + After this commit, the innards of FILE are hidden. */
  170. +# define fp_ ((struct { unsigned char *_p; \
  171. + int _r; \
  172. + int _w; \
  173. + int _flags; \
  174. + int _file; \
  175. + struct { unsigned char *_base; size_t _size; } _bf; \
  176. + int _lbfsize; \
  177. + void *_cookie; \
  178. + void *_close; \
  179. + void *_read; \
  180. + void *_seek; \
  181. + void *_write; \
  182. + struct { unsigned char *_base; size_t _size; } _ext; \
  183. + unsigned char *_up; \
  184. + int _ur; \
  185. + unsigned char _ubuf[3]; \
  186. + unsigned char _nbuf[1]; \
  187. + struct { unsigned char *_base; size_t _size; } _lb; \
  188. + int _blksize; \
  189. + fpos_t _offset; \
  190. + /* More fields, not relevant here. */ \
  191. + } *) fp)
  192. # else
  193. # define fp_ fp
  194. # endif
  195. -# if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __ANDROID__ /* NetBSD >= 1.5ZA, OpenBSD, Android */
  196. +# if (defined __NetBSD__ && __NetBSD_Version__ >= 105270000) || defined __OpenBSD__ || defined __minix /* NetBSD >= 1.5ZA, OpenBSD, Minix 3 */
  197. /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
  198. - and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
  199. + and <https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
  200. + and <https://github.com/Stichting-MINIX-Research-Foundation/minix/blob/master/lib/libc/stdio/fileext.h> */
  201. struct __sfileext
  202. {
  203. struct __sbuf _ub; /* ungetc buffer */
  204. /* More fields, not relevant here. */
  205. };
  206. # define fp_ub ((struct __sfileext *) fp->_ext._base)->_ub
  207. -# else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin, Android */
  208. +# elif defined __ANDROID__ /* Android */
  209. + struct __sfileext
  210. + {
  211. + struct { unsigned char *_base; size_t _size; } _ub; /* ungetc buffer */
  212. + /* More fields, not relevant here. */
  213. + };
  214. +# define fp_ub ((struct __sfileext *) fp_->_ext._base)->_ub
  215. +# else /* FreeBSD, NetBSD <= 1.5Z, DragonFly, Mac OS X, Cygwin */
  216. # define fp_ub fp_->_ub
  217. # endif
  218. # define HASUB(fp) (fp_ub._base != NULL)
  219. +# if defined __ANDROID__ /* Android */
  220. + /* Needed after this commit from 2016-01-25
  221. + <https://android.googlesource.com/platform/bionic.git/+/e70e0e9267d069bf56a5078c99307e08a7280de7> */
  222. +# ifndef __SEOF
  223. +# define __SLBF 1
  224. +# define __SNBF 2
  225. +# define __SRD 4
  226. +# define __SWR 8
  227. +# define __SRW 0x10
  228. +# define __SEOF 0x20
  229. +# define __SERR 0x40
  230. +# endif
  231. +# ifndef __SOFF
  232. +# define __SOFF 0x1000
  233. +# endif
  234. +# endif
  235. +
  236. #endif
  237. @@ -81,7 +141,7 @@
  238. #ifdef __TANDEM /* NonStop Kernel */
  239. # ifndef _IOERR
  240. /* These values were determined by the program 'stdioext-flags' at
  241. - <http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00165.html>. */
  242. + <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
  243. # define _IOERR 0x40
  244. # define _IOREAD 0x80
  245. # define _IOWRT 0x4
  246. @@ -99,6 +159,8 @@
  247. int _file; \
  248. unsigned int _flag; \
  249. } *) fp)
  250. +# elif defined __VMS /* OpenVMS */
  251. +# define fp_ ((struct _iobuf *) fp)
  252. # else
  253. # define fp_ fp
  254. # endif
  255. @@ -110,4 +172,31 @@
  256. # define _flag __flag
  257. # endif
  258. +#elif defined _WIN32 && ! defined __CYGWIN__ /* newer Windows with MSVC */
  259. +
  260. +/* <stdio.h> does not define the innards of FILE any more. */
  261. +# define WINDOWS_OPAQUE_FILE
  262. +
  263. +struct _gl_real_FILE
  264. +{
  265. + /* Note: Compared to older Windows and to mingw, it has the fields
  266. + _base and _cnt swapped. */
  267. + unsigned char *_ptr;
  268. + unsigned char *_base;
  269. + int _cnt;
  270. + int _flag;
  271. + int _file;
  272. + int _charbuf;
  273. + int _bufsiz;
  274. +};
  275. +# define fp_ ((struct _gl_real_FILE *) fp)
  276. +
  277. +/* These values were determined by a program similar to the one at
  278. + <https://lists.gnu.org/r/bug-gnulib/2010-12/msg00165.html>. */
  279. +# define _IOREAD 0x1
  280. +# define _IOWRT 0x2
  281. +# define _IORW 0x4
  282. +# define _IOEOF 0x8
  283. +# define _IOERR 0x10
  284. +
  285. #endif
  286. diff --git a/libpskc/gl/intprops.h b/libpskc/gl/intprops.h
  287. index feb02c3..af456ff 100644
  288. --- a/libpskc/gl/intprops.h
  289. +++ b/libpskc/gl/intprops.h
  290. @@ -1,6 +1,6 @@
  291. /* intprops.h -- properties of integer types
  292. - Copyright (C) 2001-2016 Free Software Foundation, Inc.
  293. + Copyright (C) 2001-2018 Free Software Foundation, Inc.
  294. This program is free software: you can redistribute it and/or modify it
  295. under the terms of the GNU Lesser General Public License as published
  296. @@ -13,7 +13,7 @@
  297. GNU Lesser General Public License for more details.
  298. You should have received a copy of the GNU Lesser General Public License
  299. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  300. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  301. /* Written by Paul Eggert. */
  302. @@ -21,13 +21,12 @@
  303. #define _GL_INTPROPS_H
  304. #include <limits.h>
  305. -#include <verify.h>
  306. /* Return a value with the common real type of E and V and the value of V. */
  307. #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
  308. /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
  309. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
  310. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
  311. #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
  312. /* The extra casts in the following macros work around compiler bugs,
  313. @@ -47,12 +46,16 @@
  314. /* Minimum and maximum values for integer types and expressions. */
  315. +/* The width in bits of the integer type or expression T.
  316. + Padding bits are not supported; this is checked at compile-time below. */
  317. +#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  318. +
  319. /* The maximum and minimum values for the integer type T. */
  320. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  321. #define TYPE_MAXIMUM(t) \
  322. ((t) (! TYPE_SIGNED (t) \
  323. ? (t) -1 \
  324. - : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  325. + : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  326. /* The maximum and minimum values for the type of the expression E,
  327. after integer promotion. E should not have side effects. */
  328. @@ -65,29 +68,23 @@
  329. ? _GL_SIGNED_INT_MAXIMUM (e) \
  330. : _GL_INT_NEGATE_CONVERT (e, 1))
  331. #define _GL_SIGNED_INT_MAXIMUM(e) \
  332. - (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
  333. + (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
  334. +
  335. +/* Work around OpenVMS incompatibility with C99. */
  336. +#if !defined LLONG_MAX && defined __INT64_MAX
  337. +# define LLONG_MAX __INT64_MAX
  338. +# define LLONG_MIN __INT64_MIN
  339. +#endif
  340. /* This include file assumes that signed types are two's complement without
  341. padding bits; the above macros have undefined behavior otherwise.
  342. If this is a problem for you, please let us know how to fix it for your host.
  343. - As a sanity check, test the assumption for some signed types that
  344. - <limits.h> bounds. */
  345. -verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
  346. -verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
  347. -verify (TYPE_MINIMUM (short int) == SHRT_MIN);
  348. -verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
  349. -verify (TYPE_MINIMUM (int) == INT_MIN);
  350. -verify (TYPE_MAXIMUM (int) == INT_MAX);
  351. -verify (TYPE_MINIMUM (long int) == LONG_MIN);
  352. -verify (TYPE_MAXIMUM (long int) == LONG_MAX);
  353. -#ifdef LLONG_MAX
  354. -verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
  355. -verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  356. -#endif
  357. + This assumption is tested by the intprops-tests module. */
  358. /* Does the __typeof__ keyword work? This could be done by
  359. 'configure', but for now it's easier to do it by hand. */
  360. -#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
  361. +#if (2 <= __GNUC__ \
  362. + || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  363. || (0x5110 <= __SUNPRO_C && !__STDC__))
  364. # define _GL_HAVE___TYPEOF__ 1
  365. #else
  366. @@ -116,8 +113,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  367. signed, this macro may overestimate the true bound by one byte when
  368. applied to unsigned types of size 2, 4, 16, ... bytes. */
  369. #define INT_STRLEN_BOUND(t) \
  370. - (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
  371. - - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  372. + (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  373. + _GL_SIGNED_TYPE_OR_EXPR (t))
  374. /* Bound on buffer size needed to represent an integer type or expression T,
  375. @@ -183,7 +179,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  376. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  377. See above for restrictions. Avoid && and || as they tickle
  378. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  379. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
  380. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  381. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  382. ((b) < 0 \
  383. ? ((a) < 0 \
  384. @@ -222,20 +218,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  385. ? (a) < (min) >> (b) \
  386. : (max) >> (b) < (a))
  387. -/* True if __builtin_add_overflow (A, B, P) works when P is null. */
  388. -#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__)
  389. +/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
  390. +#if 5 <= __GNUC__ && !defined __ICC
  391. +# define _GL_HAS_BUILTIN_OVERFLOW 1
  392. +#else
  393. +# define _GL_HAS_BUILTIN_OVERFLOW 0
  394. +#endif
  395. +
  396. +/* True if __builtin_add_overflow_p (A, B, C) works. */
  397. +#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  398. /* The _GL*_OVERFLOW macros have the same restrictions as the
  399. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  400. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  401. that the result (e.g., A + B) has that type. */
  402. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  403. -# define _GL_ADD_OVERFLOW(a, b, min, max)
  404. - __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0)
  405. -# define _GL_SUBTRACT_OVERFLOW(a, b, min, max)
  406. - __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0)
  407. -# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)
  408. - __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0)
  409. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  410. +# define _GL_ADD_OVERFLOW(a, b, min, max) \
  411. + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  412. +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  413. + __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  414. +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  415. + __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  416. #else
  417. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  418. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  419. @@ -315,7 +318,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  420. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  421. #define INT_SUBTRACT_OVERFLOW(a, b) \
  422. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  423. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  424. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  425. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  426. #else
  427. # define INT_NEGATE_OVERFLOW(a) \
  428. @@ -349,10 +352,6 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  429. #define INT_MULTIPLY_WRAPV(a, b, r) \
  430. _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
  431. -#ifndef __has_builtin
  432. -# define __has_builtin(x) 0
  433. -#endif
  434. -
  435. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  436. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  437. https://llvm.org/bugs/show_bug.cgi?id=25390
  438. @@ -369,17 +368,17 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  439. the operation. BUILTIN is the builtin operation, and OVERFLOW the
  440. overflow predicate. Return 1 if the result overflows. See above
  441. for restrictions. */
  442. -#if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
  443. +#if _GL_HAS_BUILTIN_OVERFLOW
  444. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
  445. #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  446. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  447. (_Generic \
  448. (*(r), \
  449. signed char: \
  450. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  451. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  452. signed char, SCHAR_MIN, SCHAR_MAX), \
  453. short int: \
  454. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  455. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  456. short int, SHRT_MIN, SHRT_MAX), \
  457. int: \
  458. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  459. @@ -393,10 +392,10 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  460. #else
  461. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  462. (sizeof *(r) == sizeof (signed char) \
  463. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  464. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  465. signed char, SCHAR_MIN, SCHAR_MAX) \
  466. : sizeof *(r) == sizeof (short int) \
  467. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  468. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  469. short int, SHRT_MIN, SHRT_MAX) \
  470. : sizeof *(r) == sizeof (int) \
  471. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  472. @@ -412,15 +411,14 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  473. # else
  474. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  475. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  476. - long int, LONG_MIN, LONG_MAX))
  477. + long int, LONG_MIN, LONG_MAX)
  478. # endif
  479. #endif
  480. /* Store the low-order bits of A <op> B into *R, where the operation
  481. is given by OP. Use the unsigned type UT for calculation to avoid
  482. - overflow problems. *R's type is T, with extremal values TMIN and
  483. - TMAX. T must be a signed integer type. Return 1 if the result
  484. - overflows. */
  485. + overflow problems. *R's type is T, with extrema TMIN and TMAX.
  486. + T must be a signed integer type. Return 1 if the result overflows. */
  487. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  488. (sizeof ((a) op (b)) < sizeof (t) \
  489. ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
  490. @@ -429,17 +427,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  491. ((overflow (a, b) \
  492. || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
  493. || (tmax) < ((a) op (b))) \
  494. - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
  495. - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
  496. -
  497. -/* Return A <op> B, where the operation is given by OP. Use the
  498. - unsigned type UT for calculation to avoid overflow problems.
  499. - Convert the result to type T without overflow by subtracting TMIN
  500. - from large values before converting, and adding it afterwards.
  501. - Compilers can optimize all the operations except OP. */
  502. -#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
  503. - (((ut) (a) op (ut) (b)) <= (tmax) \
  504. - ? (t) ((ut) (a) op (ut) (b)) \
  505. - : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
  506. + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  507. + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  508. +
  509. +/* Return the low-order bits of A <op> B, where the operation is given
  510. + by OP. Use the unsigned type UT for calculation to avoid undefined
  511. + behavior on signed integer overflow, and convert the result to type T.
  512. + UT is at least as wide as T and is no narrower than unsigned int,
  513. + T is two's complement, and there is no padding or trap representations.
  514. + Assume that converting UT to T yields the low-order bits, as is
  515. + done in all known two's-complement C compilers. E.g., see:
  516. + https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  517. +
  518. + According to the C standard, converting UT to T yields an
  519. + implementation-defined result or signal for values outside T's
  520. + range. However, code that works around this theoretical problem
  521. + runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  522. + https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  523. + As the compiler bug is real, don't try to work around the
  524. + theoretical problem. */
  525. +
  526. +#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  527. + ((t) ((ut) (a) op (ut) (b)))
  528. #endif /* _GL_INTPROPS_H */
  529. diff --git a/oathtool/gl/intprops.h b/oathtool/gl/intprops.h
  530. index e1fce5c..af456ff 100644
  531. --- a/oathtool/gl/intprops.h
  532. +++ b/oathtool/gl/intprops.h
  533. @@ -1,19 +1,19 @@
  534. /* intprops.h -- properties of integer types
  535. - Copyright (C) 2001-2016 Free Software Foundation, Inc.
  536. + Copyright (C) 2001-2018 Free Software Foundation, Inc.
  537. This program is free software: you can redistribute it and/or modify it
  538. - under the terms of the GNU General Public License as published
  539. - by the Free Software Foundation; either version 3 of the License, or
  540. + under the terms of the GNU Lesser General Public License as published
  541. + by the Free Software Foundation; either version 2.1 of the License, or
  542. (at your option) any later version.
  543. This program is distributed in the hope that it will be useful,
  544. but WITHOUT ANY WARRANTY; without even the implied warranty of
  545. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  546. - GNU General Public License for more details.
  547. + GNU Lesser General Public License for more details.
  548. - You should have received a copy of the GNU General Public License
  549. - along with this program. If not, see <http://www.gnu.org/licenses/>. */
  550. + You should have received a copy of the GNU Lesser General Public License
  551. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  552. /* Written by Paul Eggert. */
  553. @@ -21,13 +21,12 @@
  554. #define _GL_INTPROPS_H
  555. #include <limits.h>
  556. -#include <verify.h>
  557. /* Return a value with the common real type of E and V and the value of V. */
  558. #define _GL_INT_CONVERT(e, v) (0 * (e) + (v))
  559. /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
  560. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */
  561. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
  562. #define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v))
  563. /* The extra casts in the following macros work around compiler bugs,
  564. @@ -47,12 +46,16 @@
  565. /* Minimum and maximum values for integer types and expressions. */
  566. +/* The width in bits of the integer type or expression T.
  567. + Padding bits are not supported; this is checked at compile-time below. */
  568. +#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  569. +
  570. /* The maximum and minimum values for the integer type T. */
  571. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  572. #define TYPE_MAXIMUM(t) \
  573. ((t) (! TYPE_SIGNED (t) \
  574. ? (t) -1 \
  575. - : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  576. + : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  577. /* The maximum and minimum values for the type of the expression E,
  578. after integer promotion. E should not have side effects. */
  579. @@ -65,29 +68,23 @@
  580. ? _GL_SIGNED_INT_MAXIMUM (e) \
  581. : _GL_INT_NEGATE_CONVERT (e, 1))
  582. #define _GL_SIGNED_INT_MAXIMUM(e) \
  583. - (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1)
  584. + (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
  585. +
  586. +/* Work around OpenVMS incompatibility with C99. */
  587. +#if !defined LLONG_MAX && defined __INT64_MAX
  588. +# define LLONG_MAX __INT64_MAX
  589. +# define LLONG_MIN __INT64_MIN
  590. +#endif
  591. /* This include file assumes that signed types are two's complement without
  592. padding bits; the above macros have undefined behavior otherwise.
  593. If this is a problem for you, please let us know how to fix it for your host.
  594. - As a sanity check, test the assumption for some signed types that
  595. - <limits.h> bounds. */
  596. -verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
  597. -verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
  598. -verify (TYPE_MINIMUM (short int) == SHRT_MIN);
  599. -verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
  600. -verify (TYPE_MINIMUM (int) == INT_MIN);
  601. -verify (TYPE_MAXIMUM (int) == INT_MAX);
  602. -verify (TYPE_MINIMUM (long int) == LONG_MIN);
  603. -verify (TYPE_MAXIMUM (long int) == LONG_MAX);
  604. -#ifdef LLONG_MAX
  605. -verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
  606. -verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  607. -#endif
  608. + This assumption is tested by the intprops-tests module. */
  609. /* Does the __typeof__ keyword work? This could be done by
  610. 'configure', but for now it's easier to do it by hand. */
  611. -#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \
  612. +#if (2 <= __GNUC__ \
  613. + || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  614. || (0x5110 <= __SUNPRO_C && !__STDC__))
  615. # define _GL_HAVE___TYPEOF__ 1
  616. #else
  617. @@ -116,8 +113,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  618. signed, this macro may overestimate the true bound by one byte when
  619. applied to unsigned types of size 2, 4, 16, ... bytes. */
  620. #define INT_STRLEN_BOUND(t) \
  621. - (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \
  622. - - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  623. + (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  624. + _GL_SIGNED_TYPE_OR_EXPR (t))
  625. /* Bound on buffer size needed to represent an integer type or expression T,
  626. @@ -183,7 +179,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  627. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  628. See above for restrictions. Avoid && and || as they tickle
  629. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  630. - <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
  631. + <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  632. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  633. ((b) < 0 \
  634. ? ((a) < 0 \
  635. @@ -222,20 +218,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  636. ? (a) < (min) >> (b) \
  637. : (max) >> (b) < (a))
  638. -/* True if __builtin_add_overflow (A, B, P) works when P is null. */
  639. -#define _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL (7 <= __GNUC__)
  640. +/* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
  641. +#if 5 <= __GNUC__ && !defined __ICC
  642. +# define _GL_HAS_BUILTIN_OVERFLOW 1
  643. +#else
  644. +# define _GL_HAS_BUILTIN_OVERFLOW 0
  645. +#endif
  646. +
  647. +/* True if __builtin_add_overflow_p (A, B, C) works. */
  648. +#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  649. /* The _GL*_OVERFLOW macros have the same restrictions as the
  650. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  651. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  652. that the result (e.g., A + B) has that type. */
  653. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  654. -# define _GL_ADD_OVERFLOW(a, b, min, max)
  655. - __builtin_add_overflow (a, b, (__typeof__ ((a) + (b)) *) 0)
  656. -# define _GL_SUBTRACT_OVERFLOW(a, b, min, max)
  657. - __builtin_sub_overflow (a, b, (__typeof__ ((a) - (b)) *) 0)
  658. -# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)
  659. - __builtin_mul_overflow (a, b, (__typeof__ ((a) * (b)) *) 0)
  660. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  661. +# define _GL_ADD_OVERFLOW(a, b, min, max) \
  662. + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  663. +# define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  664. + __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  665. +# define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  666. + __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  667. #else
  668. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  669. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  670. @@ -315,7 +318,7 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  671. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  672. #define INT_SUBTRACT_OVERFLOW(a, b) \
  673. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  674. -#if _GL_HAS_BUILTIN_OVERFLOW_WITH_NULL
  675. +#if _GL_HAS_BUILTIN_OVERFLOW_P
  676. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  677. #else
  678. # define INT_NEGATE_OVERFLOW(a) \
  679. @@ -349,10 +352,6 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  680. #define INT_MULTIPLY_WRAPV(a, b, r) \
  681. _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
  682. -#ifndef __has_builtin
  683. -# define __has_builtin(x) 0
  684. -#endif
  685. -
  686. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  687. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  688. https://llvm.org/bugs/show_bug.cgi?id=25390
  689. @@ -369,17 +368,17 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  690. the operation. BUILTIN is the builtin operation, and OVERFLOW the
  691. overflow predicate. Return 1 if the result overflows. See above
  692. for restrictions. */
  693. -#if 5 <= __GNUC__ || __has_builtin (__builtin_add_overflow)
  694. +#if _GL_HAS_BUILTIN_OVERFLOW
  695. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
  696. #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  697. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  698. (_Generic \
  699. (*(r), \
  700. signed char: \
  701. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  702. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  703. signed char, SCHAR_MIN, SCHAR_MAX), \
  704. short int: \
  705. - _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  706. + _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  707. short int, SHRT_MIN, SHRT_MAX), \
  708. int: \
  709. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  710. @@ -393,10 +392,10 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  711. #else
  712. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  713. (sizeof *(r) == sizeof (signed char) \
  714. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned char, \
  715. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  716. signed char, SCHAR_MIN, SCHAR_MAX) \
  717. : sizeof *(r) == sizeof (short int) \
  718. - ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned short int, \
  719. + ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  720. short int, SHRT_MIN, SHRT_MAX) \
  721. : sizeof *(r) == sizeof (int) \
  722. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  723. @@ -412,15 +411,14 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  724. # else
  725. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  726. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  727. - long int, LONG_MIN, LONG_MAX))
  728. + long int, LONG_MIN, LONG_MAX)
  729. # endif
  730. #endif
  731. /* Store the low-order bits of A <op> B into *R, where the operation
  732. is given by OP. Use the unsigned type UT for calculation to avoid
  733. - overflow problems. *R's type is T, with extremal values TMIN and
  734. - TMAX. T must be a signed integer type. Return 1 if the result
  735. - overflows. */
  736. + overflow problems. *R's type is T, with extrema TMIN and TMAX.
  737. + T must be a signed integer type. Return 1 if the result overflows. */
  738. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  739. (sizeof ((a) op (b)) < sizeof (t) \
  740. ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
  741. @@ -429,17 +427,27 @@ verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
  742. ((overflow (a, b) \
  743. || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
  744. || (tmax) < ((a) op (b))) \
  745. - ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 1) \
  746. - : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t, tmin, tmax), 0))
  747. -
  748. -/* Return A <op> B, where the operation is given by OP. Use the
  749. - unsigned type UT for calculation to avoid overflow problems.
  750. - Convert the result to type T without overflow by subtracting TMIN
  751. - from large values before converting, and adding it afterwards.
  752. - Compilers can optimize all the operations except OP. */
  753. -#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t, tmin, tmax) \
  754. - (((ut) (a) op (ut) (b)) <= (tmax) \
  755. - ? (t) ((ut) (a) op (ut) (b)) \
  756. - : ((t) (((ut) (a) op (ut) (b)) - (tmin)) + (tmin)))
  757. + ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  758. + : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  759. +
  760. +/* Return the low-order bits of A <op> B, where the operation is given
  761. + by OP. Use the unsigned type UT for calculation to avoid undefined
  762. + behavior on signed integer overflow, and convert the result to type T.
  763. + UT is at least as wide as T and is no narrower than unsigned int,
  764. + T is two's complement, and there is no padding or trap representations.
  765. + Assume that converting UT to T yields the low-order bits, as is
  766. + done in all known two's-complement C compilers. E.g., see:
  767. + https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  768. +
  769. + According to the C standard, converting UT to T yields an
  770. + implementation-defined result or signal for values outside T's
  771. + range. However, code that works around this theoretical problem
  772. + runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  773. + https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  774. + As the compiler bug is real, don't try to work around the
  775. + theoretical problem. */
  776. +
  777. +#define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  778. + ((t) ((ut) (a) op (ut) (b)))
  779. #endif /* _GL_INTPROPS_H */