三元逻辑和多路复用逻辑之间的联系?

发布于 2024-10-19 08:03:32 字数 371 浏览 9 评论 0原文

我正在用 C 语言实现一个计算机模拟器,但面临的挑战是不使用条件语句(即没有 if/else、switch/case、while/for 等)。我正在模拟的硬件中有很多多路复用器,因此如果我可以使用条件三元逻辑运算符,那就太好了。所以,我的问题是:C 编译器是从三元逻辑运算符创建 MUX 逻辑,还是创建分支?

例子:

int a, b, q, r;
/* Ternary logic */
r = q ? a : b;
/* MUX equivalent logic */
r = ( q & a ) | ( (~q) & b )
/* Branch equivalent logic */
if (q) r = a; else r = b;

I'm implementing a computer simulator in C with the challenge not to use conditionals (ie no if/else, switch/case, while/for, etc.). There are a lot of muxes in the hardware I'm simulating, so it would be nice if I could use the conditional ternary logic operators. So, my question: do C compilers create MUX logic from ternary logic operators, or do they create branches?

Example:

int a, b, q, r;
/* Ternary logic */
r = q ? a : b;
/* MUX equivalent logic */
r = ( q & a ) | ( (~q) & b )
/* Branch equivalent logic */
if (q) r = a; else r = b;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-10-26 08:03:32

三元运算符相当于一个分支:即不计算未返回的值。

我不知道你受到了多少限制,所以请注意,布尔运算符 &&|| 不会评估它们的第二个参数,结果可以确定从一开始。

(请注意,“MUX”行与其他两个表达式没有太大不同:它根据 q 中相应位的值从 a 或 b 中选择位,它不会根据 q 是否是来选择 a 或 b是否为 null;编辑:更糟糕的是:您使用的是 !q 而不是 ~q...)。

The ternary operator is equivalent to a branch: i.e. the value not returned is not evaluated.

I don't know how much you are constrained, so note that boolean operators && and || don't evaluate their second argument is the result can be determined from the first.

(And note that you line "MUX" doesn't something very different from the other two expressions: it select bits from a or b depending on the value of corresponding bit in q, it doesn't select a or b depending if q is null or not; Edit: it is worse: you are using !q and not ~q...).

全部不再 2024-10-26 08:03:32

C 编译器从三元语句创建分支。

使用 Freescale 的 Codewarrior IDE,我编译了以下 C 程序:

int a, b, q, r;    
void main(void) {    
  a = 0;    
  b = 1;    
  q = 0;    
  r = q ? a : b;    
..
..    
}

与三元语句对应的程序集如下:

...
LDX    0x1104 ; load val[q] into register x    
BNE    *+4    ; branch to abs addr 0xC016 if val[q]==a    
BRA    *+5    ; branch to abs addr 0xC019 if val[q]!=a
...

C compilers create branches out of ternary statements.

Using Freescale's Codewarrior IDE, I compiled the following C program:

int a, b, q, r;    
void main(void) {    
  a = 0;    
  b = 1;    
  q = 0;    
  r = q ? a : b;    
..
..    
}

The assembly corresponding to the ternary statement is as follows:

...
LDX    0x1104 ; load val[q] into register x    
BNE    *+4    ; branch to abs addr 0xC016 if val[q]==a    
BRA    *+5    ; branch to abs addr 0xC019 if val[q]!=a
...
旧人哭 2024-10-26 08:03:32

通常,会创建分支逻辑,但如果您愿意评估两个值,则很可能无需任何类型的分支即可执行三元运算。考虑一下:

#include <stdio.h>

static inline int isel(int cond, int a, int b)
{
   int mask = cond | (-cond);
   mask >>= 31;
   return (b & mask) | (a & ~mask);
}

int main(void)
{
   printf("1 ? 3 : 4 => %d\n", isel(1, 3, 4));
   printf("0 ? 3 : 4 => %d\n", isel(0, 3, 4));
   printf("-1 ? 3 : 4 => %d\n", isel(-1, 3, 4));
   return 0;
}

此代码假设有符号数的右移将进行符号扩展,并且 sizeof(int) == 4。
某些处理器可以将 isel() 作为汇编指令执行。但是,这两个值都会被评估,而三元 ?: 则不会这样做。

根据情况,编译器通常会尝试做最快的事情,要么进行分支以避免冗余计算,要么在三元表达式中的值很简单时执行这种逻辑。

Often, branching logic is created, but is very possible to do ternary operation without any kind of branch if you're fine with both values being evaluated. Consider this:

#include <stdio.h>

static inline int isel(int cond, int a, int b)
{
   int mask = cond | (-cond);
   mask >>= 31;
   return (b & mask) | (a & ~mask);
}

int main(void)
{
   printf("1 ? 3 : 4 => %d\n", isel(1, 3, 4));
   printf("0 ? 3 : 4 => %d\n", isel(0, 3, 4));
   printf("-1 ? 3 : 4 => %d\n", isel(-1, 3, 4));
   return 0;
}

This code assumes that right-shifting of signed numbers will sign-extend, and that sizeof(int) == 4.
Some processors can do isel() as an assembly instruction. However, both values will be evaluated, which the ternary ?: doesn't do.

Depending on the situation, the compiler will generally try to do the fastest thing, either branching to avoid redundant calculation, or doing this kind of logic if values in the ternary expression are simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文