5 ms·
Ok, w/o dipping into machine code, show me a low level language. Any snippet of C-code is transparent in that you know roughly how it is going to be translated
by waynecochran 9y ago
Ok, w/o dipping into machine code, show me a low level language. Any snippet of C-code is transparent in that you know roughly how it is going to be translated into machine code.
- anonlastname 9y agoVHDL and other hardware description languages
- arghwhat 9y agoVHDL is as low-level for an FPGA as C is for a CPU. VHDL is almost low-level for an ASIC, where you can implement logic more directly. But even then, VHDL is an abstraction.
- anonlastname 9y agoHardware description languages like VHDL, maybe gpu shader languages like CUDA/HLSL
- United857 9y agoHLSL isn't a low-level language, indeed HL stands for high level. It's not much different than CPU. The runtime compiles down to a standard bytecode, and the driver translates to the GPU's proprietary native code.
- Someone 9y ago”Any snippet of C-code is transparent in that you know roughly how it is going to be translated into machine code.” …for a definition of ‘roughly’ that has become significantly less precise over the past decades. For example, there was a time where you could be reasonably sure every multiplication in your source code mapped to a multiplication instruction, but that time has long been gone. Constant folding, replacement of multiplications by shifts and loop hoisting aren’t exactly novel techniques.
- umanwizard 9y ago> Any snippet of C-code is transparent in that you know roughly how it is going to be translated into machine code. This isn't really true on a modern optimizing compiler.
- phamilton 9y agoPrecisely. As someone who's tried to duel a compiler for performance, -O3 has very little resemblance to anything I've ever written, and outperforms what I've written significantly.
- plpot 9y agoBut this will never be true in any low level language. You can spend a lot of time optimizing any single line of code manually, but software will always be faster than you. And will optimize any code you write beyond recognition.
- waynecochran 9y agoYou still have a good idea of what can get inlined, where loop unrolling can occurs, constant folding, etc.... if you have any assembly experience you still have a good idea of what machine could can be generated.
- kwillets 9y agounsigned char r(unsigned char num) { return num % 10; } https://godbolt.org/g/26HfQk https://godbolt.org/g/26HfQk
- plpot 9y agoWhat you see there is the insane amount of complexity to create a high level feature of C: functions, plus compiler optimizations. What I don't understand about this argument is that you are calling C a high-level language because of compiler optimizations. I can write code in assembler, or in LLVM SSA, and still use software to optimize it beyond recognition.
- 1ris 9y agoOr compile this with clang and -msse4.2 -O2 // https://codereview.stackexchange.com/questions/38182 // https://codereview.stackexchange.com/a/38184 // Definition: Count number of 1's and 0's from integer with bitwise operation // 2^32 = 4,294,967,296 // unsigned int 32 bit #include<stdio.h> int CountOnesFromInteger(unsigned int); int main() { unsigned int inputValue; short unsigned int onesOfValue; printf("Please Enter value (between 0 to 4,294,967,295) : "); scanf("%u",&inputValue); onesOfValue = CountOnesFromInteger(inputValue); printf("\nThe Number has \"%d\" 1's and \"%d\" 0's",onesOfValue,32-onesOfValue); } // Notice the popcnt int CountOnesFromInteger(unsigned int value) { int count; for (count = 0; value != 0; count++, value &= value-1); return count; }
- kwillets 9y agoI've seen this and others, but they're brittle. Terminating that count loop for any other reason, for example, will break it. There's another one for reversing bytes in an integer, but I ended up hard-coding the intrinsic due to brittleness.
- waynecochran 9y agoYes. this is doing to infamous division by 10 using bit shifts as you will find in hacker's delight: http://www.hackersdelight.org/divcMore.pdf http://www.hackersdelight.org/divcMore.pdf. You may notice that when you divide num by 10 you get a quotient q and a remainder r: num = q*10 + r Once you get q, you can solve for r as r = num - q*10 So this is how you get r and q: q = (num >> 1) + (num >> 2); q = q + (q >> 4); q = q + (q >> 8); q = q + (q >> 16); q = q >> 3; r = num - q*10; q = q + ((r + 6) >> 4) voila!
- zrobotics 9y agoC-code compiled for a target like AVR/PIC would fit this article's definition of 'low level', its really the platform the code is compiled for.
- anentropic 9y agoliterally the whole point of the article was that this isn't true any more