How to use higher ISA's while restraining GCC to lower ISA's

Jonathan Wakely jwakely.gcc@gmail.com
Mon May 22 11:49:00 GMT 2017


On 22 May 2017 at 07:46, Jeffrey Walton wrote:
> I'm testing some code that wants to use SSE2 and above. The code is
> guarded by runtime CPU checks. The options used to compile are
> minimal. For example, Debian will effectively compile with only
> -mcpu=i686 even though MMX and SSE are ubiquitous since the late
> 1990s/early 2000's.
>
> According to https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes,
> I access different ISA's with the target attribute:
>
>  __attribute__ ((__target__ ("sse4_2")))
> int main(int argc, char* argv[])
> {
>   if (HasSSE4())
>   {
>      // use SSE4
>   }
>   else if (HasSSE2())
>   {
>      // use SSE2
>   }
>   else
>   {
>     // use C or C++
>   }
>
>   return 0;
> }
>
> Debian packagers will compile the code with, say, `gcc -mcpu=i686
> ...`. (I believe they omit the -march=X and -mcpu=X, which effectively
> targets a machine without MMX, SSE and friends).

You've contradicted yourself there, did you mean they omit -march=X
and -mtune=X?

For x86 -mcpu=X and -mtune=X are the same, so they only omit -march=X
then. Is that right?

> Does GCC restrict itself to i686 when it selects instructions? If not,

No, not with -mcpu=i686

> then how do I ensure GCC only uses i686 or x86_64 when it selects
> instructions?

It restricts itself to the (explicit or implicit) -march setting. This
is explained at https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
under the -mtune option.



More information about the Gcc-help mailing list