在编写由LLVM后端编译的代码时,体系结构是否重要?(When writing code compiled by LLVM backend, does architecture matter?)
我的问题实际上比标题更普遍:
在编写代码时,体系结构在什么时候会重要,最终将编译成LLVM中间代码,然后再从那里编译到机器语言?
假设我正在写Rust(使用LLVM作为后端)。 我是否能够自动将我的Rust代码编译到LLVM可以定位的每个体系结构(假设该机器上有一个可以运行它的操作系统)?
或者Rust的标准库还没有做成“ARM兼容”,所以我不能编译到ARM,即使LLVM的目标是ARM?
如果我不使用任何标准库,我的整个程序只是一个马上返回的程序呢? 难道即使没有任何库,Rust(或你有什么)不能编译到ARM(或你有什么),即使LLVM是以它为目标的吗?
如果上述所有示例都编译得很好,那么我需要做些什么才能让我的代码
在某个体系结构上中断,而不是编译到某个体系结构中?相同品种的奖金问题:
- 比方说,标准库使用OS系统调用(这肯定是)。 系统调用时,您是否必须关心架构? 或者,操作系统(例如,Linux)也将架构抽象出来?
谢谢。
My question is actually more general than the title:
At what point does the architecture matter when writing code that will eventually be compiled to LLVM intermediary code, and then from there to the machine language?
Let's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?
Or could it be that the Rust standard library hasn't been made "ARM compatible" yet, so I couldn't compile to ARM even if the LLVM targets it?
What if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?
If all the above examples compile just fine, what do I have to do to get my code to
break on one architecturenot compile to a certain architecture?Bonus question of the same variety:
- Let's say the standard library makes use of OS system calls (which is surely does). Do you have to care about architecture when making system calls? Or does the OS (Linux, for example) abstract away architecture as well?
Thanks.
原文:https://stackoverflow.com/questions/34306069
最满意答案
是的,那应该有用。
但是,如果将整数中的一半舍入步骤(在您的情况下为5)添加,则会更容易。 然后你可以做简单的整数除法:
BonusValue = (Str+5) / 10
Yes, that should work.
It is however much easier if you add half the rounding step, in your case 5, to the integer. Then you can do simple integer division:
BonusValue = (Str+5) / 10
相关问答
更多-
如何在go中将float转换为int时舍入到最近的int(How to round to nearest int when casting float to int in go)[2022-06-24]
int(f+0.5)将导致它向上舍入,如果它> = .5 int(f+0.5) will cause for it to round upwards if it's >= .5 -
是的,那应该有用。 但是,如果将整数中的一半舍入步骤(在您的情况下为5)添加,则会更容易。 然后你可以做简单的整数除法: BonusValue = (Str+5) / 10 Yes, that should work. It is however much easier if you add half the rounding step, in your case 5, to the integer. Then you can do simple integer division: BonusValue = ...
-
是的,你可以指望这个代码是可移植的。 N4296 (这是C ++ 14的最新开放草案)在5.6节[expr.mul]中说: 对于整型操作数,/运算符会丢弃任何小数部分产生代数商。 [脚注:这通常称为零截断] 这不是最新C ++的新功能,它也可以在C89中使用。 唯一需要注意的是,如果toRound为负数 ,则需要减去偏移量。 另一种方法是: int RoundToMultiple(int toRound, int multiple) { const auto ratio = static_cast< ...
-
你可以使用std::max_element()和一个自定义的比较运算符: struct limited_cmp { int limit; limited_cmp(int a_limit) : limit(a_limit) { } bool operator()(int left, int right) const { if (left <= limit && limit < right) { return false; } ret ...
-
Foundation图书馆有一个round (实际上是在Darwin ,但是Foundation进口Darwin ,大多数时候你将要使用Foundation而不是直接使用Darwin ) 。 import Foundation users = round(users) 在操场上运行你的代码,然后调用: print(round(users)) 输出: 15.0 当小数位置为>= .5时, round()始终向上舍入,当< .5 (标准舍入)时, round()始终向上舍入。 您可以使用floor()强制 ...
-
这可能是一个解决方案 #include
int toNearest5(int i) { int r = i%5, o = 0; if(r) { o = r/5. >= .5 ? 5 : 0; } return (i-r+o); } int main() { using namespace std; cout << toNearest5(8) << endl; cout << toNearest5(12) << ... -
他们完全不同。 第一个是数组。 第二个是一个指向数组的指针。 第一个bar宣言后的评论绝对不正确。 第一个bar是10个int的数组。 期。 它不是一个指针(即你的“这意味着”部分根本没有意义)。 可以这样表达: typedef int T[10]; 您的第一个bar有T型,而第二个bar有T * 。 你明白T和T *之间的区别,是吗? They are completely different. The first one is an array. The second one is a pointer ...
-
错误:无法将参数从'int [10]'转换为'int'[关闭](Error: cannot convert parameters from 'int [10]' to 'int' [closed])[2023-04-09]
Gangadhar和Jansel给出的答案是不完整的。 您是通过引用传递值,所以这还不够: void push(int stack[size],int,int); 这就是你需要的: void push(int[],int&,int); void pop(int[],int&); void display(int[],int&); The answer given by Gangadhar and Jansel is incomplete. You are passing values by refere ... -
假设你不关心秒: NSDateComponents *time = [[NSCalendar currentCalendar] components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: mydate]; NSUInteger remainder = ([time minute] % 10); if (remainde ...
-
我也在Windows上使用过gcc 4.4.3。 该程序成功编译并生成输出“11”: #include
unsigned int doSomething(unsigned int *x, int y); int main() { unsigned int res = 0; unsigned int x = 10; res = doSomething(&x, 1); printf("Result: %d\n", res); return 0 ...