-4

I want to create a C code that somehow contains two separated blocks. I want to use a function or a tool that extracts the CPU model, and based on that, the program decides which block of code it executes. I only have the idea and I don't know how to implement it ! The first block of code will be executed on an Intel i7 and the second should be executed on ARM Cortex A53. PS : I am a beginner and I have nothing to do with hardware and similar stuff. Thank you for your help :)

AsmaAP
  • 21
  • 6
  • 1
    Google is your friend ... there are so many examples how you can do it. – YesThatIsMyName Jul 18 '18 at 09:42
  • 1
    you can't execute binary for some CPU for another, you must cross compile your code for both CPUs. since common section of your code that determines CPU type must run on both CPUs, you can do this by using system tools such as `lscpu` or `cat /proc/cpuinfo` linux command and base on the result run one of the two cross compiled programs. – AVM Jul 18 '18 at 09:52
  • If you only need to differentiate between specific ARM cpus, you can simply read the cpuid register to check the exact model: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0432c/Bhccjgga.html – Ctx Jul 18 '18 at 09:59
  • @AVM If you fiddle about it long enough, I bet someone could come up with some binary code which runs on both arm and intel to switch to the correct program section for the current architecture. – Ctx Jul 18 '18 at 10:04
  • The phrase to google on it `runtime CPU dispatching`. (Or compile-time with `#ifdef __x86_64__` if you only need ARM vs. x86). – Peter Cordes Jul 18 '18 at 10:20
  • 1
    @AVM, ctx: "CPU model" does not mean different architectures. I'd rather assume it's specific additional features like most/all multimedia codecs can use SSE/MMX/etc. extensions on x86 which support them. Nevertheless, there is no simeple answer without much more information (whcih OP seems to not want to provide). – too honest for this site Jul 18 '18 at 10:29
  • @Olaf the tags are both "arm" and "intel", so I assume the opposite. – Ctx Jul 18 '18 at 10:49
  • 2
    @Ctx: That's exactly what comments and (finally) close-votes are for. Apparentyl this is another fire&forget question. I don't think we should waste even more time on it; we already put more into it than OP did. – too honest for this site Jul 18 '18 at 10:51
  • Thank you for your answers. However, none of them helped me :( – AsmaAP Jul 18 '18 at 12:42
  • re: your update. It sounds like you just want compile-time detection, with `#ifdef` / `#else`, so the ARM binary includes one block of code and the x86-64 binary includes the other. I close this as a duplicate of a question about that. Update again if that's not what you were looking for. – Peter Cordes Jul 18 '18 at 23:53

2 Answers2

0

As clearly pointed out, first off you cant have a C program that runs to a point to determine ARM from x86 as that code has to already be ARM or x86. These are different instruction sets. You can use say python or JAVA or some other scripty/virtual machine language. But then you have a COMPILE time decision to build for one target or the other, at that point you already know which target as you are actually running code on it, so if this is strictly ARM vs X86 there is no reason to check runtime. Thats not to say that each architecture and/or system will have a way to check the architecture and flavor you are on ARMv6 vs ARMv7, for example, but not necessarily ARMv7 32 bit vs ARMv8 64 bit although you technically can run aarch32 and aarch64 instruction sets on most ARMv8s just not intermixed, have to have the os or execution level changes yourself to switch between them.

You do understand there are different incompatible instruction sets, specifically the ones you described and C code is compiled to one or the other. So you cannot have a program in C compiled for a target that can detect the other target. You have already selected the target before you get to this point. Now there are emulators, but they tend to target one architecture as well. There are/were products from specific vendors that would emulate one instruction set and convert it runtime to the other, over time as you re-run that code it continues to convert it. You could try that, but you still have to be running code for the right target on the right logic/emulator, and then have a now special detection that is not the norm to find the true underlaying architecture, not the faked emulator.

I suspect you are thinking you can have one architecture specific module that detects the architecture to run architecture specific code. This does not work with C in general, does not make sense to try, thus there probably isnt a good tool for this. In particular since the solution for such a thing is either you build this into the binary file format and the operating system picks because it knows, or you wrap your binary with a target independent language like Python or JAVA or scripty language like perl, bash, etc. that can independent of target determine the architecture (in that case solutions vary widely specific to operating system and language for starters) and then choose which binary to run.

old_timer
  • 69,149
  • 8
  • 89
  • 168
-1

There are many ways to achieve what you want. To check which model is present you first have to read which model you have. how to do that varies between Windows and Linux. i found this SO-topic helpful and it might also be a good start for your research: How to check CPU name, model, speed on Windows/Linux C?

Teekeks
  • 196
  • 10
  • 1
    This could be a comment, it's a low-quality answer, though. You basically redirect to another post. What if there is no OS? We don't even know which architecture. Please resonsider answering too broad questions. They are not a good fit for this site as we concentrate on **specific** questions with high-quality answers. Flag them for closure instead. – too honest for this site Jul 18 '18 at 09:54
  • thank you I already read it and it didn't help. That's not what I'm looking for. – AsmaAP Jul 18 '18 at 12:44