Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!l32g2000hse.googlegroups.com!not-for-mail
From: sturlamolden <sturlamolden@yahoo.no>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Matlab Vectorisation Speed - How is it done in c++?
Date: Mon, 17 Dec 2007 14:03:40 -0800 (PST)
Organization: http://groups.google.com
Lines: 42
Message-ID: <9f100466-2e16-48cd-a4df-5c5cab65def4@l32g2000hse.googlegroups.com>
References: <eb177713-6655-4454-bbf6-92d2c91bb6a6@s19g2000prg.googlegroups.com> 
NNTP-Posting-Host: 217.168.83.220
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1197929021 29791 127.0.0.1 (17 Dec 2007 22:03:41 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 17 Dec 2007 22:03:41 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: l32g2000hse.googlegroups.com; posting-host=217.168.83.220; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) 
Xref: news.mathworks.com comp.soft-sys.matlab:442839



On 17 Des, 22:22, "Steven G. Johnson" <stev...@alum.mit.edu> wrote:

> The old canard about pointer aliasing semantics being weaker in C than
> in Fortran hasn't been an issue even in principle for almost 10 years
> now, since the 1999 C standard introduced the "restrict" keyword.  I

That is true. But notice that most C code is still written as ANSI C. C
++ does not allow the restrict keyword either.

Microsoft's C compiler does not support ISO C (aka C99). But the most
recent Microsoft C compiler support the restrict keyword as an
extension to ANSI C. Previously one would have to use compiler switch
'/Oa' or '#pragma optimize("a", on)' to assume no aliasing in MSVC. In
GCC one would use the gnu extension __restrict__ to ANSI C, unless
compiling with -std=c99 in which case restrict would be defined. So
GCC would often require non-standard syntax, and MSVC would not allow
control of aliasing at the level of single single variables. One would
then end up with C code cluttered with preprocessor conditionals to
allow compilation on more than a single platform.

A typical pathologic case in ANSI C and ISO C++ would be:

double *c, *a, *b;
int i, n;
/* initialize pointers and n */
for (i=0; i<n;i++)
   *c++ = *a++ + *b++; /* aliasing? */

Which is easily solved in ISO C:

typedef double *restrict arrayptr;
arrayptr a, b, c;
int i, n;
/* initialize pointers and n */
for (i=0; i<n;i++)
   *c++ = *a++ + *b++; /* no aliasing */