volatile and restrict type qualifiers

yyweii posted @ 2008年5月21日 11:21 in C++/C with tags type qualifier , 2086 阅读

C89 标准委员会为 C 添加了两个限定词,const 和 volatile。C99 委员会添加了第三个 restrict 限定词。这三个限定词以及他们的结合决定了当编译器通过一个左值( lvalue,暂且翻译成左值 )访问一个对象时所做的一些假设。左值就是即可以出现在赋值语句左边也可以是右边的对象。

const 的语法和语义是从 C++ 那里改编来的,const 这个概念在许多其他语言里都有。volatile 和 restrict 是委员会的发明,两者都遵从 const 的语法模式。

类型限定词的作用是控制对代码的优化。有几项重要的优化技术是建立在缓冲原理之上的:在某些情况下,编译器可以使用寄存器保存最近一次通过地址访问的值,并且当再次访问同一地址时使用这个记住的值( CPU 寄存器或高速缓冲是典型的硬件寄存器)。如果使用寄存器而不是外部的存储器,代码可以变得更小更快。



用 const 限定的左值不可写。当缺少此限定词时就允许写操作。


用 volatile 限定的左值没有缓存,也就是说直接从存储器中读取或写回存储器。volatile 可以用来限定一些无法保证或编译器无法发现其值是否改变的对象( 例如,由系统时钟更新的变量 )。


uint8_t *pReg = (uint8_t *)0x1234;
//wait for register to become non-zero
while (*pReg == 0) {}  //do sth. else


mov ptr, #0x1234
mov a, @ptr
  bz loop

优化原理相当简单,就是记住最近访问的值,把它缓存在累加器中。但是 loop 陷入了死循环,因为在累加器的值永远不会改变。正确的方法就是使用 volatile 限定词:

uint8_t volatile *pReg = (uint8_t volatile *)0x1234;
//wait for register to become non-zero
while (*pReg == 0) {}  //do sth. else


mov ptr, #0x1234
  mov a, @ptr
  bz loop



restrict 只能使用于指针,使用 restrict 限定的指针和它所引用的对象之间有特殊的关联:只能直接或间接使用此指针或基于此指针的表达式访问其引用的对象。这样编译器可以生成优化代码。


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

ptrA,ptrB 和 val 可能指向同一个内存块,编译器不能彻底优化代码,生成的代码如下:

load R1 ← *val  ; Load the value of val pointer
load R2 ← *ptrA ; Load the value of ptrA ponter
add  R2 += R1   ; Perform Addition
set  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 ← *ptrB
add  R2 += R1
set  R2 → *ptrB

使用 restrict 后,

UpdatePtrs(size_t *restrict ptrA, size_t *restrict ptrB, size_t *restrict val)


load R1 ← *val
load R2 ← *ptrA
load R3 ← *ptrB
add  R2 += R1
add  R3 += R1
set  R2 → *ptrA
set  R3 → *ptrB

注意: val 只载入寄存器一次。

Maruti Insurance Ren 说:
2022年8月01日 16:33

We all know how trustworthy Maruti service is and there are millions of cars that are sold by this company not just in India but all across the world which makes them more reliable. At the same time, as a company they also have their own Insurance for every car they sell and put on the road for travel. Maruti Insurance Renewal Even if you don’t have a car from their company you can still buy Insurance policy for your vehicle. It is towards Maruti Insurance Renewal in online from Brooking service providing no claim bonus on policies. All good things come to an end at one point. It is the same for Insurance for any vehicle that you buy from any vendor or Insurance service provider.

AP 10th computer Mod 说:
2022年9月11日 01:46

Since the Government of AP also provided Computer labs for students in all Government schools also.Computer Knowledge is very much essential nowadays. Even Checking the Results of various examinations, AP 10th computer Model Paper Downloading hall tickets and study materials for multiple exams and booking tickets etc ..need minimum Technical Knowledge is much more important for everyone. Since the Government of AP also provided Computer labs for students in all Government schools also.

SBI Net Banking 说:
2023年1月29日 22:17

State Bank of India is one of the largest Indian banking services which is also spread widely across the country and in foreign nations as well. And it is a great decision if you have already opened up your banking account SBI Net Banking with the SBI because you will understand and get more benefits that you can avail of through their online banking service through online and from their Yono app as well to use SBI credit card and debit card services too.

登录 *

loading captcha image...
or Ctrl+Enter