I have a Microsoft Surface Pro (5th Generation) with the i5 processor and use the GNU GCC Compiler.
Now I want to use optimization settings for this compiler and I don't know what settings are "working" with my processor.
Overview of my system:
Microsoft Surface Pro
Intel Core i5-7300U CPU # 2,60 GHz
Microsoft Windows 10 Pro | 64-bit | Version 10.0.18363
IDE: Code::Blocks 20.03
Does anyone has an a similar system? Thanks in advance.
Kind Regards
randomcoder
You can always use the flag -march=native, which tells the compiler to detect the CPU of the computer that's being used to compile, and optimize the code for that type of CPU. This is ideal when you are compiling on the same machine you will be running the code on. Note that the resulting executable will not necessarily work on other computers with different CPUs.
Of course, you also have to use one of the -O options, typically -O2, to get optimizations at all. There are many other specific optimization options, but whether they are helpful will depend on what your program actually does.
Related
I’m trying to programmatically detect at runtime what would be the best GCC -march flag for a given CPU. The program I’m developing will download optimized binaries depending on the user's CPU architecture. So I must detect which architecture it is in this list:
core2
nehalem
westmere
sandybridge
ivybridge
haswell
broadwell
generic (is all above failed).
Like this command: gcc -march=native -Q --help=target | grep -- '-march=' | cut -f3, without invoking GCC of course (I can’t embed it). I found the function __builtin_cpu_is of GCC, but it does not support all architectures.
I’m looking for a portable way of doing so, at least for GNU/Linux, Windows and OSX. I’m only planning to use GCC as compiler.
I have written a program with AVX intrinsics, which works well using Ubuntu 12.4 LTS and GCC 4.6 with the following compilation line: g++ -g -Wall -mavx ProgramName.cc -o ProgramName
The problem started When i have updated the compiler up to 4.7 and 4.8.1 versions to support the 16-bit AVX2 intrinsics, which is not supported in gcc 4.6
Currently, the updated gcc version compiles both AVX and AVX2 programs properly. However, it gives me the following error when i run the program: Illegal instruction (core dumped), although it was working on gcc 4.6
My question is: what is prefect way to compile and run both AVX and AVX2 intrinsics
If you tell gcc to use AVX2, it will do so, regardless of whether your CPU supports them or not. That can be useful for cross-compiling or for examining gcc's code generation, but it's not particularly helpful for running programs. If your program crashes with an illegal instruction exception, it is most likely that your CPU does not support the AVX2 extension.
On i386 and x86-64 platforms (and in certain other circumstances), you can specify the gcc option -march=native to generate code for the host machines instruction code. The compiled code might not work on another machine with fewer capabilities, but it should allow you to use all the features of your machine.
While -march=native is a good solution for generating executables, it does not actually help much with writing code; you still need to tailor the instrinsics for the target's architecture, and writing code which can take advantage of CPU features without relying on them gets complicated. I don't know of a good C solution, but there are several C++ template frameworks available.
Upgrading to gcc 4.8 likely pulled in AVX512, so you would have needed to limit the generated instr mix to ONLY AVX2 for your machine.
The question says it all. I need to cross-compile for a Cyrix CPU. The system the compiler (doesn't have to be gcc) needs to run on is a 64bit Kubuntu, with an i5 processor. I couldn't find anything useful googling, except for a piece of information saying that "Cx486DX is software-compatible with i486". So I ran
gcc -m32 -march=i486 helloworld.c -o helloworld486.bin
but executing helloworld486.bin on the Cyrix machine gives me a floating point exception. My knowledge about CPUs is rather limited and I'm out of ideas now, any help would be really appreciated.
Unfortunately you need more than just a compiler that generates instructions for the 486. The compiler libraries, as well as any libraries that are linked in statically should be suitable as well. The GCC version included in most current Linux distributions is able to generate 486-only object files (I think), but its libraries and stub objects (e.g. crtbegin.o) have been pre-generated for 686 CPUs.
There are two main alternatives here:
Use a Linux build system that is compiled for 486 itself, either in a VM or in a chroot jail. Unfortunately getting a modern Linux distribution for the 486 is a bit of an issue - every single major distribution has moved on. Perhaps a (much) older Linux distribution would be of help?
Create a full cross-compiler toolchain for the 486. You can then cross-compile separate versions of all needed libraries and have your build scripts use them. Quite honestly, ensuring that nothing from the (usually 686-based) build host slips through to the build result is not very easy. It oftens amounts to cross-compiling a whole Linux system from scratch, ala CLFS.
An automated cross-compiler toolchain build script, such as crosstool-ng might be of help.
Could you add more details about your target system? Is it an embedded system or just an old PC? What OS is it using? Would it be possible to just run your compile in a VM with a version of the target OS?
I am new to ARM. I have written C code,but I'm looking to port the code to ARM which runs on all ARM processors. I am going to develop high end applications such as those which run on smartphones/ tablets. Can you guys tell which is the best compiler for that? I have heard about RVDS, but that seems to be expensive.
My other question is: Can I use Microsoft Visual studio 2008 to program ARM Intrinsic like Neon? Are there any other alternatives for RVDS?
Your advice is greatly appreciated.
Several version of the gcc toolchain are available for ARM processors. You need to figure out the exact target for your code such as:
Which processor or set of processors ? What instruction sets and extensions are supported ? (There are many like ARMv6, ARMV7, ARMv7a). Some of them might have NEON support and some might not.
What kind of FPU support - hard FP vs soft FP ?
What kind of OS environment ? Linux ? Android ? Bare metal ?
Which C library - glibc, uclibc, bionic libc or something else ?
The last 2 are kind of built into the toolchain and cannot be modified. But choosing the exact instruction set and optimization can be controlled using ARM specific -m flags in gcc.
And distros like Ubuntu already have the ARM toolchain as part of their repos. You can give that a try too :
sudo apt-get install gcc-arm-linux-gnueabi
Recently I've been playing around with cross compiling using GCC and discovered what seems to be a complicated area, tool-chains.
I don't quite understand this as I was under the impression GCC can create binary machine code for most of the common architectures, and all that else really matters is what libraries you link with and what type of executable is created.
Can GCC not do all these things itself? With a single build of GCC, all the appropriate libraries and the correct flags sent to GCC, could I produce a PE executable for a Windows x86 machine, then create an ELF executable for an embedded Linux MIPS device and finally an executable for an OSX PowerPC machine?
If not can someone explain how you would achieve this?
With a single build of GCC, all the
appropriate libraries and the correct
flags sent to GCC, could I produce a
PE executable for a Windows x86
machine, then create an ELF executable
for an embedded Linux MIPS device and
finally an executable for an OSX
PowerPC machine? If not can someone
explain how you would achieve this?
No. A single build of GCC produces object code for one target architecture. You would need a build targeting Intel x86, a build targeting MIPS, and a build targeting PowerPC. However, the compiler is not the only tool you need, despite the fact that you can build source code into an executable with a single invocation of GCC. Under the hood, it makes use of the assembler (as) and linker (ld) as well, and those need to be built for the target architecture and platform. Usually GCC uses the versions of these tools from the GNU binutils package, so you'd need to build that for the target platform too.
You can read more about building a cross-compiling toolchain here.
I don't quite understand this as I was
under the impression GCC can create
binary machine code for most of the
common architectures
This is true in the sense that the source code of GCC itself can be built into compilers that target various architectures, but you still require separate builds.
Regarding -march, this does not allow the same build of GCC to switch between platforms. Rather it's used to select the allowable instructions to use for the same family of processors. For example, some of the instructions supported by modern x86 processors weren't supported by the earliest x86 processors because they were introduced later on (such as extension instruction sets like MMX and SSE). When you pass -march, GCC enables all opcodes supported on that processor and its predecessors. To quote the GCC manual:
While picking a specific cpu-type will
schedule things appropriately for that
particular chip, the compiler will not
generate any code that does not run on
the i386 without the -march=cpu-type
option being used.
If you want to try cross-compiling, and don't want to build the toolchain yourself, I'd recommend looking at CodeSourcery. They have a GNU-based toolchain, and their free "Lite" version supports quite a few architectures. I've used it for Linux/ARM and Android/ARM.