含有多种&QUOT关键循环;如果"其输出恒定:如何保存条件测试? [英] Critical loop containing many "if" whose output is constant : How to save on condition tests?

查看:114
本文介绍了含有多种&QUOT关键循环;如果"其输出恒定:如何保存条件测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code。与这种形状的关键循环:

I have a critical loop in my code with this shape :

int myloop(int a, .....){

   /* some stuff */

   // Critical loop
   while(...){
       /* Some Stuff */
       if(a == 1){
          // .....
       }
       else if(a == 2){
          // .....
       }
       else if(a == 3){
          // .....
       }
       else{
          // ....
       }
   }
}

由于循环从不接触了采取永远不会改变分支的价值,但因为这是循环非常沉重,将需要测试的一很多次,这是完全没有必要的值。最好的事情可能是重复循环,使循环开始前的if进行检验,但这将意味着复制的东西很多常见的两种情况,将导致非常难看code ..

As the loop never touches the value of "a" the branch taken will never change, but as this loop is really heavy it will require to test the value of "a" many times, which is totally unnecessary. The best thing is probably to duplicate the loop, so that the "if" can be tested before the loop begins, but this would mean copying a lot of stuff common to both situations and will result in a very ugly code...

有什么办法问GCC / G ++复制此code时,它编译呢?或任何其他办法来避免测试值这么多次?

Is there any way to ask GCC/G++ to duplicate this code when it compiles it ? Or any other trick to avoid testing the value so many times ?

感谢您的帮助!

Nathann

推荐答案

这样做的一个常用方法是如下:

One common way of doing this is as follows:

// make function implementation inline...
static inline int myloop_inline(int a, .....){

   /* some stuff */

   // Critical loop
   while(...){
       /* Some Stuff */
       if(a == 1){
          // .....
       }
       else if(a == 2){
          // .....
       }
       else if(a == 3){
          // .....
       }
       else{
          // ....
       }
   }
}

// wrapper function which calls myloop_inline with hard-coded values of a
int myloop(int a, .....){
{
    switch (a)
    {

    // expand inline function for all performance-critical values of a...

    case 1:
        myloop_inline(1);
        break;

    case 2:
        myloop_inline(2);
        break;

    case 3:
        myloop_inline(3);
        break;

    ...

    // for any values of a for which the function is not performance-critical
    // we can just use a default case...

    default:
        myloop_inline(a);
        break;

    }
}

请注意,由于 A 为常量传递时 myloop_inline()被称为形式 myloop()编译器可以优化掉所有不相关的测试和死code,当它扩展了内联函数。

Note that because a is passed as a literal constant when myloop_inline() is called form myloop() the compiler can optimise away all irrelevant tests and dead code when it expands the inline function.

您可能要采取措施,确保 myloop_inline()实际上被内联,这包括与当前启用优化的编译(如 -O3 ),并在如的情况下GCC您可能要添加 __ attribute__((always_inline))

You may want to take steps to ensure that myloop_inline() actually gets inlined, this includes compiling with optimisation enabled of course (e.g. -O3), and in the case of e.g. gcc you might want to add __attribute__ ((always_inline)).

这篇关于含有多种&QUOT关键循环;如果"其输出恒定:如何保存条件测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆