ATTRIBUTES-TYPES(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

πŸ’– ATTRIBUTES OF TYPES πŸ’–

okay bestie, welcome to attributes-types(3clc) β€” straight out of the OpenCL Manual πŸ“–βœ¨. This one is all about the little decoration you slap on your types to boss the compiler around. no cap.


🌸 NAME 🌸

Attributes_of_Types β€” Attributes of Types. that's it, that's the name πŸ’…

__attribute__ ((aligned)) __attribute__
((aligned (n))) __attribute__
((packed))

πŸ¦‹ DESCRIPTION πŸ¦‹

So the keyword __attribute__ is your girl for specifying special attributes of enum, struct and union types when you define those types. It's followed by an attribute specification tucked inside double parentheses β€” two of them, don't be out here with one, that's not the vibe πŸ™ˆ

Right now, two attributes are defined for types: aligned, and packed. That's the whole roster, periodt.

You can specify type attributes in an enum, struct, or union type declaration or definition, or for other types in a typedef declaration. yes queen, multiple doors, same energy πŸšͺ✨

For an enum, struct, or union type, you can specify attributes either between the enum, struct or union tag and the name of the type, or just past the closing curly brace of the definition. The former syntax is the preferred one β€” that's the main character placement, the other one is lowkey the backup dancer πŸ’ƒ

✨ aligned (alignment) ✨

This attribute specifies a minimum alignment (in bytes) for variables of the specified type. For example, the declarations:

struct S { short f[3]; } __attribute__
((aligned (8))); typedef int
more_aligned_int __attribute__
((aligned (8)));

force the compiler to ensure (as far as it can) that each variable whose type is struct S or more_aligned_int will be allocated and aligned at least on a 8-byte boundary. like it's literally forcing it, as far as it can 🫢

Note that the alignment of any given struct or union type is required by the ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct or union in question β€” and it must also be a power of two. This means you can effectively adjust the alignment of a struct or union type by attaching an aligned attribute to any one of the members of such a type, but the notation illustrated in the example above is a more obvious, intuitive, and readable way to request the compiler to adjust the alignment of an entire struct or union type. the way that one member can carry the whole team… iconic honestly πŸ‘‘

As in the preceding example, you can explicitly specify the alignment (in bytes) that you wish the compiler to use for a given struct or union type. Alternatively, you can leave out the alignment factor and just ask the compiler to align a type to the maximum useful alignment for the target machine you are compiling for. For example, you could write:

struct S { short f[3]; } __attribute__
((aligned));

Whenever you leave out the alignment factor in an aligned attribute specification, the compiler automatically sets the alignment for the type to the largest alignment which is ever used for any data type on the target machine you are compiling for. In the example above, the size of each short is 2 bytes, and therefore the size of the entire struct S type is 6 bytes. The smallest power of two which is greater than or equal to that is 8, so the compiler sets the alignment for the entire struct S type to 8 bytes. math ate that fr fr πŸ”₯

⚠️ heads up bestie: the effectiveness of aligned attributes may be limited by inherent limitations of the OpenCL device and compiler. For some devices, the OpenCL compiler may only be able to arrange for variables to be aligned up to a certain maximum alignment. If the OpenCL compiler is only able to align variables up to a maximum of 8 byte alignment, then specifying aligned(16) in an __attribute__ will still only provide you with 8 byte alignment. See your platform-specific documentation for further information. no cap, check your platform docs πŸ“š

The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well. See below. πŸ‘‡

πŸŽ€ packed πŸŽ€

The packed attribute, attached to the struct or union type definition, specifies that each member of the structure or union is placed to minimize the memory required. When attached to an enum definition, it indicates that the smallest integral type should be used. tiny but mighty energy πŸœπŸ’–

Specifying this attribute for struct and union types is equivalent to specifying the packed attribute on each of the structure or union members. same vibe, different font.

In the following example struct my_packed_struct's members are packed closely together, but the internal layout of its s member is not packed. To do that, struct my_unpacked_struct would need to be packed, too. it's giving "you have to want it for yourself" 😭

struct my_unpacked_struct { char c; int
i; };

struct __attribute__ ((packed))
my_packed_struct { char c; int i;
struct my_unpacked_struct s; };
🚨 bestie READ THIS ONE: You may only specify this attribute on the definition of a enum, struct, or union, not on a typedef which does not also define the enumerated type, structure or union. that's the rule, don't test it πŸ’€

πŸ“œ SPECIFICATION πŸ“œ

OpenCL Specification[1]


πŸ‘€ SEE ALSO πŸ‘€

attribute(3clc), attributes-blocksAndControlFlow(3clc), attributes-variables(3clc)


🌈 AUTHORS 🌈

The Khronos Group


Copyright Β© 2007-2011 The Khronos Group Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.


πŸ—’οΈ NOTES πŸ—’οΈ

  1. OpenCL Specification page 237, section 6.11.1 - Specifying Attributes of Types

and that's the whole page bestie πŸ’–βœ¨πŸ¦‹

courtesy of The Khronos Group, dated 01/14/2021, from the OpenCL Manual β€” go forth and align your types, you absolute legend πŸ‘‘πŸŒΈ

πŸ“¦ where this page comes from