简体   繁体   English

我怎样才能重写这个if-else语句,以便它不使用跳转?

[英]How can I rewrite this if-else statement so that it doesn't use a jump?

I am not trying to optimize anything, so please don't tell me, that premature optimization is root of all evil. 我不是要优化任何东西,所以请不要告诉我,过早的优化是万恶之源。 I am trying to solve a problem and I can't find the third solution. 我试图解决一个问题,我找不到第三个解决方案。

The problem goes like this: 问题是这样的:

if (x)
    y = a;
else
    y = b;

x can only be 0 or 1. How to write this condition without a jump? x只能是0或1.如何在没有跳转的情况写出这个条件

One solution, with a data structure, is: 一种具有数据结构的解决方案是:

int temp[2] = {b, a};
y = temp[x];

Another arithmetic solution is: 另一种算术解决方案是

y = x * a + (1 - x) * b;

There is supposed to be a third one, a logical solution. 应该有第三个,一个逻辑解决方案。 Do you know how it looks like? 你知道它的样子吗? Please give a solution in C. 请用C解决。

y = a ^ ((x - 1U) & (a ^ b));

这使用按位x或

Are you saying you don't want a jump in the source code or at the CPU level? 你是说你不想跳转代码或CPU级别?

On my compiler 在我的编译器上

int choose(int x, int a, int b)
{
    int y;

    if (x)
        y = a;
    else
        y = b;

    return y;
}

compiles to this - 汇编到此 -

test    ecx, ecx
cmovne  r8d, edx
mov eax, r8d
ret 0

Although I've written a jump at the C code level there isn't one in the generated machine code as the compiler is able to use a conditional move instruction. 虽然我在C代码级别编写了一个跳转,但生成的机器代码中没有一个,因为编译器能够使用条件移动指令。

这可能不是最好的解决方案,但我认为它是没有跳跃的按位等价物:

y = (~(x*UINT_MAX) & a) | (~(1 - x) & b);

The following code use the trick that, as x is either 1 or 0, x-1 is either 0 or -1 (aka 0xFFFFFFFF, assuming 32 bit int:s). 下面的代码使用的技巧是,当x为1或0时, x-1为0或-1(也就是0xFFFFFFFF,假设32位int:s)。 If x is 1, the result of the & is zero, so the result is a . 如果x为1,则&的结果为零,因此结果为a When x is zero, the result of the & will be ba , the total result will then be a+(ba) or simply b . x为零时, &的结果为ba ,则总结果将为a+(ba)或简单为b

int test(int x, int a, int b)
{
  return a + ( (((unsigned int)x)-1) & (b - a) );
}

On a fictitious micro-controller (without any fancy conditional move instructions) this will result in four instructions: 在一个虚构的微控制器(没有任何花哨的条件移动指令),这将导致四个指令:

ADD   #-1 R12
SUB   R13, R14
AND   R14, R12
ADD   R13, R12

Admittedly, this is very similar to the XOR solution posted by @6502. 不可否认,这与@ 6502发布的XOR解决方案非常相似。

Something like the choose function here then? 那么选择功能呢?

#include <stdio.h>

int choose(unsigned int x, int a, int b)
{
    unsigned int selector = -x;    

    return (a & selector) | (b & ~selector);
}

int main()
{
    printf("%d\n", choose(0, 123, 200));
    printf("%d\n", choose(1, 123, 200));
}

Use the ternary ?: operator. 使用三元?:运算符。 It is made for this. 这是为了这个。

I agree with Jens; 我同意Jens的观点; a ternary operator will do the trick without a jump. 一个三元运算符可以在没有跳跃的情况下完成这一操作。

y = x ? a : b

BTW, there is a sister site, Code Golf , where people ask questions of the nature "How can I solve problem x with the shortest amount of code while maintaining constraint y?" 顺便说一句,有一个姊妹网站Code Golf ,在那里人们会问自然的问题“如何在保持约束条件的同时用最短的代码解决问题x?”

For processors that allow conditional execution of statements: 对于允许条件执行语句的处理器:

y = a;
if (x != 0) y = b;

On an ARM processor in 32 bit mode, this should translate into the following pseudo instructions: 在32位模式的ARM处理器上,这应该转换为以下伪指令:

mov y, a; move a into y.
test x; (or XOR X, X ; to set condition codes)
MOVNZ y, b ; move b into y if condition is non-zero.

No jumps involved here, at least at the assembly language level. 这里没有跳转,至少在汇编语言级别。

Note: platform specific solution, may not be valid for processors without conditional instruction execution capabilities. 注意:特定于平台的解决方案可能对没有条件指令执行功能的处理器有效。

switch (x) {
   case 1:
      y = a;
      break;
   default:
      y = b;
      break;
}

EDIT: the original question said "with jumps" ... not "without" 编辑:原来的问题说“有跳”......不是“没有”

EDIT: I misread the question which states that the C code shouldn't use a jump at all. 编辑:我误读了一个问题,即C代码根本不应该使用跳转。

I think I'd go with 我想我会一起去

y = b;
if (x)
    y = a;

If I read the following disassembled code right, there should be only one jump: 如果我正确阅读下面的反汇编代码,那么应该只有一个跳转:

0000000000400474 <main>:
  400474:   55                      push   %rbp
  400475:   48 89 e5                mov    %rsp,%rbp
  400478:   8b 45 fc                mov    -0x4(%rbp),%eax
  40047b:   89 45 f8                mov    %eax,-0x8(%rbp)
  40047e:   83 7d f4 00             cmpl   $0x0,-0xc(%rbp)
  400482:   74 06                   je     40048a <main+0x16>
  400484:   8b 45 f0                mov    -0x10(%rbp),%eax
  400487:   89 45 f8                mov    %eax,-0x8(%rbp)
  40048a:   c9                      leaveq
  40048b:   c3                      retq
  40048c:   90                      nop
  40048d:   90                      nop
  40048e:   90                      nop
  40048f:   90                      nop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM