473,387 Members | 1,637 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,387 software developers and data experts.

Regarding restrict qualifier

Hi,

i came across restrict qualifier while looking the code. I haven't
able to understand what does this do?. Can some one help me how does
this makes the things restrict to an specified objects. It will be
good, if explained with example.

Appriciate your help in this regard.

Thanks,
Vikas.
Jan 10 '08 #1
2 3235
On Jan 10, 1:45*pm, venkat <venkatavi...@gmail.comwrote:
Hi,

i came across restrict qualifier while looking the code. I haven't
able to understand what does this do?. Can some one help me how does
this makes the things restrict to an specified objects. It will be
good, if explained with example.

Appriciate your help in this regard.
One of the new features in the recently approved C standard C99, is
the restrict pointer qualifier. This qualifier can be applied to a
data pointer to indicate that, during the scope of that pointer
declaration, all data accessed through it will be accessed only
through that pointer but not through any other pointer. The 'restrict'
keyword thus enables the compiler to perform certain optimizations
based on the premise that a given object cannot be changed through
another pointer. Now you're probably asking yourself, "doesn't const
already guarantee that?" No, it doesn't. The qualifier const ensures
that a variable cannot be changed through a particular pointer.
However, it's still possible to change the variable through a
different pointer.

Referred from http://www.devx.com/tips/Tip/13825
Karthik Balaguru
Jan 10 '08 #2
On Jan 10, 8:45*am, venkat <venkatavi...@gmail.comwrote:
Hi,

i came across restrict qualifier while looking the code. I haven't
able to understand what does this do?. Can some one help me how does
this makes the things restrict to an specified objects. It will be
good, if explained with example.
First an example for motivation of "restrict". Take a loog at this
function:

void store_average (int* dst, size_t count, int* src) {
size_t i;
for (i = 0; i < count; ++i)
dst [i] = (src [0] + src [1] + src [2] + src [3]) / 4;
}

Looks like the same value is stored into all elements of dst, so you
would expect that an optimising compiler would calculate (src [0] +
src [1] + src [2] + src [3]) / 4 only once and store it (count) times.
Unfortunately, that doesn't work. Someone could write code like this:

int a [100];
/* Store some values into a */
store_average (a, 100, &a[50]);

As you can see, a different value has to be stored into a [51] and
consecutive elements, so the compiler is not allowed to do that
optimisation. Very annoying, because no sane person would call
store_average that way. So you try to improve it by changing the
function like this:

void store_average (int* dst, size_t count, const int* src) {
size_t i;
for (i = 0; i < count; ++i)
dst [i] = (src [0] + src [1] + src [2] + src [3]) / 4;
}

You'd think that the elements of src can't change, but you are wrong:
The "const" only means that src [0] to src [3] cannot be changed using
the pointer src, but they can be changed using the pointer dst. So the
compiler still cannot optimise the function the way you want. Now you
change to

void store_average (int* dst, size_t count, int* restrict src) {
size_t i;
for (i = 0; i < count; ++i)
dst [i] = (src [0] + src [1] + src [2] + src [3]) / 4;
}

Now the compiler can optimise the function! "int* restrict src" means:
You, the programmer, guarantee to the compiler that anything that is
read by using the pointer src, directly or indirectly, is not changed
by using a pointer that is not derived from the pointer src. And also,
you guarantee that anything that is modified by using the pointer src
is not accessed by using a different pointer.

In other words, you guarantee to the compiler that storing values into
the array dst doesn't change the values src [0] to src [3], and
therefore the compiler can now optimise the function by calculating
the average only once instead of (count) times.

There is a variation of this, if you combine const and restrict:

void store_average (int* dst, size_t count, const int* restrict src) {
size_t i;
for (i = 0; i < count; ++i)
dst [i] = (src [0] + src [1] + src [2] + src [3]) / 4;
}

Here, "const int* restrict src" means: You, the programmer, guarantee
to the compiler that anything that is accessed through the pointer src
is not modified in any way. In this example, it doesn't make much
difference. Without the "const", you would guarantee that src [0] to
src [3] are only changed by using the pointer src, directly or
indirectly. And the compiler can look at the code and see that you
don't use src to change these values, so they stay unchanged. But in
other situations, you could for example pass the pointer src to a
function that the compiler doesn't know, and that function might
change src [0]. The "const int* restrict src" guarantees to the
compiler that this doesn't happen.

Now what happens if you call

store_average (a, 100, &a[50]);

? You guaranteed to the compiler that src [0] to src [3] are not
changed. You lied to the compiler. As a result, there are no
guarantees at all what this code will do (it invokes undefined
behaviour). Whatever the code does, it is your fault because you
called the function incorrectly. On the other hand, the code will
likely run a lot faster, which is the purpose of "restrict"
Jan 11 '08 #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,
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...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
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/...
2
by: Sune | last post by:
Hi, I'd like to know where to put my restrict qualifier for function parameters. I have to files: test.h and test.c In test.h I have the declaration:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.