gcc

Type casting: double to char: multiple questions

核能气质少年 提交于 2021-02-10 10:41:47
问题 Consider this code: #include <stdio.h> int main(void) { /* TEST 1 */ double d = 128; char ch = (char)d; printf("%d\n", ch); /* TEST 2 */ printf("%d\n", (char)128.0); /* TEST 3 */ char ch1 = (char)128.0; printf("%d\n", ch1); return 0; } Results: gcc* clang* cl* TEST 1 -128 -128 -128 TEST 2 127 0 -128 TEST 3 127 -2 -128 * latest version Questions: Why the results differ between tests (excluding cl )? Why the results differ between compilers (excluding TEST 1 )? In case of UB/IB, where is the UB

Type casting: double to char: multiple questions

南笙酒味 提交于 2021-02-10 10:41:26
问题 Consider this code: #include <stdio.h> int main(void) { /* TEST 1 */ double d = 128; char ch = (char)d; printf("%d\n", ch); /* TEST 2 */ printf("%d\n", (char)128.0); /* TEST 3 */ char ch1 = (char)128.0; printf("%d\n", ch1); return 0; } Results: gcc* clang* cl* TEST 1 -128 -128 -128 TEST 2 127 0 -128 TEST 3 127 -2 -128 * latest version Questions: Why the results differ between tests (excluding cl )? Why the results differ between compilers (excluding TEST 1 )? In case of UB/IB, where is the UB

C bitwise AND gives different result with -O0 and -O2

痞子三分冷 提交于 2021-02-10 07:35:10
问题 I'm working on a PC emulator using both Bochs and DOSBox as references. When emulating the "NEG Ed" instruction (two's complement negation of a doubleword) I'm getting different results if I compile with -O0 rather than -O2 . This is a test program with just the relevant bits: #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <stdbool.h> int main(int argc, const char **argv) { uint32_t value = strtol(argv[1], NULL, 16); uint32_t negation = -(int32_t)(value); bool sign =

How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

房东的猫 提交于 2021-02-10 06:56:53
问题 Consider the following small function: void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; } Using gcc, this compiles to: foo: nop mov DWORD PTR [rdi+40], 2 ret Note in particular, that the first write to iptr , iptr[10] = 1 doesn't occur at all: the inline asm nop is the first thing in the function, and only the final write of 2 appears (after the ASM call). Apparently the compiler decides that it only needs to provide an up-to-date version of the value of

How can I indicate that the memory *pointed* to by an inline ASM argument may be used?

百般思念 提交于 2021-02-10 06:56:16
问题 Consider the following small function: void foo(int* iptr) { iptr[10] = 1; __asm__ volatile ("nop"::"r"(iptr):); iptr[10] = 2; } Using gcc, this compiles to: foo: nop mov DWORD PTR [rdi+40], 2 ret Note in particular, that the first write to iptr , iptr[10] = 1 doesn't occur at all: the inline asm nop is the first thing in the function, and only the final write of 2 appears (after the ASM call). Apparently the compiler decides that it only needs to provide an up-to-date version of the value of

IRQ symbol defined in static library does not override weak IRQ definition from ARM/GCC startup

大憨熊 提交于 2021-02-10 06:42:26
问题 I have built a static library (*.a for gcc, *.lib for keil). One of its source file, compiled into library, contains definition of RADIO_IRQHandler. An excerpt from this source file, called "ral_irq_handlers.c", is below: ... void ral_symbol_import(void) //dummy function { return; } ... void RADIO_IRQHandler(void) { ... } ... This IRQ symbol definition should override the weak definition which is declared in startup file. The startup file is not compiled into any static library and is just a

MinGW GCC wildcard

那年仲夏 提交于 2021-02-10 06:26:26
问题 I'm using MinGW GCC compiler on windows how to compile all C files in a directory. I used gcc *.c -o Output after I entered the required folder and I got this error gcc: error: *.c: Invalid argument gcc: fatal error: no input files compilation terminated. the used version of GCC is 4.7.1 回答1: For anyone else like me who's come across this problem: I ran into this problem when I installed MinGW-w64 on my tablet after having run it for a few years on my desktop. Basically, I have a ruby script

GCC Inline Assembler “memory” Clobber don't prevent from re-arrange the code in ARM

时光总嘲笑我的痴心妄想 提交于 2021-02-10 06:14:04
问题 I read article about GCC Inline Assembler (http://www.ethernut.de/en/documents/arm-inline-asm.html). In this article, "memory" Clobber forces the compiler to store all cached values before and reload them after executing the assembler instructions. And it must retain the sequence. this is the example. The following code intends to multiply c with b, of which one or both may be modified by an interrupt routine. Disabling interrupts before accessing the variables and re-enable them afterwards

Constexpr Factorial Compilation Results in VS2015 and GCC 5.4.0

感情迁移 提交于 2021-02-10 05:58:45
问题 Wondering if the following surprises anyone, as it did me? Alex Allain's article here on using constexpr shows the following factorial example: constexpr factorial (int n) { return n > 0 ? n * factorial( n - 1 ) : 1; } And states: Now you can use factorial(2) and when the compiler sees it, it can optimize away the call and make the calculation entirely at compile time. I tried this in VS2015 in Release mode with full optimizations on (/Ox) and stepped through the code in the debugger viewing

How to support dynamic plugins when statically linking in standard libraries?

大城市里の小女人 提交于 2021-02-10 05:40:50
问题 Suppose an application myapp.exe is built using g++ and it uses the flag -static-libstdc++ so that it can be installed in enviroments without libstdc++.so . myapp.exe also adds plugin support for some function plugf that can be dynamically loading via dlopen from a shard library. If libplug.so is such a plugin library that also links to libstdc++ , how can it do so in a way to be able to work with myapp.exe ? This is straightforward if libstdc++ is linked in dynamically since both myapp.exe