Restrict

Restrict

In the C programming language, as of the C99 standard, restrict is a keyword which may be used when declaring pointers. The restrict keyword is a message from the programmer to the compiler saying 'this is the only pointer to that memory block'. The data pointed to by a pointer declared with the restrict qualifier may not be pointed to by any other pointer, or strange runtime behavior "may" result.

Optimization

If the compiler knows that there is only one pointer to a memory block, it can produce better code.The following example makes it clear:

void UpdatePtrs(size_t* ptrA, size_t* ptrB, size_t* val){ *ptrA += *val; *ptrB += *val;}

In the above code, the pointers ptrA , ptrB, val "might" refer to the same memory location, so the compiler will generate a less optimal code :load R1 ← *val ; Load the value of val pointerload R2 ← *ptrA ; Load the value of ptrA ponteradd R2 += R1 ; Perform Additionset R2 → *ptrA ; Update the value of ptrA pointer; Similarly for ptrB, note that val is loaded twice, ; Because ptrA may point to val.load R1 ← *val load R2 ← *ptrBadd R2 += R1set R2 → *ptrB

However if the restrict keyword is used and the above function is declared as void UpdatePtrs(size_t* restrict ptrA, size_t* restrict ptrB, size_t* restrict val)then the compiler is allowed to "assume" that ptrA , ptrB, val point to different locations and updating one pointer will not affect the other pointers. The programmer, not the compiler, is responsible for ensuring that the pointers do not point to identic locations.

Now the compiler can generate better code as follows:load R1 ← *val load R2 ← *ptrAload R3 ← *ptrBadd R2 += R1add R3 += R1set R2 → *ptrAset R3 → *ptrBNote that the above assembly code is better and the val is loaded once.

Another example is of memcpy. The two pointers used as arguments to memcpy(void*, void*, nbytes) are declared with restrict, which tells the compiler of the memcpy function that the two data areas do not overlap. This allows the compiler to produce a faster memcpy function. A programmer might send the same pointer as both arguments, in which case behavior of the memcpy function is undefined.

External links

* [http://www.cellperformance.com/mike_acton/2006/05/demystifying_the_restrict_keyw.html Demystifying The Restrict Keyword] : explanation and examples of use


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР
Synonyms:

Look at other dictionaries:

  • restrict — re‧strict [rɪˈstrɪkt] verb [transitive] to limit or put controls on the amount, size, or range of something: restrict something to something • The bank imposed a ruling, restricting credit increases to 2.5%. • laws that restrict public employee… …   Financial and business terms

  • restrict — re·strict vt 1: to subject to bounds or limits restrict the height of buildings restrict visitation rights 2: to place under restrictions as to use or distribution restrict ed the land to recreational use Merriam Webster’s Dictio …   Law dictionary

  • Restrict — Re*strict , v. t. [imp. & p. p. {Restricted}; p. pr. & vb. n. {Restricting}.] To restrain within bounds; to limit; to confine; as, to restrict worlds to a particular meaning; to restrict a patient to a certain diet. [1913 Webster] Syn: To limit;… …   The Collaborative International Dictionary of English

  • Restrict — Re*strict , a. [L. restrictus, p. p. of restringere. See {Restrain}.] Restricted. [Obs.] [1913 Webster] …   The Collaborative International Dictionary of English

  • restrict — (v.) 1530s, from L. restrictus, pp. of restringere (see RESTRICTION (Cf. restriction)). Regarded 18c. as a Scottishism. Related: RESTRICTED (Cf. Restricted); restricting …   Etymology dictionary

  • restrict — *limit, circumscribe, confine Analogous words: bind, *tie: *contract, shrink: *restrain, curb, check Contrasted words: *extend, lengthen: *expand, amplify, swell: enlarge, * …   New Dictionary of Synonyms

  • restrict — [v] confine, limit situation or ability to participate bind, bottle up, bound, chain, check, circumscribe, come down on, constrict, contain, contract, cool down, cramp, curb, decrease, define, delimit, delimitate, demarcate, demark, diminish,… …   New thesaurus

  • restrict — ► VERB 1) put a limit on; keep under control. 2) deprive of freedom of movement or action. ORIGIN Latin restringere tie back …   English terms dictionary

  • restrict — [ri strikt′] vt. [< L restrictus, pp. of restringere: see RESTRAIN] to keep within certain limits; put certain limitations on; confine SYN. LIMIT …   English World dictionary

  • restrict — verb ADVERB ▪ greatly, seriously (esp. BrE), severely, sharply (esp. BrE), significantly ▪ further ▪ The government is considering new laws which will further restrict people s access to firearms …   Collocations dictionary

  • restrict — [[t]rɪstrɪ̱kt[/t]] restricts, restricting, restricted 1) VERB If you restrict something, you put a limit on it in order to reduce it or prevent it becoming too great. [V n] There is talk of raising the admission requirements to restrict the… …   English dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”