带有restrict关键字的typedef

带有restrict关键字的typedef,c,gcc,clang,typedef,C,Gcc,Clang,Typedef,我定义了这样的对齐浮动 typedef __attribute((aligned(64))) float aligned_float; typedef aligned_float * restrict aligned_floatptr; typedef __attribute((aligned(64))) float * restrict aligned_floatptr2 static void kernel(float * restrict a, float * restrict b,

我定义了这样的对齐浮动

typedef __attribute((aligned(64))) float aligned_float;
typedef aligned_float * restrict aligned_floatptr;
typedef __attribute((aligned(64))) float * restrict aligned_floatptr2
static void kernel(float * restrict a, float * restrict b, float * restrict c, int n) {
  a = __builtin_assume_aligned(a, 64);
  b = __builtin_assume_aligned(b, 64);
  c = __builtin_assume_aligned(c, 64);
  //rest of code
然后我用
restrict
关键字定义对齐的浮动,如下所示

typedef __attribute((aligned(64))) float aligned_float;
typedef aligned_float * restrict aligned_floatptr;
typedef __attribute((aligned(64))) float * restrict aligned_floatptr2
static void kernel(float * restrict a, float * restrict b, float * restrict c, int n) {
  a = __builtin_assume_aligned(a, 64);
  b = __builtin_assume_aligned(b, 64);
  c = __builtin_assume_aligned(c, 64);
  //rest of code
这和我预期的一样。但因为我几乎总是想把这两个放在一起,所以我试着用一行像这样的typedef

typedef __attribute((aligned(64))) float aligned_float;
typedef aligned_float * restrict aligned_floatptr;
typedef __attribute((aligned(64))) float * restrict aligned_floatptr2
static void kernel(float * restrict a, float * restrict b, float * restrict c, int n) {
  a = __builtin_assume_aligned(a, 64);
  b = __builtin_assume_aligned(b, 64);
  c = __builtin_assume_aligned(c, 64);
  //rest of code
然而,这是行不通的。
restrict
关键字仍被识别,但对齐方式未被识别。然而,编译器没有给我任何警告。我只是通过查看组件才意识到对齐不起作用

为什么组合定义不起作用,为什么我没有得到警告

您可以看到Clang和GCC的程序集


我想这样做的原因是我有这样的代码

typedef __attribute((aligned(64))) float aligned_float;
typedef aligned_float * restrict aligned_floatptr;
typedef __attribute((aligned(64))) float * restrict aligned_floatptr2
static void kernel(float * restrict a, float * restrict b, float * restrict c, int n) {
  a = __builtin_assume_aligned(a, 64);
  b = __builtin_assume_aligned(b, 64);
  c = __builtin_assume_aligned(c, 64);
  //rest of code
}

我有很多不同的版本。我发现它使用起来更方便

static void kernel(aligned_flotptr a, aligned_floatptr b, aligned_floatptr c, int n) {
    //rest of code
}


我刚刚意识到,叮当声似乎连
aligned\u float
都无法识别。只有海湾合作委员会认识到这一点。对于Clang,我仍然需要使用
\u内置\u假定对齐
(或者可能是
\praga omp sind对齐
)。另一方面,即使没有对齐,Clang也能生成好的代码(未对齐的指令在几代人的时间内都与对齐的版本一样快),因此我真正需要对齐的是GCC。

该属性很可能应用于
aligned\u floatptr2
而不是
float
type.1)从不使用typedef指针。2) 将某事物定义为
restrict
,这有什么意义呢?类型如何知道程序将如何使用该类型的变量?一般来说,在类型定义中使用类型限定符是个坏主意。@Zboson-维基百科中的示例至少可以说是不规范的。这是一个特别的玩具示例,应该删除它作为副作用的不良实践。@Lundin
intptr\t
是一个愚蠢的示例。Wikipedia定义了一个他们称之为
intptr
的int指针,我把它和
intptr\t
混淆了。好的,这是我第一次键入指针。我承认一开始我不喜欢它,因为它不再是指针了,但另一方面,它消除了大量代码重复<代码>浮动ptr a比
浮动*限制a
短得多。我在密集矩阵乘法中使用
restrict
获得了巨大的好处。@Zboson“保存按键”是改变程序设计的一个可怕的理由。如果您觉得原来的声明行变长了,那么可以对代码进行格式化,使每个函数参数都有自己的一行。