TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Is This a Branch? (2021)

33 点作者 segfaultbuserr将近 2 年前

4 条评论

MrYellowP将近 2 年前
One of my favourite things in programming is getting rid of the conditional branches. They&#x27;re great to keep the brain fresh, because sometimes you have to dig ten layers outward to rewrite your code <i>just to get rid of that fucking condition that slows down the code</i>.<p>Or a good one I recall, was my 2D-box-check. I&#x27;ve made the mistake believing that not writing the output to RAM, when the result yields &quot;not in box&quot; would be better. So naturally I checked for every output if the respective bit was 1 or 0 and a 0 restarted the loop early.<p>Nope.<p>It was faster to just always write to memory, but linking the result to a multiplication which increments the pointer based on the above mentioned bit. &quot;Not in box&quot; increased the pointer by 0 (n * 0 = 0).<p>Boom, one billion 2d-box-checks per second on an i5 7500HQ on a single core without using SIMD. Good enough.<p>I ... guess I should stop now.<p>(PS: this was used for a sprite engine for a super-high resolution text mode, which compiled sprites into the code that wrote them to the screen. That&#x27;s a LOT faster than checking if a character&#x2F;pixel needs not-to-be-written AND it allowed for potentially eight pixels&#x2F;characters to be drawn at once, too, using pop!<p>... I really should stop now. ^_^)
评论 #36931243 未加载
dang将近 2 年前
Discussed at the time:<p><i>Is This a Branch?</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26141047">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26141047</a> - Feb 2021 (74 comments)
mattnewport将近 2 年前
Code that uses min, max, clip, clamp, saturate, etc. when that is straightforwardly what operation is needed is clearer, more explicit, more concise and potentially faster. When used this way they are just straightforwardly better than equivalent if then statements.<p>Occasionally you see code that uses combinations of these in less obvious ways to do some more complex operation and I can see how one could argue that in rare cases that ends up less clear than if then statements but I&#x27;ve never really encountered that in many years of reading shader code.<p>There are other reasons to prefer functions over if then in shader code not discussed here as well, like replacing step with smoothstep for antialiasing.
评论 #36934657 未加载
mmphosis将近 2 年前
I like the readable version better than my bit twiddling:<p><pre><code> int is_this_a_branch(int v, int w) { int t; t = !(v &lt; 10); return (v + ((w ^ -t) + t)) &lt;&lt; t; } cc -O3 -c branch.c objdump -drwC -Mintel -S branch.o</code></pre>