PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] What is __ext_vector_type__ and simd?

 July 10, 2022     c, c++, clang, reference, simd     No comments   

Issue

I'm playing with Apple Metal API along with so called simd library. There's such code in a header:

typedef __attribute__((__ext_vector_type__(3))) float vector_float3;

And I'm curious what it actually does and why the compiler doesn't allow references to vector_float3 in a function's argument or in the next manner:

vector_float3  v;
vector_float3& ref = v;

Solution

It appears to be a clang extension to GNU C vector extensions, where something like this would be normal:

typedef int v4si __attribute__ ((vector_size (16)));

Googling for ext_vector_type without the leading/trailing underscores found the Clang docs. (The underscores version is usable in headers even if some program had used #define on the non-underscores version of the token. But names with leading underscores in the global namespace are reserved for the implementation.)

My best guess is that it's a way to let the compiler know you only care about the value of the first 3 elements of a 4 element SIMD register. So it can potentially do optimizations that leave different garbage in the high element than the source would have.


why the compiler doesn't allow references to vector_float3

Works for me with clang3.8. Try a different compiler.

typedef __attribute__((__ext_vector_type__(3))) float vector_float3;

int foo(vector_float3 &arg) {
  vector_float3  v;
  vector_float3& ref = v;  // unused, but syntactically valid
  return arg[0];           // return the low element of the vector, converted to an integer.  GNU C vector-extensions syntax.
}

compiles to the following asm on the Godbolt compiler explorer

    # targeting x86-64 SystemV ABI (so the first integer/pointer arg is in rdi)
    cvttss2si       eax, dword ptr [rdi]
    ret


Answered By - Peter Cordes
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,259,132

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing