www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "register int n" alternative

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
Possible mark variable for force use register ?

Example C-code:
{
     register char *buf;
     long           pos;
     register int   n;
     register int   r;

     if (!n)
         return 0;
}


How to implement in D ?
Feb 16 2020
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Sunday, 16 February 2020 at 13:48:43 UTC, Виталий Фадеев wrote:
 Possible mark variable for force use register ?

 Example C-code:
 {
     register char *buf;
     long           pos;
     register int   n;
     register int   r;

     if (!n)
         return 0;
 }


 How to implement in D ?
Don't you get a warning from your c compiler a C compiler? The register keyword as been deprecated for ages in C. Since the compiler cannot actually guarantee that the variable will be a register. As a result D does not have the register keyword. in D simply allocating a local is enough (and compiling with optimization enabled), if there is a register free to put the variable in, that's what the optimizer will do. If you don't want to be at the mercy of the optimizer you can always write a block of asm. Which is what I usually do when I _really_ care.
Feb 16 2020
parent lithium iodate <whatdoiknow doesntexist.net> writes:
On Sunday, 16 February 2020 at 15:15:44 UTC, Stefan Koch wrote:
 The register keyword as been deprecated for ages in C.
 Since the compiler cannot actually guarantee that the variable 
 will be a register.
 As a result D does not have the register keyword.
That only applies for C++, where it doesn't (or rather didn't) even do the same thing as in C. In C it's an optimization aid with actual semantic implications. A register storage-class variable cannot be aliased, in fact, any attempt should cause compilation failure. Whether it actually helps modern compilers with optimization is of course another matter ;)
Feb 16 2020