简体   繁体   English

在C语言中以64位平台为目标时,是否最好使用64位变量进行数组引用?

[英]When targeting 64-bit platforms in C, is it better to use 64-bit variables for array references?

I was looking at the assembly of a code snippet I wrote and noticed that the indicated movsxd op only appears if the ret variable is 32-bit. 我正在看我编写的代码片段的汇编,并注意到指示的movsxd op仅在ret变量为32位时出现。 If ret is 64-bit, it is used directly: mov BYTE PTR [rdi+rbp+86], al . 如果ret是64位,则直接使用: mov BYTE PTR [rdi+rbp+86], al

; 861  :        _BitScanForward(&depth, subject);

    movsx   edx, dx

; 862  :        qry_args->lo_refs[++ret] = (BYTE)depth;

    inc ebp                             // ret is in ebp
    bsf ecx, edx
    movsxd  rax, ebp                    // convert 32-bit ebp to 64-bit rax

; 865  :        subject ^= (1 << depth);
; 866  :        nulls_mask.lo |= (1 << depth);

    movsx   r9d, r9w
    btc edx, ecx
    bts r9d, ecx
    mov BYTE PTR [rax+rbx+86], cl       // 64-bit rax used by mov

Since the mov op requires 64-bit registers in 64-bit mode, it makes sense to me that any variables used to reference memory (such as array referencers) should ideally be in 64-bit. 由于mov op需要在64位模式下使用64位寄存器,所以对我而言,理想的是,用于引用内存的任何变量(例如数组引用程序)都应理想地为64位。

However, I know it's common to simply use int in a loop that is not going to exceed 2^^31 iterations. 但是,我知道在不超过2 ^^ 31次迭代的循环中简单地使用int是很常见的。 Should we in fact be using long long ( int64 ) as a best practice for 64-bit code? 实际上,我们是否应该将long longint64 )作为64位代码的最佳实践? Any comments on this? 对此有何评论?

I haven't gone to the trouble of testing this beyond what is shown here. 除了这里显示的内容之外,我还没有麻烦去进行测试。

Ps. 附言 This isn't a micro-optimization question. 这不是一个微观优化问题。 It's a question of form. 这是形式问题。 To me, it makes sense to use the type used by the compiler. 对我来说,使用编译器使用的类型是有意义的。

Info: I'm compiling with VS 2016 with max optimizations on. 信息:我正在使用VS 2016进行编译,并启用了最大优化功能。

Use the size_t type for array indexes. size_t类型用于数组索引。 It is large enough to hold array indexes. 它足够大以容纳数组索引。 Usually it holds 64 bits on 64-bit platforms and 32 bits on 32-bit platforms. 通常,它在64位平台上拥有64位,在32位平台上拥有32位。

See https://stackoverflow.com/a/2550799/909655 参见https://stackoverflow.com/a/2550799/909655

It is generally not a good idea to use long long in your case. 在您的情况下long long使用通常不是一个好主意。 The next developer to read your code will think that either the code needs to handle large numbers or that the original programmer didn't know what he was doing. 下一个阅读您的代码的开发人员将认为该代码需要处理大量数字,或者原始程序员不知道他在做什么。

Better to use either size_t , indicating that the variable should be able to handle any array size, or int , indicating that it is a general purpose variable with normal range requirements. 最好使用size_t (表示变量应该能够处理任何数组大小),或者使用int (表示它是具有正常范围要求的通用变量)。

What integer type should I choose? 我应该选择哪种整数类型?

int is used for normal integer variables. int用于普通整数变量。 This is the type you should use unless there is a reason to choose another type. 除非有理由选择其他类型,否则应使用此类型。 int has a size that has been chosen by the platform developer because it is a good size to use (for whatever reason, but typically a good tradeoff between range, memory consumption and performance on that platform) int的大小已由平台开发人员选择,因为它是一个可以使用的大小(无论出于何种原因,但通常在该平台的范围,内存消耗和性能之间要进行权衡)

char is used for strings and binary data. char用于字符串和二进制数据。 If you plan to use binary operators (especially shift operators) you should use unsigned char . 如果计划使用二进制运算符(尤其是shift运算符),则应使用unsigned char

size_t Used for array/memory sizes, array indexes, etc. size_t用于数组/内存大小,数组索引等。

Other int sizes ( short , long , long long , fixed size) are used as needed. 根据需要使用其他int大小( shortlonglong long ,固定大小)。 Fixed size is typically used for data that is interchanged between different systems. 固定大小通常用于在不同系统之间交换的数据。 long/short are typically used when the return value of a standard function is of the respective size. 当标准函数的返回值具有相应的大小时,通常使用long / short。 long long is used when you need to store large numbers, but for really big integers you need a BigInt library. 需要存储大量数字时使用long long,但是对于很大的整数,则需要一个BigInt库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM