473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Restrict qualified pointers in C

Hello!

I am having trouble understanding certain aspects of restrict
qualified pointers. I understand what they are in the simplest terms,
but I want to get high performance and portability out a certain piece
of code and there are some issues I don't fully understand. I'm
hoping someone can help me out. Suppose I have:

struct Vector
{
float x, y;
};

And then I make a method which takes 2 vectors, and adds the second
one componentwise to the first, so I have

inline Vector* add(Vector* v1, const Vector* v2)
{
v1->x += v2->x;
v1->y += v2->y;
return v1;
}

Note that I am able to call "add(v, v)" to add v to itself, so
aliasing is possible. But my naive understanding says that in this
special case aliasing shouldn't matter, and shouldn't change any
optimizations that might otherwise be made. Normally there is
aliasing because while v2 is const, the object it points to is not;
writes cannot happen to the object through the pointer v2, but they
_can_ happen through v1, so the compiler must assume they do.
However, notice that for all component-wise vector operations, the
values of v2 will never be read again once they are written to through
v1.

This wouldn't be true for something like the 3 dimensional cross
product, where you need to compute the new x, and then still use the
old x to compute the new z, etc. But for any of the +=, -=, *=, etc.
like binary operations, an rvalue isn't referenced again once it is
read the first time, in order to change the object.

My first question:

1. Can a compiler figure this out, and thus still generate fast code?
Even it can theoretically, would _most_ of them do it? I would like
the "performance" to be as portable as possible. The assembly from my
compiler looks ok.

2. Assuming the answer is "no", and most compilers cannot figure this
out, what happens if I start declaring things restricted? I'll be
using a new compiler soon, but right now I use Microsofts (no C99,
can't try it out). The definitions I've seen of restricted basically
say "it means they point to disjoint areas of memory". And here they
don't, but in this special case it _seems_ to not matter. Can I trick
the compiler into giving me extra performance? Will it ever come back
to bite me, depending on how exactly the assembly is generated?
Furthermore, will the compiler be intelligent enough to noice I'm
giving it the following "restricted" pointers:

add(v, v) //compiler says "hmmm, wait a minute..." and doesn't
compile?

Thanks for your time,
Ken

Mar 31 '07 #1
2 2698
On 31 maalis, 06:36, "Ken Camann" <kjcam...@gmail.comwrote:
Hello!

I am having trouble understanding certain aspects of restrict
qualified pointers. I understand what they are in the simplest terms,
but I want to get high performance and portability out a certain piece
of code and there are some issues I don't fully understand. I'm
hoping someone can help me out. Suppose I have:

struct Vector
{
float x, y;

};

And then I make a method which takes 2 vectors, and adds the second
one componentwise to the first, so I have

inline Vector* add(Vector* v1, const Vector* v2)
{
v1->x += v2->x;
v1->y += v2->y;
return v1;

}

Note that I am able to call "add(v, v)" to add v to itself, so
aliasing is possible. But my naive understanding says that in this
special case aliasing shouldn't matter, and shouldn't change any
optimizations that might otherwise be made. Normally there is
aliasing because while v2 is const, the object it points to is not;
writes cannot happen to the object through the pointer v2, but they
_can_ happen through v1, so the compiler must assume they do.
However, notice that for all component-wise vector operations, the
values of v2 will never be read again once they are written to through
v1.

This wouldn't be true for something like the 3 dimensional cross
product, where you need to compute the new x, and then still use the
old x to compute the new z, etc. But for any of the +=, -=, *=, etc.
like binary operations, an rvalue isn't referenced again once it is
read the first time, in order to change the object.

My first question:

1. Can a compiler figure this out, and thus still generate fast code?
Even it can theoretically, would _most_ of them do it? I would like
the "performance" to be as portable as possible. The assembly from my
compiler looks ok.

2. Assuming the answer is "no", and most compilers cannot figure this
out, what happens if I start declaring things restricted? I'll be
using a new compiler soon, but right now I use Microsofts (no C99,
can't try it out). The definitions I've seen of restricted basically
say "it means they point to disjoint areas of memory". And here they
don't, but in this special case it _seems_ to not matter. Can I trick
the compiler into giving me extra performance? Will it ever come back
to bite me, depending on how exactly the assembly is generated?
Furthermore, will the compiler be intelligent enough to noice I'm
giving it the following "restricted" pointers:

add(v, v) //compiler says "hmmm, wait a minute..." and doesn't
compile?

Thanks for your time,
Ken

What seems to be the problem?
Mar 31 '07 #2
Your example didn't look like a good candidate for aggressive
optimization. How about this:

/* not tested */
void smear(int n, const float * restrict in, float * restrict out)
{
int i;
out[0]= ( in[0] + 2*in[1] )/3.f;
for(i= 1; i<n-1; i++) {
out[i]= ( in[i-1] + in[i] + in[i + 1] )/3.f;
}
out[n-1]= ( 2*in[n-2] + in[n-1] )/3.f;
}

(I think the "in" pointer doesn't need restrict, but I don't want
to spend time thinking about it.)

--
pa at panix dot com
Mar 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
4
by: Vijay Kumar R Zanvar | last post by:
Greetings, Are the following inferences of mine correct? 1. #include <string.h> char *strcpy(char * restrict s1, const char * restrict s2); a. s1 != s2 b. That means,
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
2
by: pemo | last post by:
In Harbison and Steele's book, they say that using 'restrict' allows functions like memcpy() to be prototyped like this: void * memcpy(void * restrict s1, const void * restrict s2, size_t n); ...
11
by: pemo | last post by:
If you were to compile/run the code below, and get the result '30', I'd be very interested to know what compiler you're using - and its optimisation settings #include <stdio.h> int test(int...
12
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.