add some code
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "dsps_sqrt.h"
|
||||
#include <math.h>
|
||||
|
||||
|
||||
inline float dsps_sqrtf_f32_ansi(float f)
|
||||
{
|
||||
int result;
|
||||
int *f_ptr = (int *)&f;
|
||||
result = 0x1fbb4000 + (*f_ptr >> 1);
|
||||
const int *p = &result;
|
||||
float *f_result = (float *)p;
|
||||
return *f_result;
|
||||
}
|
||||
|
||||
esp_err_t dsps_sqrt_f32_ansi(const float *input, float *output, int len)
|
||||
{
|
||||
if (NULL == input) {
|
||||
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
|
||||
}
|
||||
if (NULL == output) {
|
||||
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < len ; i++) {
|
||||
output[i] = dsps_sqrtf_f32_ansi(input[i]);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
float dsps_inverted_sqrtf_f32_ansi(float data )
|
||||
{
|
||||
const float x2 = data * 0.5F;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
union {
|
||||
float f;
|
||||
uint32_t i;
|
||||
} conv = {data}; // member 'f' set to value of 'data'.
|
||||
conv.i = 0x5f3759df - ( conv.i >> 1 );
|
||||
conv.f *= ( threehalfs - ( x2 * conv.f * conv.f ) );
|
||||
return conv.f;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _dsps_sqrt_H_
|
||||
#define _dsps_sqrt_H_
|
||||
#include "dsp_err.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief square root approximation
|
||||
*
|
||||
* The function takes square root approximation
|
||||
* x[i] ~ sqrt(y[i]); i=[0..len)
|
||||
* The implementation use ANSI C and could be compiled and run on any platform
|
||||
*
|
||||
* @param[in] input: input array
|
||||
* @param output: output array
|
||||
* @param len: amount of operations for arrays
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - One of the error codes from DSP library
|
||||
*/
|
||||
esp_err_t dsps_sqrt_f32_ansi(const float *input, float *output, int len);
|
||||
//esp_err_t dsps_sqrt_s32_ansi(const int32_t *input, int16_t *output, int len);
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief square root approximation
|
||||
*
|
||||
* The function takes square root approximation
|
||||
* x ~ sqrt(y);
|
||||
* The implementation use ANSI C and could be compiled and run on any platform
|
||||
*
|
||||
* @param[in] data: input value
|
||||
*
|
||||
* @return
|
||||
* - square root value
|
||||
*/
|
||||
float dsps_sqrtf_f32_ansi(const float data);
|
||||
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief inverted square root approximation
|
||||
*
|
||||
* The function takes inverted square root approximation
|
||||
* x ~ 1/sqrt(y);
|
||||
* The implementation use ANSI C and could be compiled and run on any platform
|
||||
*
|
||||
* @param[in] data: input value
|
||||
*
|
||||
* @return
|
||||
* - inverted square root value
|
||||
*/
|
||||
float dsps_inverted_sqrtf_f32_ansi(float data );
|
||||
/**@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef CONFIG_DSP_OPTIMIZED
|
||||
#define dsps_sqrt_f32 dsps_sqrt_f32_ansi
|
||||
#define dsps_sqrtf_f32 dsps_sqrtf_f32_ansi
|
||||
#define dsps_inverted_sqrtf_f32 dsps_inverted_sqrtf_f32_ansi
|
||||
#else
|
||||
#define dsps_sqrt_f32 dsps_sqrt_f32_ansi
|
||||
#define dsps_sqrtf_f32 dsps_sqrtf_f32_ansi
|
||||
#define dsps_inverted_sqrtf_f32 dsps_inverted_sqrtf_f32_ansi
|
||||
#endif
|
||||
|
||||
#endif // _dsps_sqrt_H_
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright 2018-2023 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "dsp_platform.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "dsp_tests.h"
|
||||
#include "dsps_sqrt.h"
|
||||
#include "esp_attr.h"
|
||||
|
||||
static const char *TAG = "dsps_sqrt";
|
||||
|
||||
TEST_CASE("dsps_sqrtf_f32_ansi functionality", "[dsps]")
|
||||
{
|
||||
float max_err = -1000;
|
||||
float max_value = 0;
|
||||
float min_err = 0;
|
||||
float min_value = (float)INT32_MAX;
|
||||
int test_points = 100000;
|
||||
for (size_t i = 0; i < test_points; i++) {
|
||||
float test_value = rand();
|
||||
// if (test_value > max_value) max_value = test_value;
|
||||
// if (test_value < min_value) min_value = test_value;
|
||||
float x = sqrtf(test_value);
|
||||
float y = dsps_sqrtf_f32(test_value);
|
||||
float error = 20 * log10f(fabs((x - y) / x) + 0.000001);
|
||||
if (error > max_err) {
|
||||
max_err = error;
|
||||
max_value = test_value;
|
||||
}
|
||||
if (error < min_err) {
|
||||
min_err = error;
|
||||
min_value = test_value;
|
||||
}
|
||||
if (error > -25) {
|
||||
ESP_LOGE(TAG, "dsps_sqrtf_f32_ansi: error = %f dB, value = %f (0x%8.8x)\n", error, test_value, (int)test_value);
|
||||
TEST_ASSERT_EQUAL(x, y);
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "dsps_sqrtf_f32_ansi: max error = %f dB, min error = %f dB, max_value = %f (0x%8.8x), min_value = %f (0x%8.8x)\n", max_err, min_err, max_value, (int)max_value, min_value, (int)min_value);
|
||||
}
|
||||
|
||||
TEST_CASE("dsps_sqrt_f32_ansi functionality", "[dsps]")
|
||||
{
|
||||
int n = 256;
|
||||
float *x = (float *)malloc(sizeof(float) * n);
|
||||
float *result = (float *)malloc(sizeof(float) * n);
|
||||
float *y = (float *)malloc(sizeof(float) * n);
|
||||
for (int i = 0 ; i < n ; i++) {
|
||||
y[i] = i * 10;
|
||||
x[i] = y[i] * y[i];
|
||||
}
|
||||
unsigned int start_b = dsp_get_cpu_cycle_count();
|
||||
dsps_sqrt_f32_ansi(x, result, n);
|
||||
float cycles = dsp_get_cpu_cycle_count() - start_b;
|
||||
|
||||
for (int i = 0 ; i < n ; i++) {
|
||||
//printf("Result[%i] = %f, expected = %f, diff = %f\n", i, result[i], y[i], 20*logf(fabs((result[i] - y[i])/y[i]) + 0.000001));
|
||||
float error = 20 * log10f(fabs((result[i] - y[i]) / y[i]) + 0.000001);
|
||||
if (error > -25) {
|
||||
TEST_ASSERT_EQUAL(result[i], y[i]);
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "dsps_sqrt_f32_ansi - %f cycles for %i samples \n", cycles, n);
|
||||
free(x);
|
||||
free(y);
|
||||
free(result);
|
||||
}
|
||||
Reference in New Issue
Block a user