* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#if !defined(__ASCENDC_INCLUDE_INTERNAL_HEADERS__)
#pragma message( \
"impl/adv_api/detail/math/asin/asin_common_impl.h is an internal header file and must not be used directly. Functions or variables defined in this file may be removed in the future. Please use \"#include \"adv_api/math/asin.h\"\" and use public functions or variables defined in interface headers files.")
#define __ASCENDC_INCLUDE_INTERNAL_HEADERS__
#define __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_MATH_ASIN_ASIN_COMMON_IMPL_H__
#endif
#ifndef IMPL_MATH_ASIN_ASIN_COMMON_IMPL_H
#define IMPL_MATH_ASIN_ASIN_COMMON_IMPL_H
#include "kernel_tensor.h"
#include "kernel_basic_intf.h"
#include "../math_constant_util.h"
#include "../../common/check.h"
#ifdef ASCENDC_CPU_DEBUG
#include "../../api_check/kernel_check/math/asin/asin_check.h"
#endif
#include "../../api_check/kernel_api_check.h"
namespace AscendC {
template <typename T>
__aicore__ inline void GetSign(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& denominator)
{
UnaryRepeatParams unaryParams;
BinaryRepeatParams binaryParams;
constexpr float FP16_MAX = 32768;
constexpr float FP16_MIN = 3.0517578125e-05;
constexpr float FP32_MAX = 4611686018427387904;
constexpr float FP32_MIN = 2.168404344971009e-19;
constexpr float kFpMax = sizeof(T) == sizeof(float) ? FP32_MAX : FP16_MAX;
constexpr float kFpMin = sizeof(T) == sizeof(float) ? FP32_MIN : FP16_MIN;
Muls<T, false>(dst, src, static_cast<T>(kFpMax), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Abs<T, false>(denominator, dst, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<T, false>(denominator, denominator, static_cast<T>(kFpMin), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Div<T, false>(dst, dst, denominator, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
template <typename T>
__aicore__ inline void AsinTaylorCompute(
const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& localTemp)
{
BinaryRepeatParams binaryParams;
UnaryRepeatParams unaryParams;
Mul<T, false>(dst, src, src, MASK_PLACEHOLDER, 1, binaryParams);
Mul<T, false>(localTemp, src, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Muls<T, false>(dst, dst, static_cast<T>(kCOEF[ASIN_TAYLOR_EXPAND_COUNT]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
for (uint32_t i = ASIN_TAYLOR_EXPAND_COUNT - 1; i > 0; i--) {
Adds<T, false>(dst, dst, static_cast<T>(kCOEF[i]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, localTemp, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
Adds<T, false>(dst, dst, static_cast<T>(kCOEF[0]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
template <typename T>
__aicore__ inline void AsinTaylorComputeBySquareValue(const LocalTensor<T>& dst, const LocalTensor<T>& src)
{
BinaryRepeatParams binaryParams;
UnaryRepeatParams unaryParams;
Muls<T, false>(dst, src, static_cast<T>(NUM_ONE), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Muls<T, false>(dst, dst, static_cast<T>(kCOEF[ASIN_TAYLOR_EXPAND_COUNT]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
for (uint32_t i = ASIN_TAYLOR_EXPAND_COUNT - 1; i > 0; i--) {
Adds<T, false>(dst, dst, static_cast<T>(kCOEF[i]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
Adds<T, false>(dst, dst, static_cast<T>(kCOEF[0]), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Sqrt<T, false>(src, src, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
__aicore__ inline void AsinFp16Compute(
const LocalTensor<half>& dst, const LocalTensor<half>& src, const LocalTensor<half>& stackBuffer, uint32_t calSize)
{
UnaryRepeatParams unaryParams;
BinaryRepeatParams binaryParams;
const LocalTensor<float>& tmpFloatBuffer1 = stackBuffer.ReinterpretCast<float>();
const LocalTensor<float>& tmpFloatBuffer2 = tmpFloatBuffer1[calSize];
const uint32_t tmpHalfBuffer1Offset = calSize * 4;
const uint32_t tmpHalfBuffer2Offset = calSize * 5;
const LocalTensor<half>& tmpHalfBuffer1 = stackBuffer[tmpHalfBuffer1Offset];
const LocalTensor<half>& tmpHalfBuffer2 = stackBuffer[tmpHalfBuffer2Offset];
Cast<float, half, false>(
tmpFloatBuffer2, src, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, DEFAULT_REPEAT_STRIDE, HALF_DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Mul<float, false>(tmpFloatBuffer2, tmpFloatBuffer2, tmpFloatBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Muls<float, false>(tmpFloatBuffer2, tmpFloatBuffer2, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<float, false>(tmpFloatBuffer2, tmpFloatBuffer2, NUM_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
AsinTaylorComputeBySquareValue(tmpFloatBuffer1, tmpFloatBuffer2);
PipeBarrier<PIPE_V>();
Muls<float, false>(tmpFloatBuffer1, tmpFloatBuffer1, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<float, false>(tmpFloatBuffer1, tmpFloatBuffer1, HALF_PI, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Abs<half, false>(tmpHalfBuffer2, src, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
AsinTaylorCompute(dst, tmpHalfBuffer2, tmpHalfBuffer1);
PipeBarrier<PIPE_V>();
Mins<half, false>(tmpHalfBuffer2, tmpHalfBuffer2, static_cast<half>(BOUNDARY), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<half, false>(tmpHalfBuffer2, tmpHalfBuffer2, static_cast<half>(-BOUNDARY), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
const LocalTensor<int8_t>& tmpS8Buffer = tmpHalfBuffer1.ReinterpretCast<int8_t>();
Cast<int8_t, half, false>(
tmpS8Buffer, tmpHalfBuffer2, RoundMode::CAST_FLOOR, MASK_PLACEHOLDER, 1,
{1, 1, HALF_DEFAULT_REPEAT_STRIDE, DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Cast<half, int8_t, false>(
tmpHalfBuffer2, tmpS8Buffer, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, DEFAULT_REPEAT_STRIDE, HALF_DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Muls<half, false>(tmpHalfBuffer2, tmpHalfBuffer2, static_cast<half>(NEG_ONE), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<half, false>(dst, dst, tmpHalfBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Muls<half, false>(tmpHalfBuffer2, tmpHalfBuffer2, static_cast<half>(NEG_ONE), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<half, false>(tmpHalfBuffer2, tmpHalfBuffer2, static_cast<half>(NUM_ONE), MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Cast<float, half, false>(
tmpFloatBuffer2, tmpHalfBuffer2, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, DEFAULT_REPEAT_STRIDE, HALF_DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Mul<float, false>(tmpFloatBuffer1, tmpFloatBuffer1, tmpFloatBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Cast<float, half, false>(
tmpFloatBuffer2, dst, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, DEFAULT_REPEAT_STRIDE, HALF_DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Add<float, false>(tmpFloatBuffer1, tmpFloatBuffer2, tmpFloatBuffer1, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
GetSign(tmpHalfBuffer1, src, tmpHalfBuffer2);
PipeBarrier<PIPE_V>();
Cast<float, half, false>(
tmpFloatBuffer2, tmpHalfBuffer1, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, DEFAULT_REPEAT_STRIDE, HALF_DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
Mul<float, false>(tmpFloatBuffer1, tmpFloatBuffer1, tmpFloatBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Cast<half, float, false>(
dst, tmpFloatBuffer1, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1,
{1, 1, HALF_DEFAULT_REPEAT_STRIDE, DEFAULT_REPEAT_STRIDE});
PipeBarrier<PIPE_V>();
}
template <typename T>
__aicore__ inline void AsinCompute(
const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& tmpBuffer, uint32_t calSize)
{
UnaryRepeatParams unaryParams;
BinaryRepeatParams binaryParams;
const LocalTensor<T>& tmpBuffer2 = tmpBuffer[calSize];
const LocalTensor<T>& dupBuffer = tmpBuffer[calSize * 2];
Mul<T, false>(tmpBuffer2, src, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Muls<T, false>(tmpBuffer2, tmpBuffer2, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<T, false>(tmpBuffer2, tmpBuffer2, NUM_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Sqrt<T, false>(dst, tmpBuffer2, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
AsinTaylorCompute(tmpBuffer2, dst, tmpBuffer);
PipeBarrier<PIPE_V>();
Muls<T, false>(tmpBuffer2, tmpBuffer2, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<T, false>(tmpBuffer2, tmpBuffer2, HALF_PI, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(tmpBuffer, src, src, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
AsinTaylorComputeBySquareValue(dst, tmpBuffer);
PipeBarrier<PIPE_V>();
Mins<T, false>(tmpBuffer, tmpBuffer, BOUNDARY, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<T, false>(tmpBuffer, tmpBuffer, -BOUNDARY, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
LocalTensor<int32_t> tmpS32Buffer = tmpBuffer.template ReinterpretCast<int32_t>();
Cast<int32_t, T, false>(tmpS32Buffer, tmpBuffer, RoundMode::CAST_FLOOR, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Cast<T, int32_t, false>(tmpBuffer, tmpS32Buffer, RoundMode::CAST_NONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
PipeBarrier<PIPE_V>();
Muls<T, false>(tmpBuffer, tmpBuffer, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, tmpBuffer, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Muls<T, false>(tmpBuffer, tmpBuffer, NEG_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Adds<T, false>(tmpBuffer, tmpBuffer, NUM_ONE, MASK_PLACEHOLDER, 1, unaryParams);
PipeBarrier<PIPE_V>();
Mul<T, false>(tmpBuffer2, tmpBuffer2, tmpBuffer, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
Add<T, false>(dst, dst, tmpBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
GetSign(tmpBuffer2, src, tmpBuffer);
PipeBarrier<PIPE_V>();
Mul<T, false>(dst, dst, tmpBuffer2, MASK_PLACEHOLDER, 1, binaryParams);
PipeBarrier<PIPE_V>();
}
template <>
__aicore__ inline void AsinCompute<half>(
const LocalTensor<half>& dst, const LocalTensor<half>& src, const LocalTensor<half>& tmpBuffer, uint32_t calSize)
{
AsinFp16Compute(dst, src, tmpBuffer, calSize);
}
template <typename T, bool isReuseSource = false>
__aicore__ inline void AsinImpl(
const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer,
const uint32_t calCount)
{
if ASCEND_IS_AIC {
return;
}
CHECK_FUNC_HIGHLEVEL_API(Asin, (T, isReuseSource), (dstTensor, srcTensor, sharedTmpBuffer, calCount));
uint32_t bufferSize = sharedTmpBuffer.GetSize();
uint32_t tmpBufferSize = bufferSize / sizeof(T);
CheckTmpBufferSize(tmpBufferSize, 0, bufferSize);
LocalTensor<T> tmpBuffer = sharedTmpBuffer.ReinterpretCast<T>();
uint32_t calSize = 0;
if constexpr (sizeof(T) == sizeof(float)) {
calSize = tmpBufferSize / ASIN_FLOAT_CALC_PROCEDURE / ONE_BLK_SIZE * ONE_BLK_SIZE;
} else {
calSize = tmpBufferSize / ASIN_HALF_CALC_PROCEDURE / ONE_BLK_SIZE * ONE_BLK_SIZE;
}
CheckTmpBufferSize(calSize, 0, bufferSize);
const uint32_t round = calCount / calSize;
const uint32_t tail = calCount % calSize;
SetMaskCount();
SetVectorMask<T, MaskMode::COUNTER>(0, calSize);
uint32_t offset = 0;
for (uint32_t i = 0; i < round; i++) {
AsinCompute(dstTensor[offset], srcTensor[offset], tmpBuffer, calSize);
offset = offset + calSize;
}
if (tail != 0) {
SetVectorMask<T, MaskMode::COUNTER>(0, tail);
AsinCompute(dstTensor[offset], srcTensor[offset], tmpBuffer, calSize);
}
SetMaskNorm();
ResetMask();
}
template <typename T, bool isReuseSource = false>
__aicore__ inline void AsinImpl(
const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const uint32_t calCount)
{
if ASCEND_IS_AIC {
return;
}
LocalTensor<uint8_t> sharedTmpBuffer;
bool ans = PopStackBuffer<uint8_t, TPosition::LCM>(sharedTmpBuffer);
ASCENDC_ASSERT((ans), { KERNEL_LOG(KERNEL_ERROR, "PopStackBuffer Error!"); });
AsinImpl<T, isReuseSource>(dstTensor, srcTensor, sharedTmpBuffer, calCount);
}
}
#endif
#if defined(__UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_MATH_ASIN_ASIN_COMMON_IMPL_H__)
#undef __ASCENDC_INCLUDE_INTERNAL_HEADERS__
#undef __UNDEF_ASCENDC_INCLUDE_INTERNAL_HEADERS_MATH_ASIN_ASIN_COMMON_IMPL_H__
#endif