From: Jakub Jelinek Date: Mon, 4 Aug 2014 15:46:33 +0000 (+0200) Subject: * runtime/memory.c (xmallocarray): Avoid division for the common case. X-Git-Tag: releases/gcc-5.1.0~5664 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=62c986afde031d141961d2619a860def836c8e9b;p=thirdparty%2Fgcc.git * runtime/memory.c (xmallocarray): Avoid division for the common case. From-SVN: r213593 --- diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog index 80f87523a695..77afd166d075 100644 --- a/libgfortran/ChangeLog +++ b/libgfortran/ChangeLog @@ -1,3 +1,7 @@ +2014-08-04 Jakub Jelinek + + * runtime/memory.c (xmallocarray): Avoid division for the common case. + 2014-07-20 Jerry DeLisle PR libgfortran/61632 diff --git a/libgfortran/runtime/memory.c b/libgfortran/runtime/memory.c index 501d870b83ca..16f06065e2b5 100644 --- a/libgfortran/runtime/memory.c +++ b/libgfortran/runtime/memory.c @@ -56,7 +56,9 @@ xmallocarray (size_t nmemb, size_t size) if (!nmemb || !size) size = nmemb = 1; - else if (nmemb > SIZE_MAX / size) +#define HALF_SIZE_T (((size_t) 1) << (__CHAR_BIT__ * sizeof (size_t) / 2)) + else if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0) + && nmemb > SIZE_MAX / size) { errno = ENOMEM; os_error ("Integer overflow in xmallocarray");