3

I am looking a set of #ifdef's to check availability of __restrict keyword for GCC and Visual Studio. I assume that it needs to check compiler version, but I don't know for which versions it was introduced. Anyone that can help me out?

UPDATE: This must (and only needs to) work when compiling as C89! So I cannot rely on __STDC_VERSION__ indicating C99, or C99 support.

Mat
  • 202,337
  • 40
  • 393
  • 406
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • I thought `__restrict` is a C++ standard approved keyword. – iammilind May 10 '11 at 08:57
  • It doesn't matter - I need this to work also for C and old compiler versions (Visual Studio 98) – Johan Kotlinski May 10 '11 at 09:02
  • What do you mean by Visual Studio 98? I’m not aware that such a version exists. Prior to VS.NET (Visual Studio 2002) there was only Visual Studio 6 and its C++ compiler is so buggy that supporting it shouldn’t be necessary since that essentially precludes the use of modern C++. – Konrad Rudolph May 10 '11 at 09:08
  • @iammilind: You're wrong. The underscores give it away. – Lightness Races in Orbit May 10 '11 at 09:11
  • @Tomalak, means ? There is a keyword called `restrict` in C99 & `__restrict` is introduced for C++ also. Isn't it correct ? – iammilind May 10 '11 at 09:13
  • Actually this only needs to work for C89 standard. So feel free to ignore both C99 and C++. – Johan Kotlinski May 10 '11 at 09:18
  • @iammilind: No. There is no standard C++ keyword `__restrict`. – Lightness Races in Orbit May 10 '11 at 09:27
  • For gcc, this has been supported for both C and C++ kind of "forever" (I have no older documentation than that, but at the very least gcc 2.95 supports it) in the form of `__restrict__`. `restrict` works too, but only cor C, not for C++. `#define` to the rescue... – Damon May 10 '11 at 09:36
  • gcc [defines `__GNUC__`](http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html), MS Visual Studio [defines `_MSC_VER`](http://msdn.microsoft.com/en-us/library/b0084kay.aspx) which may be helpful; Intel, Dev-C++, Borland, LCC, tcc, ..., ... certainly have their own `#define`s – pmg May 10 '11 at 09:44
  • Gcc and intel c++ treat __restrict as equivalent to c99 restrict but visual studio uses it somewhat diffèrently. None document it well as it's only a default facto quasi-standard feature. – tim18 May 21 '16 at 18:15

4 Answers4

2

How I fixed it:

#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
#   define SKP_restrict __restrict
#elif defined(_MSC_VER) && _MSC_VER >= 1400
#   define SKP_restrict __restrict
#else
#   define SKP_restrict
#endif
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
1

Just use the C99 standard keyword restrict, and possibly #define it to something else.

You can test for C99 conformance with, for example:

#if __STDC__ != 1
#    error not conforming
#    define restrict __restrict /* use implementation __ format */
#else
#    ifndef __STDC_VERSION__
#        error not conforming
#        define restrict __restrict /* use implementation __ format */
#    else
#        if __STDC_VERSION__ < 199901L
#            error Compiler for C before C99
#            define restrict __restrict /* use implementation __ format */
#        else
#            /* all ok */
#        endif
#    endif
#endif

int fx(int *restrict a, char *restrict b) {
  *b = *a;
  return 0;
}

int main(void) {
  int a[1];
  char b[1];
  fx(a, b);
  return 0;
}

Of course the #errors should be edited out in a working version

pmg
  • 106,608
  • 13
  • 126
  • 198
1

In a 'configure, make, make install' scenario, this should be checked in 'configure'. 'configure' should define a 'HAS_RESTRICT' in config.h. This should in turn be checked in your headers to define a suitable macro.

For visual studio, I have zero idea.. :(

cole
  • 95
  • 1
  • 7
0

IMHO, __restrict should be available in all standard compilers for both C/C++ programs. It's similar to C99 restrict in certain way.

iammilind
  • 68,093
  • 33
  • 169
  • 336