- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# arm_sqrt_q15.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------
    00002 * Copyright (C) 2010 ARM Limited. All rights reserved.
    00003 *
    00004 * $Date:        15. July 2011
    00005 * $Revision:    V1.0.10
    00006 *
    00007 * Project:      CMSIS DSP Library
    00008 * Title:        arm_sqrt_q15.c
    00009 *
    00010 * Description:  Q15 square root function.
    00011 *
    00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
    00013 *
    00014 * Version 1.0.10 2011/7/15
    00015 *    Big Endian support added and Merged M0 and M3/M4 Source code.
    00016 *
    00017 * Version 1.0.3 2010/11/29
    00018 *    Re-organized the CMSIS folders and updated documentation.
    00019 *
    00020 * Version 1.0.2 2010/11/11
    00021 *    Documentation updated.
    00022 *
    00023 * Version 1.0.1 2010/10/05
    00024 *    Production release and review comments incorporated.
    00025 *
    00026 * Version 1.0.0 2010/09/20
    00027 *    Production release and review comments incorporated.
    00028 * -------------------------------------------------------------------- */
    00029
    00030 #include "arm_math.h"
    00031 #include "arm_common_tables.h"
    00032
    00033
    00051 arm_status arm_sqrt_q15(
    00052   q15_t in,
    00053   q15_t * pOut)
    00054 {
    00055   q31_t prevOut;
    00056   q15_t oneByOut;
    00057   uint32_t sign_bits;
    00058
    00059 #ifndef ARM_MATH_CM0
    00060
    00061   /* Run the below code for Cortex-M4 and Cortex-M3 */
    00062
    00063   q31_t out;
    00064
    00065   if(in > 0)
    00066   {
    00067     /* run for ten iterations */
    00068
    00069     /* Take initial guess as half of the input and first iteration */
    00070     out = ((q31_t) in >> 1u) + 0x3FFF;
    00071
    00072     /* Calculation of reciprocal of out */
    00073     /* oneByOut contains reciprocal of out which is in 2.14 format
    00074        and oneByOut should be upscaled by signBits */
    00075     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00076
    00077     /* 0.5 * (out) */
    00078     out = out >> 1u;
    00079     /* prevOut = 0.5 * out + (in * (oneByOut << signBits))) */
    00080     prevOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00081
    00082     /* Third iteration */
    00083     sign_bits = arm_recip_q15((q15_t) prevOut, &oneByOut, armRecipTableQ15);
    00084     prevOut = prevOut >> 1u;
    00085     out = prevOut + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00086
    00087     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00088     out = out >> 1u;
    00089     prevOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00090
    00091     /* Fifth iteration */
    00092     sign_bits = arm_recip_q15((q15_t) prevOut, &oneByOut, armRecipTableQ15);
    00093     prevOut = prevOut >> 1u;
    00094     out = prevOut + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00095
    00096     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00097     out = out >> 1u;
    00098     prevOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00099
    00100     /* Seventh iteration */
    00101     sign_bits = arm_recip_q15((q15_t) prevOut, &oneByOut, armRecipTableQ15);
    00102     prevOut = prevOut >> 1u;
    00103     out = prevOut + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00104
    00105     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00106     out = out >> 1u;
    00107     prevOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00108
    00109     sign_bits = arm_recip_q15((q15_t) prevOut, &oneByOut, armRecipTableQ15);
    00110     prevOut = prevOut >> 1u;
    00111     out = prevOut + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00112
    00113     /* tenth iteration */
    00114     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00115     out = out >> 1u;
    00116     *pOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00117
    00118     return (ARM_MATH_SUCCESS);
    00119   }
    00120
    00121 #else
    00122
    00123   /* Run the below code for Cortex-M0 */
    00124
    00125   q31_t out, loopVar;                            /* Temporary variable for output, loop variable */
    00126   if(in > 0)
    00127   {
    00128     /* run for ten iterations */
    00129
    00130     /* Take initial guess as half of the input and first iteration */
    00131     out = ((q31_t) in >> 1u) + 0x3FFF;
    00132
    00133     /* Calculation of reciprocal of out */
    00134
    00135     /* oneByOut contains reciprocal of out which is in 2.14 format
    00136        and oneByOut should be upscaled by sign bits */
    00137     sign_bits = arm_recip_q15((q15_t) out, &oneByOut, armRecipTableQ15);
    00138
    00139     /* 0.5 * (out) */
    00140     out = out >> 1u;
    00141     /* prevOut = 0.5 * out + (in * oneByOut) << signbits))) */
    00142     prevOut = out + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00143
    00144     /* loop for third iteration to tenth iteration */
    00145
    00146     for (loopVar = 1; loopVar <= 8; loopVar++)
    00147     {
    00148
    00149       sign_bits = arm_recip_q15((q15_t) prevOut, &oneByOut, armRecipTableQ15);
    00150       /* 0.5 * (prevOut) */
    00151       prevOut = prevOut >> 1u;
    00152       /* prevOut = 0.5 * prevOut+ (in * oneByOut) << signbits))) */
    00153       out =
    00154         prevOut + (((q15_t) (((q31_t) in * oneByOut) >> 16)) << sign_bits);
    00155       /* prevOut      = out */
    00156       prevOut = out;
    00157
    00158     }
    00159     /* output is moved to pOut pointer */
    00160     *pOut = prevOut;
    00161
    00162     return (ARM_MATH_SUCCESS);
    00163   }
    00164
    00165 #endif /* #ifndef ARM_MATH_CM0 */
    00166
    00167   else
    00168   {
    00169
    00170     *pOut = 0;
    00171     return (ARM_MATH_ARGUMENT_ERROR);
    00172   }
    00173
    00174 }
    00175

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines