Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? Given a 32-bit integer N,Devise an algorithm to find the number of zeros in the binary bit representation of N. The simplest algorithm I can think of is to check the binary representation for Zeros,in C something like this: int num_of_zero(int num) { if(0 == num) return 1; /*For the input 0 it shoul
可能重复: 计算32位整数中设定位数的最佳算法? 给定一个32位整数N,设计一个算法来找出N的二进制位表示中零的个数。 我能想到的最简单的算法是检查零点的二进制表示,用C语言来表示: int num_of_zero(int num) { if(0 == num) return 1; /*For the input 0 it should output 1 */ int Count = 0; while(num>0){ if(0 == (num&1)) Count++; num >>= 1; } return Count; } 如果有一些
I would like to use the Intel C Compiler due to its superior vectorization abilities. However, it understandably has no -march=bdver2 flag which is what I would use on gcc for my AMD FX-8350 CPU. It does have -xavx but I am not sure what other flags to use. What are the optimal compiler flags for the AMD FX-8350 CPU using the Intel C Compiler? cat /proc/cpuinfo |grep flags gives: flags :
由于其优越的矢量化能力,我想使用英特尔C编译器。 但是,可以理解的是,没有-march=bdver2标志,这是我在AMD FX-8350 CPU上使用gcc的标志。 它确实有-xavx但我不确定要使用哪些其他标志。 使用英特尔C编译器的AMD FX-8350 CPU的最佳编译器标志是什么? cat /proc/cpuinfo |grep flags给出: flags:fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx
I'm trying to optimize a cube function using SSE long cube(long n) { return n*n*n; } I have tried this : return (long) _mm_mul_su32(_mm_mul_su32((__m64)n,(__m64)n),(__m64)n); And the performance was even worse (and yes I have never done anything with sse). Is there a SSE function which could increase the performance? Or something else? output from cat /proc/cpuinfo processor
我正在尝试使用SSE优化立方体函数 long cube(long n) { return n*n*n; } 我试过这个: return (long) _mm_mul_su32(_mm_mul_su32((__m64)n,(__m64)n),(__m64)n); 而且表现更差(是的,我从来没有做过任何事情)。 是否有SSE功能可以提高性能? 或者是其他东西? 来自cat / proc / cpuinfo的输出 processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 15 model name : Intel(R) Xeon(R) CP
It's been a while since I last coded arm assembler and I'm a little rusty on the details. If I call a C function from arm, I only have to worry about saving r0-r3 and lr, right? If the C function uses any other registers, is it responsible for saving those on the stack and restoring them? In other words, the compiler would generate code to do this for C functions. For example if I use
从我上次编码的arm汇编程序起,我已经有一段时间了,对细节有点生疏。 如果我从arm调用C函数,我只需要担心保存r0-r3和lr,对吧? 如果C函数使用任何其他寄存器,它是否负责将这些保存在堆栈中并恢复它们? 换句话说,编译器会为C函数生成代码。 例如,如果我在汇编函数中使用r10,我不必将它的值推入堆栈或内存,并在C调用后弹出/恢复它,是吗? 这是为arm-eabi-gcc 4.3.0。 我意识到我可以阅读整个EABI,但是然后缩写R
I often build C (and C++) code, using GCC (or clang) with the -Wall flag turned on. Now I happen to need to make sure a small C project, which builds fine on Linux with this flag, also builds on Windows with MSVC. However, if I run MSVC with -Wall , I get many warnings which I find rather spurious, such as: warning C4255: 'some_func': no function prototype given: converting '()
我经常使用启用了-Wall标志的GCC(或clang)构建C(和C ++)代码。 现在我碰巧需要确保一个小型的C项目,这个项目可以在Linux上使用这个标志进行构建,也可以在MSVC上构建在Windows上。 但是,如果我使用-Wall运行MSVC,则会收到很多警告,我发现它们很虚假,例如: warning C4255: 'some_func': no function prototype given: converting '()' to '(void)' `警告C4820:'some_struct':
Assume you have the following Enumeration, which contains flags. [Flags] public enum MyFlag { None = 0, Foo = 1 << 0, Bar = 1 << 1, Baz = 1 << 2, Quuz = 1 << 3 } And you instantiate a new and old : var newF = MyFlag.Foo | MyFlaq.Quuz; // 1001 var oldF = MyFlag.Foo | MyFlag.Baz; // 0101 My first question: How to perform a bitwise operation on new an
假设您有以下Enumeration,其中包含标志。 [Flags] public enum MyFlag { None = 0, Foo = 1 << 0, Bar = 1 << 1, Baz = 1 << 2, Quuz = 1 << 3 } 你实例化一个新的和旧的: var newF = MyFlag.Foo | MyFlaq.Quuz; // 1001 var oldF = MyFlag.Foo | MyFlag.Baz; // 0101 我的第一个问题:如何对新老进行按位操作,获取XOR标志? var xor = ????? // -> should be 1100
In SQL Server, shift operators are not present as per knowledge. If I have to achieve right shift and left shift, what will be the efficient way of doing it? With mathematical expressions which will give me the same output as shift operator must have given. OR Will call CLR function to calculate the right shift and left shift because shift operators are available in C# which will give me
在SQL Server中,按照知识,移位操作符不存在。 如果我必须实现右移和左移,那么执行它的有效方式是什么? 用数学表达式可以给出与移位算子必须给出的输出相同的输出。 要么 将调用CLR函数来计算右移和左移,因为移位操作符在C#中可用,这将使我得到我期望的输出。 请建议哪一个是更有效的方式。
The obvious way to do it would be with locking. But I know that there is Interlocked class in c#, which is good for thread safe increment and decrement, so I wondered if there is something like that which would let me do the same for binary operations like left shift. Is there anything like Interlocked class for left-shift operator? 假设你试图左移和分配,并假设你不想碰撞,你可以这样做: // t
做到这一点的显而易见的方式是锁定。 但是我知道c#中有Interlocked类,这对于线程安全的递增和递减是很好的,所以我想知道是否有类似于左移的二进制操作可以做同样的事情。 是否有类似左移操作符的Interlocked类? 假设你试图左移和分配,并假设你不想碰撞,你可以这样做: // this method will only return a value when this thread's shift operation "won" the race int GetNextValue() { // execute until we "win
Is there a method (in c#/.net) that would left-shift (bitwise) each short in a short[] that would be faster then doing it in a loop? I am talking about data coming from a digital camera (16bit gray), the camera only uses the lower 12 bits. So to see something when rendering the data it needs to be shifted left by 4. This is what I am doing so far: byte[] RawData; // from camera along with t
有没有一种方法(在C#/。净),将左移(按位)每短在短[]会更快,然后在一个循环? 我正在谈论数码相机(16位灰色)的数据,相机只使用较低的12位。 因此,在呈现数据时要查看某些内容,需要将其左移4。 这是我迄今为止所做的: byte[] RawData; // from camera along with the other info if (pf == PixelFormats.Gray16) { fixed (byte* ptr = RawData) { short* wptr = (short*)ptr; short te
I am aware of the basic premise of what bitwise operation are (although would appreciate a "for dummies" explanation); however I am unaware of when it is appropriate to use this technique. My understanding is that older CPU architectures could perform bitwise operations faster then other operations and hence it was advantageous to know how to use them. Given this is no longer the ca
我知道什么是按位操作的基本前提(虽然会赞赏“对于傻瓜”的解释); 但是我不知道什么时候适合使用这种技术。 我的理解是,较早的CPU架构可以比其他操作更快地执行按位操作,因此知道如何使用它们是有利的。 鉴于这不再是这种情况; 执行它们是否合适,如果是这样,为了什么目的和在什么条件下? (我特别感兴趣的是C#的上下文,但很高兴收到一般答案) 按位操作是快速检查可能在变量上设置的标志的好方法。 下面的示例