delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2007/02/28/18:09:16

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Subject: <sys/gcc.h> macros
To: djgpp-workers AT delorie DOT com
X-Mailer: Lotus Notes Release 7.0.2 September 26, 2006
Message-ID: <OF34A9E363.E9B4C873-ON87257290.007C2BCF-87257290.007F2844@seagate.com>
From: Gordon DOT Schumacher AT seagate DOT com
Date: Wed, 28 Feb 2007 16:08:54 -0700
X-MIMETrack: Serialize by Router on SV-GW1/Seagate Internet(Release 7.0.1 HF29|March 07, 2006) at
02/28/2007 03:08:59 PM
MIME-Version: 1.0
X-Proofpoint-FWRule: outbound2
X-Proofpoint-Virus-Version: vendor=fsecure engine=4.65.5502:2.3.11,1.2.37,4.0.164 definitions=2007-02-28_06:2007-02-28,2007-02-27,2007-02-28 signatures=0
Reply-To: djgpp-workers AT delorie DOT com

This is the gcc.h header I was proposing, for the purposes of allowing
simpler optimizations and safety checks based on GCC attributes and
built-ins.

The difficulty with using those extensions is that they are implemented
in various different versions of GCC, and it's often hard to tell which
one... so I've done a bunch of research to determine this.

I've had particular success with the likely() and unlikely() macros in
helping the compiler with optimizations; it doesn't buy much, but in
performance-critical code every little bit can help.


/* Copyright (C) 2007 Gordon Schumacher, see COPYING.DJ for details */
#ifndef __dj_include_sys_gcc_h__
#define __dj_include_sys_gcc_h__

#define HAVE_GCC_VERSION(MAJ, MIN) \
   ((__GNUC__ > (MAJOR)) || ((__GNUC__ == (MAJOR)) && (__GNUC_MINOR__ >=
(MINOR))))

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-2.95.3
/gcc_4.html#IDX275
#if HAVE_GCC_VERSION(2,3)
   #define __attr_format(type, fmt, parms)   __attribute__((format(type,
fmt, parms)))
#else
   #define __attr_format(type, fmt, parms)
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-2.95.3
/gcc_4.html#IDX272
#if HAVE_GCC_VERSION(2,5)
   #define __attr_noreturn    __attribute__((noreturn))
#else
   #define __attr_noreturn
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-2.95.3
/gcc_4.html#IDX274
#if HAVE_GCC_VERSION(2,5)
   #define __attr_const       __attribute__((const))
#else
   #define __attr_const
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.0.4
/gcc_5.html#IDX1151
#if HAVE_GCC_VERSION(3,0)
   #define __attr_pure        __attribute__((pure))
#else
   #define __attr_pure
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.0.4
/gcc_5.html#IDX1165
#if HAVE_GCC_VERSION(3,0)
   #define __attr_malloc      __attribute__((malloc))
#else
   #define __attr_malloc
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.0.4
/gcc_5.html#IDX1222
// __builtin_expect was introduced in GCC 2.95.3 but is not stable until
GCC 3.0
#if HAVE_GCC_VERSION(3,0)
   #define likely(x)          __builtin_expect((x) != 0, 1)
   #define unlikely(x)        __builtin_expect((x) != 0, 0)
#else
   #define likely(x)          (x)
   #define unlikely(x)        (x)
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.1.1
/gcc/Function-Attributes.html#Function%20Attributes
#if HAVE_GCC_VERSION(3,1)
   #define __attr_deprecated  __attribute__((deprecated))
#else
   #define __attr_deprecated
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.1.1
/gcc/Function-Attributes.html#Function%20Attributes
#if HAVE_GCC_VERSION(3,1)
   #define __attr_noinline    __attribute__((noinline))
#else
   #define __attr_noinline
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.1.1
/gcc/Function-Attributes.html#Function%20Attributes
#if HAVE_GCC_VERSION(3,1)
   #define __attr_forceinline __attribute__((always_inline))
#else
   #define __attr_forceinline
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.3.6
/gcc/Function-Attributes.html#index-g_t_0040code_007bnonnull_007d-function-attribute-1609
#if HAVE_GCC_VERSION(3,3)
   #define __attr_nonnull(parm)  __attribute__((nonnull(parm)))
#else
   #define __attr_nonnull(parm)
#endif

// Documentation: http://gcc.gnu.org/onlinedocs/gcc-3.4.6
/gcc/Function-Attributes.html#index-g_t_0040code_007bwarn_005funused_005fresult_007d-attribute-1618
#if HAVE_GCC_VERSION(3,4)
   #define __attr_warn_unused_result   __attribute__((warn_unused_result))
#else
   #define __attr_warn_unused_result
#endif

#endif // define __dj_include_sys_gcc_h__

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019