mirror of
https://github.com/Fishwaldo/bl_mcu_sdk.git
synced 2025-07-23 21:29:17 +00:00
[feat][nmsis] add nmsis component and nn,dsp demo
This commit is contained in:
parent
b2aada479b
commit
5d1126d0f0
989 changed files with 286224 additions and 0 deletions
|
@ -0,0 +1,5 @@
|
|||
set(TARGET_REQUIRED_PRIVATE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
set(TARGET_REQUIRED_SRCS divide.c exp.c log.c sqrt.c)
|
||||
set(TARGET_REQUIRED_LIBS nmsis)
|
||||
set(mains main.c)
|
||||
generate_bin()
|
49
examples/dsp/FastMathFunctions_squareRootPart/divide.c
Normal file
49
examples/dsp/FastMathFunctions_squareRootPart/divide.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include "ref.h"
|
||||
|
||||
riscv_status ref_divide_q15(q15_t numerator,
|
||||
q15_t denominator,
|
||||
q15_t *quotient,
|
||||
int16_t *shift)
|
||||
{
|
||||
int16_t sign=0;
|
||||
q31_t temp;
|
||||
int16_t shiftForNormalizing;
|
||||
|
||||
*shift = 0;
|
||||
|
||||
sign = (numerator>>15) ^ (denominator>>15);
|
||||
|
||||
if (denominator == 0)
|
||||
{
|
||||
if (sign)
|
||||
{
|
||||
*quotient = 0x8000;
|
||||
}
|
||||
else
|
||||
{
|
||||
*quotient = 0x7FFF;
|
||||
}
|
||||
return(RISCV_MATH_NANINF);
|
||||
}
|
||||
|
||||
numerator = abs(numerator);
|
||||
denominator = abs(denominator);
|
||||
|
||||
temp = ((q31_t)numerator << 15) / ((q31_t)denominator);
|
||||
|
||||
shiftForNormalizing= 17 - __CLZ(temp);
|
||||
if (shiftForNormalizing > 0)
|
||||
{
|
||||
*shift = shiftForNormalizing;
|
||||
temp = temp >> shiftForNormalizing;
|
||||
}
|
||||
|
||||
if (sign)
|
||||
{
|
||||
temp = -temp;
|
||||
}
|
||||
|
||||
*quotient=temp;
|
||||
|
||||
return(RISCV_MATH_SUCCESS);
|
||||
}
|
22
examples/dsp/FastMathFunctions_squareRootPart/exp.c
Normal file
22
examples/dsp/FastMathFunctions_squareRootPart/exp.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_vexp_f32(
|
||||
const float32_t * pSrc,
|
||||
float32_t * pDst,
|
||||
uint32_t blockSize)
|
||||
{
|
||||
uint32_t blkCnt;
|
||||
|
||||
blkCnt = blockSize;
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* C = log(A) */
|
||||
|
||||
/* Calculate log and store result in destination buffer. */
|
||||
*pDst++ = expf(*pSrc++);
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
}
|
22
examples/dsp/FastMathFunctions_squareRootPart/log.c
Normal file
22
examples/dsp/FastMathFunctions_squareRootPart/log.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include "ref.h"
|
||||
|
||||
void ref_vlog_f32(
|
||||
const float32_t * pSrc,
|
||||
float32_t * pDst,
|
||||
uint32_t blockSize)
|
||||
{
|
||||
uint32_t blkCnt;
|
||||
|
||||
blkCnt = blockSize;
|
||||
|
||||
while (blkCnt > 0U)
|
||||
{
|
||||
/* C = log(A) */
|
||||
|
||||
/* Calculate log and store result in destination buffer. */
|
||||
*pDst++ = logf(*pSrc++);
|
||||
|
||||
/* Decrement loop counter */
|
||||
blkCnt--;
|
||||
}
|
||||
}
|
144
examples/dsp/FastMathFunctions_squareRootPart/main.c
Normal file
144
examples/dsp/FastMathFunctions_squareRootPart/main.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
//
|
||||
// Created by lujun on 19-6-28.
|
||||
//
|
||||
// This contains sin, cos and sqrt calculates
|
||||
// each one has it's own function.
|
||||
// All function can be found in main function.
|
||||
// If you don't want to use it, then comment it.
|
||||
#include "riscv_math.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "../common.h"
|
||||
|
||||
#include "../HelperFunctions/math_helper.c"
|
||||
#include "../HelperFunctions/ref_helper.c"
|
||||
|
||||
#include <stdio.h>
|
||||
#define DELTAF32 (0.05f)
|
||||
#define DELTAQ31 (63)
|
||||
#define DELTAQ15 (1)
|
||||
#define DELTAQ7 (1)
|
||||
|
||||
int test_flag_error = 0;
|
||||
|
||||
static int DSP_SQRT(void)
|
||||
{
|
||||
float32_t f32_pOUT, f32_pOUT_ref;
|
||||
q31_t q31_pOUT, q31_pOUT_ref;
|
||||
q15_t q15_pOUT, q15_pOUT_ref;
|
||||
// f32_sqrt
|
||||
BENCH_START(riscv_sqrt_f32);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
riscv_sqrt_f32(100, &f32_pOUT);
|
||||
BENCH_END(riscv_sqrt_f32);
|
||||
// ref_sqrt_f32(100, &f32_pOUT_ref);
|
||||
f32_pOUT_ref = 10.0f;
|
||||
if (fabs(f32_pOUT - f32_pOUT_ref) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_sqrt_f32);
|
||||
printf("expect: %f, actual: %f\n", f32_pOUT_ref, f32_pOUT);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_sqrt_f32);
|
||||
// q31_sqrt
|
||||
BENCH_START(riscv_sqrt_q31);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
riscv_sqrt_q31(100, &q31_pOUT);
|
||||
BENCH_END(riscv_sqrt_q31);
|
||||
ref_sqrt_q31(100, &q31_pOUT_ref);
|
||||
if (labs(q31_pOUT - q31_pOUT_ref) > DELTAQ31) {
|
||||
BENCH_ERROR(riscv_sqrt_q31);
|
||||
printf("expect: %x, actual: %x\n", q31_pOUT_ref, q31_pOUT);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_sqrt_q31);
|
||||
// q15_sqrt
|
||||
BENCH_START(riscv_sqrt_q15);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
riscv_sqrt_q15(100, &q15_pOUT);
|
||||
BENCH_END(riscv_sqrt_q15);
|
||||
ref_sqrt_q15(100, &q15_pOUT_ref);
|
||||
if (abs(q15_pOUT - q15_pOUT_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_sqrt_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_pOUT_ref, q15_pOUT);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_sqrt_q15);
|
||||
}
|
||||
|
||||
static int DSP_DIVIDE(void)
|
||||
{
|
||||
q15_t q15_pOUT, q15_pOUT_ref;
|
||||
int16_t shift;
|
||||
// q15_sqrt
|
||||
BENCH_START(riscv_divide_q15);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
riscv_divide_q15(4203, 2490, &q15_pOUT, &shift);
|
||||
BENCH_END(riscv_divide_q15);
|
||||
ref_divide_q15(4203, 2490, &q15_pOUT_ref, &shift);
|
||||
if (abs(q15_pOUT - q15_pOUT_ref) > DELTAQ15) {
|
||||
BENCH_ERROR(riscv_divide_q15);
|
||||
printf("expect: %x, actual: %x\n", q15_pOUT_ref, q15_pOUT);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_divide_q15);
|
||||
}
|
||||
|
||||
static int DSP_EXP(void)
|
||||
{
|
||||
float32_t f32_pOUT[256], f32_pOUT_ref[256];
|
||||
float32_t f32_pIN[256];
|
||||
for (int i = 0; i < 256; i++)
|
||||
f32_pIN[i] = (float32_t) rand();
|
||||
// f32_sqrt
|
||||
BENCH_START(riscv_vexp_f32);
|
||||
for (int i = 0; i < 256; i++)
|
||||
riscv_vexp_f32(f32_pIN, &f32_pOUT, 256);
|
||||
BENCH_END(riscv_vexp_f32);
|
||||
ref_vexp_f32(f32_pIN, &f32_pOUT_ref, 256);
|
||||
for (int i = 0; i < 256; i++)
|
||||
if (fabs(f32_pOUT[i] - f32_pOUT_ref[i]) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_vexp_f32);
|
||||
printf("index: %d, expect: %f, actual: %f\n", i, f32_pOUT_ref[i], f32_pOUT[i]);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_vexp_f32);
|
||||
}
|
||||
|
||||
static int DSP_LOG(void)
|
||||
{
|
||||
float32_t f32_pOUT[256], f32_pOUT_ref[256];
|
||||
float32_t f32_pIN[256];
|
||||
for (int i = 0; i < 256; i++)
|
||||
f32_pIN[i] = (float32_t) rand();
|
||||
// f32_sqrt
|
||||
BENCH_START(riscv_vlog_f32);
|
||||
for (int i = 0; i < 256; i++)
|
||||
riscv_vlog_f32(f32_pIN, &f32_pOUT, 256);
|
||||
BENCH_END(riscv_vlog_f32);
|
||||
ref_vlog_f32(f32_pIN, &f32_pOUT_ref, 256);
|
||||
for (int i = 0; i < 256; i++)
|
||||
if (fabs(f32_pOUT[i] - f32_pOUT_ref[i]) > DELTAF32) {
|
||||
BENCH_ERROR(riscv_vlog_f32);
|
||||
printf("index: %d, expect: %f, actual: %f\n", i, f32_pOUT_ref[i], f32_pOUT[i]);
|
||||
test_flag_error = 1;
|
||||
}
|
||||
BENCH_STATUS(riscv_vlog_f32);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
BENCH_INIT;
|
||||
DSP_SQRT();
|
||||
DSP_DIVIDE();
|
||||
DSP_EXP();
|
||||
DSP_LOG();
|
||||
BENCH_FINISH;
|
||||
if (test_flag_error) {
|
||||
printf("test error apprears, please recheck.\n");
|
||||
return 1;
|
||||
} else {
|
||||
printf("all test are passed. Well done!\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
15
examples/dsp/FastMathFunctions_squareRootPart/sqrt.c
Normal file
15
examples/dsp/FastMathFunctions_squareRootPart/sqrt.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "ref.h"
|
||||
|
||||
riscv_status ref_sqrt_q31(q31_t in, q31_t *pOut)
|
||||
{
|
||||
*pOut = (q31_t)(sqrtf((float32_t)in / 2147483648.0f) * 2147483648.0f);
|
||||
|
||||
return RISCV_MATH_SUCCESS;
|
||||
}
|
||||
|
||||
riscv_status ref_sqrt_q15(q15_t in, q15_t *pOut)
|
||||
{
|
||||
*pOut = (q15_t)(sqrtf((float32_t)in / 32768.0f) * 32768.0f);
|
||||
|
||||
return RISCV_MATH_SUCCESS;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue