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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Inline assembly. When do I need it?

7 点作者 moccajoghurt超过 12 年前
I read the article about the beauty of the Doom 3 source code and stumbled upon the mention of inline assembly parts. When is the C or C++ compiler not sufficient and you actually need to write assembly code? It's something I wouldn't have expected.

5 条评论

wtracy超过 12 年前
I would say: Whenever you have a hard performance target that you must hit, and pure C can't meet that target.<p>Every game, including Doom 3, has some targeted minimum frame rate that the developers want it to run at. (This target can be anywhere from 30-60 frames per second, depending on the studio's standards.) Additionally, the game will have some targeted hardware that it is expected to run on. (Hardware requirements for PCs may slip during development, but console hardware is pretty non-negotiable.) The developer will optimize until the game runs at the required frame rate on all the targeted hardware platforms (using a profiler to identify the functions that need optimization) then no further.<p>Now, if what you really meant is, "When is assembler actually going to be faster than pure C?" that's a harder question. Modern C compilers are pretty smart, and most assembly hackers today start by looking at the assembly generated by a C compiler, and optimizing from there.<p>Most compilers are conservative about what optimizations they run, so there are often situations where it is safe to apply an optimization yet the compiler will not recognize it as such. (Really, would you want your compiler adding bugs to your code in an effort to make it "faster"?) So, sometimes you can apply optimizations that the compiler missed.<p>Most of the time, though, if you're not John Carmack then you probably can't beat your C compiler.
J_Darnley超过 12 年前
Whenever you want to use simd. I have not yet seen a multimedia project say "The compiler does the right thing here". This may not apply to games and almost certainly does not to most programs.
trin_超过 12 年前
you'll know it when you need it ... seriously. unless you really know what you want and what you are doing the chances of outsmarting the compiler are very very slim.<p>there's a rather old but good video about this very topic that i just remembered:<p><a href="http://media.ccc.de/browse/conferences/camp2007/cccamp07-en-1952-Know_your_compiler.html" rel="nofollow">http://media.ccc.de/browse/conferences/camp2007/cccamp07-en-...</a><p>and i think these are the corresponding slides: <a href="http://www.fefe.de/know-your-compiler.pdf" rel="nofollow">http://www.fefe.de/know-your-compiler.pdf</a>
mattgrice超过 12 年前
When the code isn't fast enough and you know some trick or shortcut that the compiler doesn't know or can't use.
meaty超过 12 年前
Probably never these days.