1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_NNACL_QUANTIZATION_FIXED_POINT_H_ 18 #define MINDSPORE_NNACL_QUANTIZATION_FIXED_POINT_H_ 19 20 #include <stdint.h> 21 #include <limits.h> 22 #ifdef ENABLE_NEON 23 #include <arm_neon.h> 24 #endif 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 // returns the high-32 bits of a * b with rounding 31 // assume that a and b is divided by 2^31, who fall into [-1, 1] 32 // so the mantissa of a * b is (a / 2^31) * (b / 2^31) * 2^31= (a * b) / 2^31 33 // actually we compute 2 * a * b / 2^32 34 // and take 32 bits of mantissa for rounding 35 int SaturatingRoundingDoublingHighMul(int a, int b); 36 37 int16_t SaturatingRoundingDoublingHighMulInt16(int16_t a, int16_t b); 38 39 // division by a 2^exponent with rounding 40 // or arithmetic right shift with rounding 41 int RoundingDivideByPOT(int x, int exponent); 42 43 int UpwardRounding(int x, int exponent); 44 45 int MultiplyByQuantizedMultiplier(int32_t value, int32_t multiplier, int32_t left_shift, int32_t right_shift); 46 47 int MultiplyByQuantizedMultiplierWithUpwardRounding(int32_t value, int32_t multiplier, int32_t left_shift, 48 int32_t right_shift); 49 50 int MultiplyByMultiplierAndRightShift(int32_t value, int32_t multiplier, int32_t right_shift); 51 52 int SaturatingRoundingMultiplyByPOT(int32_t x, int exponent); 53 54 int32_t Rescale(int x, int kIntegerBitsSrc, int kIntegerBitsDst); 55 56 int CountLeadingSignBits(int32_t x); 57 58 int32_t ComputerReciprocal(int32_t x, int x_digits, int *recip_shift); 59 60 int exp_on_negative_values(int a, const int tIntegerBits); 61 62 void GetSqrtQuantMultiplierExp(int32_t input, int reverse_shift, int32_t *multiplier, int32_t *shift); 63 64 #ifdef __cplusplus 65 } 66 #endif 67 68 #ifdef ENABLE_NEON 69 int32x4_t RoundingDivideByPOTInt32x4(int32x4_t x, int exponent); 70 71 int32x4_t SaturatingRoundingDoublingHighMulInt32x4(int32x4_t a, int32x4_t b); 72 #endif 73 74 #endif // MINDSPORE_NNACL_QUANTIZATION_FIXED_POINT_H_ 75