compiler-optimization

Does the C# compiler automatically dispose of IDisposable objects?

删除回忆录丶 提交于 2019-12-13 17:13:17
问题 Assuming I have a method public static Rectangle DrawRectangle(Vector origin, Vector size) which returns an object of type Rectangle : IDisposable If I call only the method DrawRectangle(origin, size) , but do not assign the return value to a variable myRectangle = DrawRectangle(origin, size) , will the compiler automatically detect this and call DrawRectangle(origin, size).Dispose() , or do I have to do it myself? 回答1: There are only two scenarios I can think of where the compiler

How can I compile this very big, but boring C source?

痞子三分冷 提交于 2019-12-13 15:26:27
问题 The central function in my code looks like this (everything else is vanilla input and output): const int n = 40000; double * foo (double const * const x) { double * y = malloc (n*sizeof(double)); y[0] = x[0] + (0.2*x[1]*x[0] - x[2]*x[2]); y[1] = x[1] + (0.2*x[1]*x[0] - x[2]*x[2]); // … // 39997 lines of similar code // that cannot be simplified to fewer lines // … y[40000] = 0.5*x[40000] - x[12345] + 5*x[0]; return y; } Assume for the purpose of this question that hard-coding these 40000

Why the difference in code generation for bool = bool ? int : int

痞子三分冷 提交于 2019-12-13 14:48:56
问题 This code... bool condSet(int cond, int a, int b) { return cond ? a : b; } ..Generates for gcc 6.3... test edx, edx setne al test edi, edi jne .L6 rep ret .L6: test esi, esi setne al ret .. For icc 17... test edi, edi cmovne edx, esi mov eax, 1 test edx, edx cmove eax, edx ret ..And for clang 3.9 test edi, edi cmove esi, edx test esi, esi setne al ret Why do we have theses differences, for a code pattern, that I'd expect to be common? They all rely on conditional instruction, setne, cmovne,

Can the C preprocessor perform arithmetic and if so, how?

我是研究僧i 提交于 2019-12-13 14:25:34
问题 I'm currently writing code for a microcontroller; since the ATMega128 does not have a hardware multiplier or divider, these operations must be done in software and they take up a decent amount of cycles. However, for code portability and ease of use, I'd prefer not to hard-code precomputed values into my code So for instance, I have a number of tasks which are dependent on the system clock frequency. Currently I' running at 16MHz, but should I choose to lower that, say to reduce power

Forcing GCC to perform loop unswitching of memcpy runtime size checks?

三世轮回 提交于 2019-12-13 12:23:34
问题 Is there any reliable way to force GCC (or any compiler) to factor out runtime size checks in memcpy() outside of a loop (where that size is not compile-time constant, but constant within that loop), specializing the loop for each relevant size range rather than repeatedly checking the size within it? This is an test case reduced down from a performance regression reported here for an open source library designed for efficient in-memory analysis of large data sets. (The regression happens to

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

纵饮孤独 提交于 2019-12-13 11:36:27
问题 When writing a project, I ran into a strange issue. This is the minimal code I managed to write to recreate the issue. I am intentionally storing an actual string in the place of something else, with enough space allocated. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <stddef.h> // For offsetof() typedef struct _pack{ // The type of `c` doesn't matter as long as it's inside of a struct. int64_t c; } pack; int main(){ pack *p; char str[9] =

Swift App Takes ~6 Minutes To Build

天涯浪子 提交于 2019-12-13 10:58:18
问题 I have a Swift app with an array of about ~100k strings. The array looks something like this: let strings: [String] = [ "a", "as", // 99,998 elements later... "zebra" ] It takes nearly 6 minutes to build and run the app in the iOS Simulator. I've isolated the slow build time to the inclusion of this array in the project. Once built, subsequent launches are very fast (until I have to build again). What can I do to speed up the build process? 回答1: Per the comments above, the best solution for

Global variables modified by main() and accessed by ISR()

北城以北 提交于 2019-12-13 07:44:22
问题 Here is my c code char global_variable = 0; ISR(){ PORTA = global_variable; toggle_led;//to make sure that the interrupt is triggered } int main(){ while(1){ _delay_ms(500); gobal_variable++; PORTB = global_variable; } return 0; } The bottom line is that I have a global variable modified by the main function and read by both the main and ISR - interrupt handler . When the global variable is read by main I get the expected values, but in ISR I get the value that was first assigned to the

Is there a reason why NOT to force 8-byte alignment for complex float type?

[亡魂溺海] 提交于 2019-12-13 03:38:23
问题 This is a follow-up for this question. We have an implementation of GCC for our embedded architecture. As such we have control over some aspects of the compiler and optimizer. Such aspect may be potentially forcing the 8-byte aligned allocation of complex float objects. Generally speaking, on our architecture we can optimize access to these objects if they are properly aligned, by requiring a single double-load instruction instead of two regular loads. Just before a round of enhancements and

No Memory Alignment with GCC

て烟熏妆下的殇ゞ 提交于 2019-12-12 12:19:52
问题 I am working with some packet data. I have created structs to hold the packet data. These structs have been generated by python for a specific networking protocol. The issue is that due to the fact that the compiler aligns the structures, when I send the data via the networking protocol, the message ends up being longer than I would like. This causes the other device to not recognize the command. Does anyone know possible a work around this so that my packers are exactly the size the struct