V4.0.1 of the ARM CMSIS DSP libraries. Note that arm_bitreversal2.s, arm_cfft_f32.c and arm_rfft_fast_f32.c had to be removed. arm_bitreversal2.s will not assemble with the online tools. So, the fast f32 FFT functions are not yet available. All the other FFT functions are available.
Dependents: MPU9150_Example fir_f32 fir_f32 MPU9150_nucleo_noni2cdev ... more
FastMathFunctions/arm_sqrt_q31.c@0:3d9c67d97d6f, 2014-07-28 (annotated)
- Committer:
- emh203
- Date:
- Mon Jul 28 15:03:15 2014 +0000
- Revision:
- 0:3d9c67d97d6f
1st working commit. Had to remove arm_bitreversal2.s arm_cfft_f32.c and arm_rfft_fast_f32.c. The .s will not assemble. For now I removed these functions so we could at least have a library for the other functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:3d9c67d97d6f | 1 | /* ---------------------------------------------------------------------- |
emh203 | 0:3d9c67d97d6f | 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
emh203 | 0:3d9c67d97d6f | 3 | * |
emh203 | 0:3d9c67d97d6f | 4 | * $Date: 12. March 2014 |
emh203 | 0:3d9c67d97d6f | 5 | * $Revision: V1.4.3 |
emh203 | 0:3d9c67d97d6f | 6 | * |
emh203 | 0:3d9c67d97d6f | 7 | * Project: CMSIS DSP Library |
emh203 | 0:3d9c67d97d6f | 8 | * Title: arm_sqrt_q31.c |
emh203 | 0:3d9c67d97d6f | 9 | * |
emh203 | 0:3d9c67d97d6f | 10 | * Description: Q31 square root function. |
emh203 | 0:3d9c67d97d6f | 11 | * |
emh203 | 0:3d9c67d97d6f | 12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
emh203 | 0:3d9c67d97d6f | 13 | * |
emh203 | 0:3d9c67d97d6f | 14 | * Redistribution and use in source and binary forms, with or without |
emh203 | 0:3d9c67d97d6f | 15 | * modification, are permitted provided that the following conditions |
emh203 | 0:3d9c67d97d6f | 16 | * are met: |
emh203 | 0:3d9c67d97d6f | 17 | * - Redistributions of source code must retain the above copyright |
emh203 | 0:3d9c67d97d6f | 18 | * notice, this list of conditions and the following disclaimer. |
emh203 | 0:3d9c67d97d6f | 19 | * - Redistributions in binary form must reproduce the above copyright |
emh203 | 0:3d9c67d97d6f | 20 | * notice, this list of conditions and the following disclaimer in |
emh203 | 0:3d9c67d97d6f | 21 | * the documentation and/or other materials provided with the |
emh203 | 0:3d9c67d97d6f | 22 | * distribution. |
emh203 | 0:3d9c67d97d6f | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
emh203 | 0:3d9c67d97d6f | 24 | * may be used to endorse or promote products derived from this |
emh203 | 0:3d9c67d97d6f | 25 | * software without specific prior written permission. |
emh203 | 0:3d9c67d97d6f | 26 | * |
emh203 | 0:3d9c67d97d6f | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
emh203 | 0:3d9c67d97d6f | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
emh203 | 0:3d9c67d97d6f | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
emh203 | 0:3d9c67d97d6f | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
emh203 | 0:3d9c67d97d6f | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
emh203 | 0:3d9c67d97d6f | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
emh203 | 0:3d9c67d97d6f | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
emh203 | 0:3d9c67d97d6f | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
emh203 | 0:3d9c67d97d6f | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
emh203 | 0:3d9c67d97d6f | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
emh203 | 0:3d9c67d97d6f | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
emh203 | 0:3d9c67d97d6f | 38 | * POSSIBILITY OF SUCH DAMAGE. |
emh203 | 0:3d9c67d97d6f | 39 | * -------------------------------------------------------------------- */ |
emh203 | 0:3d9c67d97d6f | 40 | #include "arm_math.h" |
emh203 | 0:3d9c67d97d6f | 41 | #include "arm_common_tables.h" |
emh203 | 0:3d9c67d97d6f | 42 | |
emh203 | 0:3d9c67d97d6f | 43 | /** |
emh203 | 0:3d9c67d97d6f | 44 | * @ingroup groupFastMath |
emh203 | 0:3d9c67d97d6f | 45 | */ |
emh203 | 0:3d9c67d97d6f | 46 | |
emh203 | 0:3d9c67d97d6f | 47 | /** |
emh203 | 0:3d9c67d97d6f | 48 | * @addtogroup SQRT |
emh203 | 0:3d9c67d97d6f | 49 | * @{ |
emh203 | 0:3d9c67d97d6f | 50 | */ |
emh203 | 0:3d9c67d97d6f | 51 | |
emh203 | 0:3d9c67d97d6f | 52 | /** |
emh203 | 0:3d9c67d97d6f | 53 | * @brief Q31 square root function. |
emh203 | 0:3d9c67d97d6f | 54 | * @param[in] in input value. The range of the input value is [0 +1) or 0x00000000 to 0x7FFFFFFF. |
emh203 | 0:3d9c67d97d6f | 55 | * @param[out] *pOut square root of input value. |
emh203 | 0:3d9c67d97d6f | 56 | * @return The function returns ARM_MATH_SUCCESS if the input value is positive |
emh203 | 0:3d9c67d97d6f | 57 | * and ARM_MATH_ARGUMENT_ERROR if the input is negative. For |
emh203 | 0:3d9c67d97d6f | 58 | * negative inputs, the function returns *pOut = 0. |
emh203 | 0:3d9c67d97d6f | 59 | */ |
emh203 | 0:3d9c67d97d6f | 60 | |
emh203 | 0:3d9c67d97d6f | 61 | arm_status arm_sqrt_q31( |
emh203 | 0:3d9c67d97d6f | 62 | q31_t in, |
emh203 | 0:3d9c67d97d6f | 63 | q31_t * pOut) |
emh203 | 0:3d9c67d97d6f | 64 | { |
emh203 | 0:3d9c67d97d6f | 65 | q31_t number, temp1, bits_val1, var1, signBits1, half; |
emh203 | 0:3d9c67d97d6f | 66 | float32_t temp_float1; |
emh203 | 0:3d9c67d97d6f | 67 | union |
emh203 | 0:3d9c67d97d6f | 68 | { |
emh203 | 0:3d9c67d97d6f | 69 | q31_t fracval; |
emh203 | 0:3d9c67d97d6f | 70 | float32_t floatval; |
emh203 | 0:3d9c67d97d6f | 71 | } tempconv; |
emh203 | 0:3d9c67d97d6f | 72 | |
emh203 | 0:3d9c67d97d6f | 73 | number = in; |
emh203 | 0:3d9c67d97d6f | 74 | |
emh203 | 0:3d9c67d97d6f | 75 | /* If the input is a positive number then compute the signBits. */ |
emh203 | 0:3d9c67d97d6f | 76 | if(number > 0) |
emh203 | 0:3d9c67d97d6f | 77 | { |
emh203 | 0:3d9c67d97d6f | 78 | signBits1 = __CLZ(number) - 1; |
emh203 | 0:3d9c67d97d6f | 79 | |
emh203 | 0:3d9c67d97d6f | 80 | /* Shift by the number of signBits1 */ |
emh203 | 0:3d9c67d97d6f | 81 | if((signBits1 % 2) == 0) |
emh203 | 0:3d9c67d97d6f | 82 | { |
emh203 | 0:3d9c67d97d6f | 83 | number = number << signBits1; |
emh203 | 0:3d9c67d97d6f | 84 | } |
emh203 | 0:3d9c67d97d6f | 85 | else |
emh203 | 0:3d9c67d97d6f | 86 | { |
emh203 | 0:3d9c67d97d6f | 87 | number = number << (signBits1 - 1); |
emh203 | 0:3d9c67d97d6f | 88 | } |
emh203 | 0:3d9c67d97d6f | 89 | |
emh203 | 0:3d9c67d97d6f | 90 | /* Calculate half value of the number */ |
emh203 | 0:3d9c67d97d6f | 91 | half = number >> 1; |
emh203 | 0:3d9c67d97d6f | 92 | /* Store the number for later use */ |
emh203 | 0:3d9c67d97d6f | 93 | temp1 = number; |
emh203 | 0:3d9c67d97d6f | 94 | |
emh203 | 0:3d9c67d97d6f | 95 | /*Convert to float */ |
emh203 | 0:3d9c67d97d6f | 96 | temp_float1 = number * 4.6566128731e-010f; |
emh203 | 0:3d9c67d97d6f | 97 | /*Store as integer */ |
emh203 | 0:3d9c67d97d6f | 98 | tempconv.floatval = temp_float1; |
emh203 | 0:3d9c67d97d6f | 99 | bits_val1 = tempconv.fracval; |
emh203 | 0:3d9c67d97d6f | 100 | /* Subtract the shifted value from the magic number to give intial guess */ |
emh203 | 0:3d9c67d97d6f | 101 | bits_val1 = 0x5f3759df - (bits_val1 >> 1); // gives initial guess |
emh203 | 0:3d9c67d97d6f | 102 | /* Store as float */ |
emh203 | 0:3d9c67d97d6f | 103 | tempconv.fracval = bits_val1; |
emh203 | 0:3d9c67d97d6f | 104 | temp_float1 = tempconv.floatval; |
emh203 | 0:3d9c67d97d6f | 105 | /* Convert to integer format */ |
emh203 | 0:3d9c67d97d6f | 106 | var1 = (q31_t) (temp_float1 * 1073741824); |
emh203 | 0:3d9c67d97d6f | 107 | |
emh203 | 0:3d9c67d97d6f | 108 | /* 1st iteration */ |
emh203 | 0:3d9c67d97d6f | 109 | var1 = ((q31_t) ((q63_t) var1 * (0x30000000 - |
emh203 | 0:3d9c67d97d6f | 110 | ((q31_t) |
emh203 | 0:3d9c67d97d6f | 111 | ((((q31_t) |
emh203 | 0:3d9c67d97d6f | 112 | (((q63_t) var1 * var1) >> 31)) * |
emh203 | 0:3d9c67d97d6f | 113 | (q63_t) half) >> 31))) >> 31)) << 2; |
emh203 | 0:3d9c67d97d6f | 114 | /* 2nd iteration */ |
emh203 | 0:3d9c67d97d6f | 115 | var1 = ((q31_t) ((q63_t) var1 * (0x30000000 - |
emh203 | 0:3d9c67d97d6f | 116 | ((q31_t) |
emh203 | 0:3d9c67d97d6f | 117 | ((((q31_t) |
emh203 | 0:3d9c67d97d6f | 118 | (((q63_t) var1 * var1) >> 31)) * |
emh203 | 0:3d9c67d97d6f | 119 | (q63_t) half) >> 31))) >> 31)) << 2; |
emh203 | 0:3d9c67d97d6f | 120 | /* 3rd iteration */ |
emh203 | 0:3d9c67d97d6f | 121 | var1 = ((q31_t) ((q63_t) var1 * (0x30000000 - |
emh203 | 0:3d9c67d97d6f | 122 | ((q31_t) |
emh203 | 0:3d9c67d97d6f | 123 | ((((q31_t) |
emh203 | 0:3d9c67d97d6f | 124 | (((q63_t) var1 * var1) >> 31)) * |
emh203 | 0:3d9c67d97d6f | 125 | (q63_t) half) >> 31))) >> 31)) << 2; |
emh203 | 0:3d9c67d97d6f | 126 | |
emh203 | 0:3d9c67d97d6f | 127 | /* Multiply the inverse square root with the original value */ |
emh203 | 0:3d9c67d97d6f | 128 | var1 = ((q31_t) (((q63_t) temp1 * var1) >> 31)) << 1; |
emh203 | 0:3d9c67d97d6f | 129 | |
emh203 | 0:3d9c67d97d6f | 130 | /* Shift the output down accordingly */ |
emh203 | 0:3d9c67d97d6f | 131 | if((signBits1 % 2) == 0) |
emh203 | 0:3d9c67d97d6f | 132 | { |
emh203 | 0:3d9c67d97d6f | 133 | var1 = var1 >> (signBits1 / 2); |
emh203 | 0:3d9c67d97d6f | 134 | } |
emh203 | 0:3d9c67d97d6f | 135 | else |
emh203 | 0:3d9c67d97d6f | 136 | { |
emh203 | 0:3d9c67d97d6f | 137 | var1 = var1 >> ((signBits1 - 1) / 2); |
emh203 | 0:3d9c67d97d6f | 138 | } |
emh203 | 0:3d9c67d97d6f | 139 | *pOut = var1; |
emh203 | 0:3d9c67d97d6f | 140 | |
emh203 | 0:3d9c67d97d6f | 141 | return (ARM_MATH_SUCCESS); |
emh203 | 0:3d9c67d97d6f | 142 | } |
emh203 | 0:3d9c67d97d6f | 143 | /* If the number is a negative number then store zero as its square root value */ |
emh203 | 0:3d9c67d97d6f | 144 | else |
emh203 | 0:3d9c67d97d6f | 145 | { |
emh203 | 0:3d9c67d97d6f | 146 | *pOut = 0; |
emh203 | 0:3d9c67d97d6f | 147 | return (ARM_MATH_ARGUMENT_ERROR); |
emh203 | 0:3d9c67d97d6f | 148 | } |
emh203 | 0:3d9c67d97d6f | 149 | } |
emh203 | 0:3d9c67d97d6f | 150 | |
emh203 | 0:3d9c67d97d6f | 151 | /** |
emh203 | 0:3d9c67d97d6f | 152 | * @} end of SQRT group |
emh203 | 0:3d9c67d97d6f | 153 | */ |