473,737 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to understand "restrict"


I'm going to be using an acronym a lot in this post:

IINM = If I'm not mistaken

Let's say we've got translation units which are going to be compiled to
object files, and that these object files will be supplied to people to link
with their own projects. Here's a sample function in one of the object files:

void Func(int const *const p)
{
int i = 0;

if(*p) ++i;

if(*p) ++i;

if(*p) ++i;

if(*p) ++i;

/* More code */
}

(Forget for the moment how stupid the function is.)

We can see that "*p" is accessed four times. If I'm not mistaken, the
compiler can optimise this to the following:

void Func(int const *const p)
{
int i = 0;

if(*p) i = 4;
}

Now, "*p" is only accessed once.

IINM, the only condition under which the compiler can't perform this
optimisation is if either:

(1) "p" is a pointer to volatile.

(2) "i" is volatile.

Could someone please confirm that?

Here's our next function:

void Func(int const *const p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p) i += OtherFunc();

if (*p) i += OtherFunc();
}

It is my understanding that, even thought "p" is not a pointer to volatile,
the compiler can NOT optimise this to the following:

void Func(int const *const p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}

IINM, the second access to "*p" cannot be optimised away because we don't
know if the invocation of "Func" may have altered the data at that address.
Am I right?

If I understand correctly, this is why "restrict" was brought in. If we want
to be able to optimise away the second access of "*p", then we must write the
function as:

void Func(int const *const restrict p, int (*const OtherFunc)(void ));

Am I right in thinking that, because the pointer is restrict, the compiler
can know turn that into:

void Func(int const *const restrict p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}

--

Frederick Gotham
Aug 9 '06 #1
2 2631
Frederick Gotham wrote:
[...] It is my understanding that, even thought "p" is not a pointer to volatile,
the compiler can NOT optimise this to the following:

void Func(int const *const p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}

IINM, the second access to "*p" cannot be optimised away because we don't
know if the invocation of "Func" may have altered the data at that address.
Am I right?
Yes of course. C does not have a declaration for "I have no side
effects". A strong enough compiler might *deduce* such a theoretical
attribute, however (and in fact declare it for some functions like
strlen.)
If I understand correctly, this is why "restrict" was brought in.
No. Restrict was brought in to explicitely document a fundamental
weakness of the C language, and allow the compiler to perform some
optimizations that the C compiler could sometime never guaranteed to be
correct unless a no-alias assumption is made.
[...] If we want
to be able to optimise away the second access of "*p", then we must write the
function as:

void Func(int const *const restrict p, int (*const OtherFunc)(void ));

Am I right in thinking that, because the pointer is restrict, the compiler
can know turn that into:

void Func(int const *const restrict p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}
I'm pretty sure that this is wrong. A side effect is not the same as a
restrict declaration. restrict just says that the pointer can be
assumed to point to storage that is distinct from anything else
directly referenced in the same scope. I.e., its meant really mostly
for leaf-node functions that do not make other calls in their inner
loop. It would be horrendously difficult to debug certain scenarios if
they did not limit the definition of restrict to this.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Aug 10 '06 #2
Frederick Gotham <fg*******@SPAM .comwrote:
void Func(int const *const p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p) i += OtherFunc();

if (*p) i += OtherFunc();
}

It is my understanding that, even thought "p" is not a pointer to volatile,
the compiler can NOT optimise this to the following:

void Func(int const *const p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}

IINM, the second access to "*p" cannot be optimised away because we don't
know if the invocation of "Func" may have altered the data at that address.
Am I right?
Yes.
If I understand correctly, this is why "restrict" was brought in.
Amongst others. AIUI, array aliasing (more precisely, the optimisations
you can do when you can assume there is no array aliasing) was also a
major reason for it.
If we want to be able to optimise away the second access of "*p", then
we must write the function as:

void Func(int const *const restrict p, int (*const OtherFunc)(void ));

Am I right in thinking that, because the pointer is restrict, the compiler
can know turn that into:

void Func(int const *const restrict p, int (*const OtherFunc)(void ))
{
int i = 0;

if (*p)
{
i = OtherFunc() + OtherFunc();
}
}
Yes.

Richard
Aug 10 '06 #3

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

Similar topics

15
2130
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been using Python for a grand total of about a fortnight now. Hence, I apologise if what follows is a stupid suggestion, or if its already been made somewhere else, or if this is not the appropriate place to make it, etc. But I did honestly do some...
2
4815
by: Gerry | last post by:
I have a developer here with a website running with only "Windows Integrated Authentication" set on a Windows 2000 member server that uses GetObject to get a user's group membership in the domain. This is the code she's using: set adsUser = getobject("WinNT://" & strUsername) for each group in adsUser.groups GrpList = GrpList & lcase(trim(group.name)) & ";" next
0
1212
by: Perttu Pulkkinen | last post by:
Is there some way to restrict in mysql that certan field has only ONE ROW with CERTAIN VALUE X while other rows can have any values but not this one(so "unique" is not the answer..)? This can be of course done in application level, but it would just be nice to know. I would build my application so that only one user can be superadmin, while others are normal admins or something else. Of course this restriction would not be enough...
1
1740
by: wrytat | last post by:
Hi! I'm very new to ASP.NET and really need some good advice from experts here. I'm creating a web application for my company now. This application has 2 parts. 1 part for the customers to access. The 2nd part is for our staff to access only. My director hopes to make the 2nd part to be something like an intranet, such that only our company's computers (maybe only 1 or 2 in the company) can login to this part of the application. 1. My...
3
4045
by: google | last post by:
This is something I've done plenty of times in '97, but I can't seem to get it to work correctly in Access 2003. Say, for example, I have a form with an unbound combobox, the data source is a table of customers, first column is the key field, which is hidden with a 0" width, and the second column is the customer name. The SQL is a union query that also inserts Null in the first column, and "All" in the second column. That combobox gets...
1
3294
by: Mark Sandfox | last post by:
Is there a way to restrict the user to only selecting and sending either a ..gif or .jpg. Everything I have read says this option can not be done by design (security reasons). I find that irronic as this is the reason (security) that I want to restrict their selection. Any help on this one will be greatly appreciated. The page is using ASP.NET.
0
1572
by: kanu | last post by:
Hi, We have moved our domain controllers from win2K to win3K. The script below doesnt work at all now because by default Win3K disables "Network access: Let Everyone permissions apply to anonymous users" GetObject("WinNT://domain/domainController/username,user") We are using GetObject to get the user groups, so as to restrict them to certain parts of the website. It still works with one of the old DC's which is win2K.
6
2378
by: rainy6144 | last post by:
Does the following code have defined behavior? double *new_array(unsigned n) { double *p = malloc(n * sizeof(double)); unsigned i; for (i = 0; i < n; i++) p = 0.0; return p; }
7
1973
by: jayakrishnanav | last post by:
Hi , Is it possible to restrict "pasting" any data in a text box,through keyboard(Ctrl-p) and through mouse??Can anybody help in dis WarmRegards jk
0
8786
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9257
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6749
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.