三元逻辑和多路复用逻辑之间的联系?
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
三元运算符相当于一个分支:即不计算未返回的值。
我不知道你受到了多少限制,所以请注意,布尔运算符
&&
和||
不会评估它们的第二个参数,结果可以确定从一开始。(请注意,“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...).
C 编译器从三元语句创建分支。
使用 Freescale 的 Codewarrior IDE,我编译了以下 C 程序:
与三元语句对应的程序集如下:
C compilers create branches out of ternary statements.
Using Freescale's Codewarrior IDE, I compiled the following C program:
The assembly corresponding to the ternary statement is as follows:
通常,会创建分支逻辑,但如果您愿意评估两个值,则很可能无需任何类型的分支即可执行三元运算。考虑一下:
此代码假设有符号数的右移将进行符号扩展,并且 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:
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.