Delete math, modify kpu

This commit is contained in:
zhaozhongxiang 2018-10-26 17:12:22 +08:00
parent 0357b9b941
commit c67a838a9e
4 changed files with 23 additions and 187 deletions

View File

@ -134,6 +134,7 @@ int kpu_run(kpu_task_t* v_task, dmac_channel_number_t dma_ch, const void *src, v
{
if(g_kpu_context.kpu_status)
return -1;
memcpy((void *)&g_kpu_context.kpu_task, v_task, sizeof(kpu_task_t));
kpu_task_t *task = (kpu_task_t *)&g_kpu_context.kpu_task;
@ -158,3 +159,16 @@ int kpu_run(kpu_task_t* v_task, dmac_channel_number_t dma_ch, const void *src, v
return 0;
}
uint8_t *kpu_get_output_buf(kpu_task_t* task)
{
kpu_layer_argument_t* last_layer = &task->layers[task->length-1];
size_t output_size = ((last_layer->dma_parameter.data.dma_total_byte+1) + 7) / 8 * 8;
return malloc(output_size);
}
void kpu_release_output_buf(uint8_t *output_buf)
{
if(output_buf != NULL)
free(output_buf);
}

View File

@ -343,4 +343,13 @@ extern kpu_task_t* kpu_task_init(kpu_task_t* task);
*/
int kpu_run(kpu_task_t* task, dmac_channel_number_t dma_ch, const void *src, void* dest, plic_irq_callback_t callback);
/**
* @brief Get kpu result buf
*
* @param[in] task Kpu handler
*
* @return Kpu result buf
*/
uint8_t *kpu_get_output_buf(kpu_task_t* task);
#endif

View File

@ -1,71 +0,0 @@
/* Copyright 2018 Canaan Inc.
*
* 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 "fastexp.h"
#include <stdint.h>
static inline float _fast_exp2f(float in)
{
static const float p1 = 0.0f;
static const float p2 = 0.0f;
static const float p3 = 0.0f;
static const float p4 = 0.0f;
static const float p5 = 0.0f;
float x1 = in;
float x2 = x1 * in;
float x3 = x2 * in;
float x4 = x3 * in;
return x4 * p1 + x3 * p2 + x2 * p3 + x1 * p4 + p5;
}
float fast_exp2f(float x)
{
union _float {
float f;
struct {
uint32_t frac : 23;
uint32_t expo : 8;
uint32_t sign : 1;
} data;
};
if (x < 0)
return 1.0f / fast_exp2f(-x);
union _float f = {.f = 1.0f};
f.data.expo = (int)x + 127;
return _fast_exp2f(x - (int)x) * f.f;
}
float fast_expf(float x)
{
extern float __ieee754_expf(float x);
return __ieee754_expf(x);
}
float fast_powf(float base, float expo)
{
extern float __ieee754_powf(float base, float expo);
return __ieee754_powf(base, expo);
}

View File

@ -1,116 +0,0 @@
/* Copyright 2018 Canaan Inc.
*
* 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 KENDRYTE_FAST_EXP
#define KENDRYTE_FAST_EXP
#include <math.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* calc exp2f(x)
**/
float fast_exp2f(float x);
/**
* calc expf(x)
**/
float fast_expf(float x);
/**
* calc powf(base,expo)
**/
float fast_powf(float base, float expo);
/**
* calc exp2(x)
**/
static inline double fast_exp2(double x)
{
return fast_exp2f((float)x);
}
/**
* calc exp(x)
**/
static inline double fast_exp(double x)
{
return fast_expf((float)x);
}
/**
* calc pow(base,expo)
**/
static inline double fast_pow(double base, double expo)
{
return fast_powf((float)base, (float)expo);
}
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
#include <forward_list>
namespace std{
template < typename... Args >
auto fast_pow(Args && ... args)->decltype(::fast_pow(std::forward < Args > (args)...)) {
return ::fast_pow(std::forward < Args > (args)...);
}
template < typename... Args >
auto fast_powf(Args && ... args)->decltype(::fast_powf(std::forward < Args > (args)...)) {
return ::fast_powf(std::forward < Args > (args)...);
}
template < typename... Args >
auto fast_exp(Args && ... args)->decltype(::fast_exp(std::forward < Args > (args)...)) {
return ::fast_exp(std::forward < Args > (args)...);
}
template < typename... Args >
auto fast_expf(Args && ... args)->decltype(::fast_expf(std::forward < Args > (args)...)) {
return ::fast_expf(std::forward < Args > (args)...);
}
template < typename... Args >
auto fast_exp2(Args && ... args)->decltype(fast_exp2(std::forward < Args > (args)...)) {
return ::fast_exp2(std::forward < Args > (args)...);
}
template < typename... Args >
auto fast_exp2f(Args && ... args)->decltype(::fast_exp2f(std::forward < Args > (args)...)) {
return ::fast_exp2f(std::forward < Args > (args)...);
}
}
#endif
#ifdef CONFIG_FAST_EXP_OVERRIDE
#define pow fast_pow
#define powf fast_powf
#define exp fast_exp
#define expf fast_expf
#define exp2 fast_exp2
#define exp2f fast_exp2f
#endif
#endif