├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal Nifty 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | 117 | 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NEON intrinsics guide 2 | 3 | Makes ARM NEON documentation accessible (with examples). Born from frustration with ARM documentation and general lack of examples. 4 | 5 | **Update: earlier this year (2020) ARM released [new docs](https://developer.arm.com/architectures/instruction-sets/simd-isas/neon/neon-programmers-guide-for-armv8-a)** 6 | 7 | ## Intro 8 | 9 | When you convert your iOS code to NEON, usually it's inside loops that can be written in parallel code. Also you have to keep in mind that the more load/store operations you have, the slower your code will be. 10 | 11 | ## Assumptions 12 | 13 | This guide is about inline *NEON intrinsics*, which should work on both 32bit and 64bit architectures. Vectors are always supposed to be of length 4, but you can generally just remove the letter *q* in the instruction name to use 2-vectors. 14 | 15 | ## Supported types 16 | 17 | While you can see all available types in [Apple's source code](https://opensource.apple.com/source/gcc/gcc-5659/gcc/config/arm/arm_neon.h.auto.html), there are mainly these scalar types contained in vectors: 18 | 19 | - `uint8` (really fast, but can only use for masks or short ranges) 20 | - `uint16` (typical fast values) 21 | - `uint32` (full precision) 22 | - `uint64` (I personally have never seen this used) 23 | - `int8` 24 | - `int16` 25 | - `int32` 26 | - `int64` 27 | - `float16` (doesn't seem supported by Apple CPU intrinsics, but it is supported in [Metal](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf) for example) 28 | - `float32` (full precision and slowest type) 29 | - `poly` (used for [carryless multiplication](https://en.wikipedia.org/wiki/CLMUL_instruction_set) and useful for cryptography) 30 | 31 | These are then composed into vector types like `float32x4_t` or `int8x16_t`, to make full use of the available registers (total of 128 bits), an exception is `int8x8_t`. There are vector of vectors types as well, but they don't provide any speed bump over the standard 128 bit ones. 32 | 33 | ## Syntax 34 | 35 | ### Header 36 | 37 | Include this header in iOS to include supported ARM intrinsics and types: 38 | 39 | ```c 40 | #include 41 | ``` 42 | 43 | [Source here](https://opensource.apple.com/source/gcc/gcc-5659/gcc/config/arm/arm_neon.h.auto.html). 44 | 45 | ### Detecting support at build time 46 | 47 | To detect support for NEON at build time (e.g. build branches or pragmas, you want to exclude ARM instructions when running on the Simulator etc.) use `__ARM_NEON__`. 48 | 49 | ### Float 50 | 51 | #### Arithmetic 52 | 53 | - add: **vaddq_f32** or **vaddq_f64** 54 | ```c 55 | float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 }, v2 = { 1.0, 1.0, 1.0, 1.0 }; 56 | float32x4_t sum = vaddq_f32(v1, v2); 57 | // => sum = { 2.0, 3.0, 4.0, 5.0 } 58 | ``` 59 | - multiply: **vmulq_f32** or **vmulq_f64** 60 | ```c 61 | float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 }, v2 = { 1.0, 1.0, 1.0, 1.0 }; 62 | float32x4_t prod = vmulq_f32(v1, v2); 63 | // => prod = { 1.0, 2.0, 3.0, 4.0 } 64 | ``` 65 | - multiply and accumulate: **vmlaq_f32** 66 | ```c 67 | float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 }, v2 = { 2.0, 2.0, 2.0, 2.0 }, v3 = { 3.0, 3.0, 3.0, 3.0 }; 68 | float32x4_t acc = vmlaq_f32(v3, v1, v2); // acc = v3 + v1 * v2 69 | // => acc = { 5.0, 7.0, 9.0, 11.0 } 70 | ``` 71 | - multiply by a scalar: **vmulq_n_f32** or **vmulq_n_f64** 72 | ```c 73 | float32x4_t v = { 1.0, 2.0, 3.0, 4.0 }; 74 | float32_t s = 3.0; 75 | float32x4_t prod = vmulq_n_f32(v, s); 76 | // => prod = { 3.0, 6.0, 9.0, 12.0 } 77 | ``` 78 | - multiply by a scalar and accumulate: **vmlaq_n_f32** or **vmlaq_n_f64** 79 | ```c 80 | float32x4_t v1 = { 1.0, 2.0, 3.0, 4.0 }, v2 = { 1.0, 1.0, 1.0, 1.0 }; 81 | float32_t s = 3.0; 82 | float32x4_t acc = vmlaq_n_f32(v1, v2, s); 83 | // => acc = { 4.0, 5.0, 6.0, 7.0 } 84 | ``` 85 | - invert (needed for division): **vrecpeq_f32** or **vrecpeq_f64** 86 | ```c 87 | float32x4_t v = { 1.0, 2.0, 3.0, 4.0 }; 88 | float32x4_t reciprocal = vrecpeq_f32(v); 89 | // => reciprocal = { 0.998046875, 0.499023438, 0.333007813, 0.249511719 } 90 | ``` 91 | - invert (more accurately): use a [Newton-Raphson iteration](http://en.wikipedia.org/wiki/Division_algorithm#Newton.E2.80.93Raphson_division) to refine the estimate 92 | ```c 93 | float32x4_t v = { 1.0, 2.0, 3.0, 4.0 }; 94 | float32x4_t reciprocal = vrecpeq_f32(v); 95 | float32x4_t inverse = vmulq_f32(vrecpsq_f32(v, reciprocal), reciprocal); 96 | // => inverse = { 0.999996185, 0.499998093, 0.333333015, 0.249999046 } 97 | ``` 98 | 99 | #### Load 100 | 101 | - load vector: **vld1q_f32** or **vld1q_f64** 102 | ```c 103 | float values[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 }; 104 | float32x4_t v = vld1q_f32(values); 105 | // => v = { 1.0, 2.0, 3.0, 4.0 } 106 | ``` 107 | - load same value for all lanes: **vld1q_dup_f32** or **vld1q_dup_f64** 108 | ```c 109 | float val = 3.0; 110 | float32x4_t v = vld1q_dup_f32(&val); 111 | // => v = { 3.0, 3.0, 3.0, 3.0 } 112 | ``` 113 | 114 | - set all lanes to a hardcoded value: **vmovq_n_f16** or **vmovq_n_f32** or **vmovq_n_f64** 115 | ```c 116 | float32x4_t v = vmovq_n_f32(1.5); 117 | // => v = { 1.5, 1.5, 1.5, 1.5 } 118 | ``` 119 | #### Store 120 | 121 | - store vector: **vst1q_f32** or **vst1q_f64** 122 | ```c 123 | float32x4_t v = { 1.0, 2.0, 3.0, 4.0 }; 124 | float values[5]; 125 | vst1q_f32(values, v); 126 | // => values = { 1.0, 2.0, 3.0, 4.0, #undef } 127 | ``` 128 | - store lane of array of vectors: **vst4q_lane_f16** or **vst4q_lane_f32** or **vst4q_lane_f64** (change to **vst1...** / **vst2...** / **vst3...** for other array lengths); 129 | ```c 130 | float32x4_t v0 = { 1.0, 2.0, 3.0, 4.0 }, v1 = { 5.0, 6.0, 7.0, 8.0 }, v2 = { 9.0, 10.0, 11.0, 12.0 }, v3 = { 13.0, 14.0, 15.0, 16.0 }; 131 | float32x4x4_t u = { v0, v1, v2, v3 }; 132 | float buff[4]; 133 | vst4q_lane_f32(buff, u, 0); 134 | // => buff = { 1.0, 5.0, 9.0, 13.0 } 135 | ``` 136 | 137 | #### Arrays 138 | 139 | - access to values: **val[n]** 140 | ```c 141 | float32x4_t v0 = { 1.0, 2.0, 3.0, 4.0 }, v1 = { 5.0, 6.0, 7.0, 8.0 }, v2 = { 9.0, 10.0, 11.0, 12.0 }, v3 = { 13.0, 14.0, 15.0, 16.0 }; 142 | float32x4x4_t ary = { v0, v1, v2, v3 }; 143 | float32x4_t v = ary.val[2]; 144 | // => v = { 9.0, 10.0, 11.0, 12.0 } 145 | ``` 146 | 147 | #### Max and min 148 | 149 | - max of two vectors, element by element: 150 | ```c 151 | float32x4_t v0 = { 5.0, 2.0, 3.0, 4.0 }, v1 = { 1.0, 6.0, 7.0, 8.0 }; 152 | float32x4_t v2 = vmaxq_f32(v0, v1); 153 | // => v2 = { 5.0, 6.0, 7.0, 8.0 } 154 | ``` 155 | 156 | - max of vector elements, using folding maximum: 157 | ```c 158 | float32x4_t v0 = { 1.0, 2.0, 3.0, 4.0 }; 159 | float32x2_t maxOfHalfs = vpmax_f32(vget_low_f32(v0), vget_high_f32(v0)); 160 | float32x2_t maxOfMaxOfHalfs = vpmax_f32(maxOfHalfs, maxOfHalfs); 161 | float maxValue = vget_lane_f32(maxOfMaxOfHalfs, 0); 162 | // => maxValue = 4.0 163 | ``` 164 | 165 | - min of two vectors, element by element: 166 | ```c 167 | float32x4_t v0 = { 5.0, 2.0, 3.0, 4.0 }, v1 = { 1.0, 6.0, 7.0, 8.0 }; 168 | float32x4_t v2 = vminq_f32(v0, v1); 169 | // => v2 = { 1.0, 2.0, 3.0, 4.0 } 170 | ``` 171 | 172 | - min of vector elements, using folding minimum: 173 | ```c 174 | float32x4_t v0 = { 1.0, 2.0, 3.0, 4.0 }; 175 | float32x2_t minOfHalfs = vpmin_f32(vget_low_f32(v0), vget_high_f32(v0)); 176 | float32x2_t minOfMinOfHalfs = vpmin_f32(minOfHalfs, minOfHalfs); 177 | float minValue = vget_lane_f32(minOfMinOfHalfs, 0); 178 | // => minValue = 1.0 179 | ``` 180 | 181 | ### Conditionals 182 | 183 | - ternary operator: use vector comparison (for example **vcltq_f32** for *less than* comparison) 184 | ```c 185 | float32x4_t v1 = { 1.0, 0.0, 1.0, 0.0 }, v2 = { 0.0, 1.0, 1.0, 0.0 }; 186 | uint32x4_t mask = vcltq_f32(v1, v2); // v1 < v2 187 | float32x4_t ones = vmovq_n_f32(10.0), twos = vmovq_n_f32(20.0); // the conditional branches: if condition is true returns 10.0, else returns 20.0 188 | float32x4_t v3 = vbslq_f32(mask, ones, twos); // will select first if mask 1, second if mask 0 189 | // => v3 = { 20.0, 10.0, 20.0, 20.0 } 190 | ``` 191 | 192 | Conditional branches are really bad for NEON cpus. In general we need eager execution (calculating both branches first, and then deciding which results to actually use in which lanes) and it's not possible to skip any steps. 193 | 194 | ## Links 195 | 196 | - [summary of NEON intrinsics](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491h/CIHJBEFE.html) 197 | - [ARM NEON intrinsics reference](http://infocenter.arm.com/help/topic/com.arm.doc.ihi0073a/IHI0073A_arm_neon_intrinsics_ref.pdf?resultof=%22%76%6d%6f%76%71%5f%6e%5f%66%33%32%22%20) 198 | 199 | ## Contributing 200 | 201 | Change README.md and send a pull request. 202 | 203 | ## Author 204 | 205 | This has been provided as part of the development that happens at [Nifty](http://www.thenifty.me). 206 | 207 | With Nifty, the automated measurement app for easy and confident shopping, online shopping is a unique experience tailored to each shopper allowing them to buy garments with the perfect fit even on the go. 208 | --------------------------------------------------------------------------------