📄 vector-extensions.html
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded in the section entitled "GNU Free Documentation License". <p>(a) The FSF's Front-Cover Text is: <p>A GNU Manual <p>(b) The FSF's Back-Cover Text is: <p>You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller }--></style></head><body><div class="node"><p>Node: <a name="Vector%20Extensions">Vector Extensions</a>,Next: <a rel="next" accesskey="n" href="Other-Builtins.html#Other%20Builtins">Other Builtins</a>,Previous: <a rel="previous" accesskey="p" href="Return-Address.html#Return%20Address">Return Address</a>,Up: <a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a><hr><br></div><h3 class="section">Using vector instructions through built-in functions</h3><p>On some targets, the instruction set contains SIMD vector instructions thatoperate on multiple values contained in one large register at the same time. For example, on the i386 the MMX, 3Dnow! and SSE extensions can be usedthis way. <p>The first step in using these extensions is to provide the necessary datatypes. This should be done using an appropriate <code>typedef</code>:<pre class="smallexample"> typedef int v4si __attribute__ ((vector_size (16))); </pre> <p>The <code>int</code> type specifies the base type, while the attribute specifiesthe vector size for the variable, measured in bytes. For example, thedeclaration above causes the compiler to set the mode for the <code>v4si</code>type to be 16 bytes wide and divided into <code>int</code> sized units. Fora 32-bit <code>int</code> this means a vector of 4 units of 4 bytes, and thecorresponding mode of <code>foo</code> will be <small>V4SI</small>. <p>The <code>vector_size</code> attribute is only applicable to integral andfloat scalars, although arrays, pointers, and function return valuesare allowed in conjunction with this construct. <p>All the basic integer types can be used as base types, both as signedand as unsigned: <code>char</code>, <code>short</code>, <code>int</code>, <code>long</code>,<code>long long</code>. In addition, <code>float</code> and <code>double</code> can beused to build floating-point vector types. <p>Specifying a combination that is not valid for the current architecturewill cause GCC to synthesize the instructions using a narrower mode. For example, if you specify a variable of type <code>V4SI</code> and yourarchitecture does not allow for this specific SIMD type, GCC willproduce code that uses 4 <code>SIs</code>. <p>The types defined in this manner can be used with a subset of normal Coperations. Currently, GCC will allow using the following operatorson these types: <code>+, -, *, /, unary minus, ^, |, &, ~</code>. <p>The operations behave like C++ <code>valarrays</code>. Addition is defined asthe addition of the corresponding elements of the operands. Forexample, in the code below, each of the 4 elements in <var>a</var> will beadded to the corresponding 4 elements in <var>b</var> and the resultingvector will be stored in <var>c</var>.<pre class="smallexample"> typedef int v4si __attribute__ ((vector_size (16))); v4si a, b, c; c = a + b; </pre> <p>Subtraction, multiplication, division, and the logical operationsoperate in a similar manner. Likewise, the result of using the unaryminus or complement operators on a vector type is a vector whoseelements are the negative or complemented values of the correspondingelements in the operand. <p>You can declare variables and use them in function calls and returns, aswell as in assignments and some casts. You can specify a vector type asa return type for a function. Vector types can also be used as functionarguments. It is possible to cast from one vector type to another,provided they are of the same size (in fact, you can also cast vectorsto and from other datatypes of the same size). <p>You cannot operate between vectors of different lengths or differentsignedness without a cast. <p>A port that supports hardware vector operations, usually provides a setof built-in functions that can be used to operate on vectors. Forexample, a function to add two vectors and multiply the result by athird could look like this:<pre class="smallexample"> v4si f (v4si a, v4si b, v4si c) { v4si tmp = __builtin_addv4si (a, b); return __builtin_mulv4si (tmp, c); } </pre> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -