This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: if-else-if chains to switch conversion optimization


On Fri, May 15, 2015 at 7:47 PM, Daniel Gutson
<daniel.gutson@tallertechnologies.com> wrote:
> Hi,
>
>   the particular motivation is a TMP compile-time search of an
> element, but could be extended to other scenarios.
>
> In my example, given:
>
> template <size_t Count, size_t... List>
> struct Static_Find
> {
>     static size_t find(size_t /*target*/)
>     {
>         return 0;
>     }
> };
>
> template <size_t Count, size_t Head, size_t... Tail>
> struct Static_Find<Count, Head, Tail...>
> {
>     static size_t find(size_t target)
>     {
>         return target == Head ? Count : Static_Find<Count + 1,
> Tail...>::find(target);
>     }
> };
>
> template<size_t... List>
> size_t static_find(size_t target)
> {
>     return Static_Find<0u, List...>::find(target);
> }
>
> consider these functions:
>
> size_t func1(size_t x)
> {
>     return static_find<1, 4, 5, 10, 100>(x);
> }
>
> size_t func2(size_t x)
> {
>     switch(x)
>     {
>         case 1: return 0;
>         case 4: return 1;
>         case 5: return 2;
>         case 10: return 3;
>         case 100: return 4;
>     }
>     return 0;
> }
>
> Using gcc 5 and below, the generated code of func2 outperforms func1
> since switch-cases can be
> alternatively emitted as a binary tree or a branch table. I have not
> seen any equivalent optimizations
> for an if-else-if chain (as is func1 generated), or at least a
> conversion from the chain to the switch (whereas the opposite does
> exist IIUC tree-switch-conversion).
>
> Does such converting optimization (if-else-if chain to switch) exist
> and I didn't see it? If it doesn't,
> is there any reason or just nobody coded it yet?

I remember people posting patches to this effect.

Richard.

> Thanks,
>
>    Daniel.
>
>
> --
>
> Daniel F. Gutson
> Chief Engineering Officer, SPD
>
> San Lorenzo 47, 3rd Floor, Office 5
> CÃrdoba, Argentina
>
> Phone:   +54 351 4217888 / +54 351 4218211
> Skype:    dgutson
> LinkedIn: http://ar.linkedin.com/in/danielgutson


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]