Warning: volatile does NOT do what you think it does. WRT. DS or similar development.Warning: volatile does NOT do what you think it does. WRT. DS or similar development.

downs default_357-line at yahoo.de
Mon Jan 12 00:15:16 PST 2009


Yesterday, I tried porting parts of libnds (the open-source utility library for the Nintendo DS) to D.

Worked relatively fine too, templates work, structs work, volatile DOESN'T WORK.

Let me repeat that. Volatile Doesn't Work.

This was my code:

>
> struct VolatilePointerProxy(T) {
>   T* ptr;
>   T access(T i) { volatile (*ptr) = i; return i; }
>   T access() { T res; volatile res = *ptr; return res; }
>   mixin(PropertyForward!("access"));
> }

This prevents the compiler from optimizing memory accesses past access().

It does *NOT* prevent the compiler from removing "redundant" calls to access.

Like, say, three subsequent 0 writes to MATRIX_MULT4x4. ¹

It took me four hours to find this problem. Foolishly, I trusted volatile to behave at least a _little_ like it does in C.

Walter: I'm certain that you thought changing the behavior of volatile was a good idea at the time, ill-defined though it was, but let me assure you, the act left a gaping hole in the language.

In any case, eventually I settled on this workakludge:

> struct VolatilePointerProxy(T) {
>   T* ptr;
>   pragma(GNU_attribute, noinline) T access(T i) { (*ptr) = i; return i; }
>   pragma(GNU_attribute, noinline) T access() { return *ptr; }
>   mixin(PropertyForward!("access"));
> }

This implies redundant function calls, but at least it works.

(Note: If anybody can tell me how to copy a short from memory to memory in ARM assembly, I'd be much obliged)

To have some good news for a change, here's the pay-off : http://img55.imageshack.us/img55/6811/victory2wv7.jpg .. D on the DS *is feasible*.

 --downs

¹ This is definitely the problem: I dropped three 0 calls in the main function and saw them compile to a single 0 write.



More information about the Digitalmars-d mailing list