www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nonstandard GCC features

reply bearophile <bearophileHUGS lycos.com> writes:
Just found this cute article on Reddit: "GCC hacks in the Linux kernel", by M.
Tim Jones:
http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html

Here are few comparisons between those featuers are D ones.

The range syntax that can be used in switch statements and array defintions is
cute:
1 ... 7
D2 already has a range syntax, it just needs to be extended for other purposes
(and adding a stride too may be useful, but it's less important).


Zero-length arrays are available in D, but the following code (used in C to
define a struct with ane or two variable-length arrays) can't be used, because
of array bound controls:
struct iso_block_store {
        atomic_t refcount;
        size_t data_size;
        quadlet_t data[0];
};


__builtin_return_address is only for special situations, maybe for kernel code.
(While I have found few situations where the computed gotos of GCC are useful).


I think __builtin_constant_p is doable in D2.


deprecated is available in D1:
__attribute__((deprecated)) 
While the pure functions will become available in D2.


This looks interesting for D too, from the article:
__attribute__((warn_unused_result)) forces the compiler to check that all
callers check the result of the function. This ensures that callers are
properly validating the function result so that they can handle the appropriate
errors. 


This may be for special situations only, from the article:
__attribute__((__used__)) tells the compiler that this function is used
regardless of whether GCC finds instances of calls to the function. This can be
useful in cases where C functions are called from assembly.


__builtin_expect() is of course useful (recently they have extended its
usefulness), I have used it few times, it may avoid some of the need of
profile-guided optimization.


The article says __builtin_prefetch() is used extensively by the Linux kernel.
So far I've never used it yet, because I find its usage not easy and tricky.

Bye,
bearophile
Nov 21 2008
next sibling parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2008-11-21 07:43:47 -0500, bearophile <bearophileHUGS lycos.com> said:

 Just found this cute article on Reddit: "GCC hacks in the Linux 
 kernel", by M. Tim Jones:
 http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html
 
 Here are few comparisons between those featuers are D ones.
 
 The range syntax that can be used in switch statements and array 
 defintions is cute:
 1 ... 7
 D2 already has a range syntax, it just needs to be extended for other 
 purposes (and adding a stride too may be useful, but it's less 
 important).

I've wanted that range thing in switch statements some time ago while writing a parser working with character ranges.
 Zero-length arrays are available in D, but the following code (used in 
 C to define a struct with ane or two variable-length arrays) can't be 
 used, because of array bound controls:
 struct iso_block_store {
         atomic_t refcount;
         size_t data_size;
         quadlet_t data[0];
 };

Just use data.ptr if you want to escape the bound checks. But what would be really great is some way to parametrize the length of data to data_size. That's not very high on my wish list though.
 This looks interesting for D too, from the article:
 __attribute__((warn_unused_result)) forces the compiler to check that 
 all callers check the result of the function. This ensures that callers 
 are properly validating the function result so that they can handle the 
 appropriate errors.

Do we need that when we have exceptions in D? -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Nov 21 2008
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Michel Fortin:

Just use data.ptr if you want to escape the bound checks.<

Ah, thank you.
Do we need that when we have exceptions in D?<

You are right. But maybe people that write a kernel may prefer to check error return values instead of using exceptions. Note this article was about GCC used to write the Linux kernel, that has different (and harder) requirements compared to application code, for example I think you don't need __builtin_return_address often in normal code. Bye, bearophile
Nov 21 2008
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"Michel Fortin" <michel.fortin michelf.com> wrote in message 
news:gg6ddd$1s09$1 digitalmars.com...
 On 2008-11-21 07:43:47 -0500, bearophile <bearophileHUGS lycos.com> said:

 Just found this cute article on Reddit: "GCC hacks in the Linux kernel", 
 by M. Tim Jones:
 http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html

 Here are few comparisons between those featuers are D ones.

 The range syntax that can be used in switch statements and array 
 defintions is cute:
 1 ... 7
 D2 already has a range syntax, it just needs to be extended for other 
 purposes (and adding a stride too may be useful, but it's less 
 important).

I've wanted that range thing in switch statements some time ago while writing a parser working with character ranges.

I've been wanting the ability to use "partial" and multiple boolean expressions in a case. In which case (no pun intended), case 5..20: would be a special-case (boy, I can't avoid the unintentional puns, can I?) syntactic sugar for the more generalized syntax of: case >=5, <20:
Nov 22 2008
prev sibling parent Kagamin <spam here.lot> writes:
bearophile Wrote:

 Zero-length arrays are available in D, but the following code (used in C to
define a struct with ane or two variable-length arrays) can't be used, because
of array bound controls:
 struct iso_block_store {
         atomic_t refcount;
         size_t data_size;
         quadlet_t data[0];
 };

I use something like this: struct iso_block_store { atomic_t refcount; size_t data_size; private quadlet_t xdata[0]; quadlet_t[] data() { return xdata.ptr[0..data_size]; } };
Nov 21 2008