| 1 | // Leo is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 Leo <leo@nullptr.com.cn>. All rights reserved. |
| 4 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | // |
| 6 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 7 | // in compliance with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // https://opensource.org/licenses/BSD-3-Clause |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software distributed |
| 12 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | // specific language governing permissions and limitations under the License. |
| 15 | |
| 16 | #ifndef MIPS_USABILITY_H |
| 17 | #define MIPS_USABILITY_H |
| 18 | |
| 19 | #if __mips_msa |
| 20 | #include <msa.h> |
| 21 | #endif // __mips_msa |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | namespace ncnn { |
| 26 | |
| 27 | typedef union |
| 28 | { |
| 29 | int32_t i; |
| 30 | float f; |
| 31 | } FloatInt; |
| 32 | |
| 33 | } // namespace ncnn |
| 34 | |
| 35 | #if __mips_msa |
| 36 | /* declare some mips constants with union */ |
| 37 | #define _MIPS_FLOAT_CONST(Name, Val) \ |
| 38 | static const ncnn::FloatInt Name = {.f = Val} |
| 39 | |
| 40 | /* float type data load instructions */ |
| 41 | static NCNN_FORCEINLINE v4f32 __msa_fill_w_f32(float val) |
| 42 | { |
| 43 | ncnn::FloatInt fi_tmpval = {.f = val}; |
| 44 | return (v4f32)__msa_fill_w(fi_tmpval.i); |
| 45 | } |
| 46 | |
| 47 | static NCNN_FORCEINLINE float __msa_reduce_fadd_w(v4f32 _v) |
| 48 | { |
| 49 | // TODO find a more efficient way |
| 50 | return _v[0] + _v[1] + _v[2] + _v[3]; |
| 51 | } |
| 52 | |
| 53 | static NCNN_FORCEINLINE int __msa_reduce_add_w(v4i32 _v) |
| 54 | { |
| 55 | // TODO find a more efficient way |
| 56 | return _v[0] + _v[1] + _v[2] + _v[3]; |
| 57 | } |
| 58 | |
| 59 | static NCNN_FORCEINLINE int __msa_cfcmsa_msacsr() |
| 60 | { |
| 61 | int v; |
| 62 | asm volatile("cfcmsa %0, $1 \n" |
| 63 | : "=r"(v) |
| 64 | : |
| 65 | :); |
| 66 | return v; |
| 67 | } |
| 68 | |
| 69 | static NCNN_FORCEINLINE void __msa_ctcmsa_msacsr(int v) |
| 70 | { |
| 71 | asm volatile("ctcmsa $1, %0 \n" |
| 72 | : |
| 73 | : "r"(v) |
| 74 | :); |
| 75 | } |
| 76 | #endif // __mips_msa |
| 77 | |
| 78 | static NCNN_FORCEINLINE signed char float2int8(float v) |
| 79 | { |
| 80 | int int32 = round(v); |
| 81 | if (int32 > 127) return 127; |
| 82 | if (int32 < -127) return -127; |
| 83 | return (signed char)int32; |
| 84 | } |
| 85 | |
| 86 | #if __mips_msa |
| 87 | static NCNN_FORCEINLINE v16i8 float2int8(v4f32 _v) |
| 88 | { |
| 89 | // simulate round to nearest via +/-0.5 |
| 90 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 91 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 92 | |
| 93 | v16u8 _sign = __msa_and_v((v16u8)_v, _signmask); |
| 94 | v4f32 _p5s = (v4f32)__msa_or_v((v16u8)_p5, _sign); |
| 95 | v4f32 _v5 = __msa_fadd_w(_v, _p5s); |
| 96 | v4i32 _v32 = __msa_ftrunc_s_w(_v5); |
| 97 | |
| 98 | v8i16 _v32_16 = (v8i16)__msa_sat_s_w(_v32, 15); |
| 99 | v8i16 _v16 = __msa_pckev_h(_v32_16, _v32_16); |
| 100 | _v16 = __msa_max_s_h(_v16, __msa_fill_h(-127)); |
| 101 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 102 | v16i8 _v8 = __msa_pckev_b(_v16_8, _v16_8); |
| 103 | |
| 104 | return _v8; |
| 105 | } |
| 106 | |
| 107 | static NCNN_FORCEINLINE int64_t float2int8(v4f32 _vlow, v4f32 _vhigh) |
| 108 | { |
| 109 | // simulate round to nearest via +/-0.5 |
| 110 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 111 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 112 | |
| 113 | v16u8 _signlow = __msa_and_v((v16u8)_vlow, _signmask); |
| 114 | v16u8 _signhigh = __msa_and_v((v16u8)_vhigh, _signmask); |
| 115 | v4f32 _p5low = (v4f32)__msa_or_v((v16u8)_p5, _signlow); |
| 116 | v4f32 _p5high = (v4f32)__msa_or_v((v16u8)_p5, _signhigh); |
| 117 | v4f32 _vlow5 = __msa_fadd_w(_vlow, _p5low); |
| 118 | v4f32 _vhigh5 = __msa_fadd_w(_vhigh, _p5high); |
| 119 | v4i32 _vlow32 = __msa_ftrunc_s_w(_vlow5); |
| 120 | v4i32 _vhigh32 = __msa_ftrunc_s_w(_vhigh5); |
| 121 | |
| 122 | v8i16 _vlow32_16 = (v8i16)__msa_sat_s_w(_vlow32, 15); |
| 123 | v8i16 _vhigh32_16 = (v8i16)__msa_sat_s_w(_vhigh32, 15); |
| 124 | v8i16 _v16 = __msa_pckev_h(_vhigh32_16, _vlow32_16); |
| 125 | _v16 = __msa_max_s_h(_v16, __msa_fill_h(-127)); |
| 126 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 127 | v2i64 _v8 = (v2i64)__msa_pckev_b(_v16_8, _v16_8); |
| 128 | |
| 129 | return _v8[0]; |
| 130 | } |
| 131 | |
| 132 | static NCNN_FORCEINLINE v16i8 float2int8relu(v4f32 _v) |
| 133 | { |
| 134 | // simulate round to nearest via +/-0.5 |
| 135 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 136 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 137 | |
| 138 | v16u8 _sign = __msa_and_v((v16u8)_v, _signmask); |
| 139 | v4f32 _p5s = (v4f32)__msa_or_v((v16u8)_p5, _sign); |
| 140 | v4f32 _v5 = __msa_fadd_w(_v, _p5s); |
| 141 | v4i32 _v32 = __msa_ftrunc_s_w(_v5); |
| 142 | |
| 143 | v8i16 _v32_16 = (v8i16)__msa_sat_s_w(_v32, 15); |
| 144 | v8i16 _v16 = __msa_pckev_h(_v32_16, _v32_16); |
| 145 | _v16 = __msa_maxi_s_h(_v16, 0); |
| 146 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 147 | v16i8 _v8 = __msa_pckev_b(_v16_8, _v16_8); |
| 148 | |
| 149 | return _v8; |
| 150 | } |
| 151 | |
| 152 | static NCNN_FORCEINLINE int64_t float2int8relu(v4f32 _vlow, v4f32 _vhigh) |
| 153 | { |
| 154 | // simulate round to nearest via +/-0.5 |
| 155 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 156 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 157 | |
| 158 | v16u8 _signlow = __msa_and_v((v16u8)_vlow, _signmask); |
| 159 | v16u8 _signhigh = __msa_and_v((v16u8)_vhigh, _signmask); |
| 160 | v4f32 _p5low = (v4f32)__msa_or_v((v16u8)_p5, _signlow); |
| 161 | v4f32 _p5high = (v4f32)__msa_or_v((v16u8)_p5, _signhigh); |
| 162 | v4f32 _vlow5 = __msa_fadd_w(_vlow, _p5low); |
| 163 | v4f32 _vhigh5 = __msa_fadd_w(_vhigh, _p5high); |
| 164 | v4i32 _vlow32 = __msa_ftrunc_s_w(_vlow5); |
| 165 | v4i32 _vhigh32 = __msa_ftrunc_s_w(_vhigh5); |
| 166 | |
| 167 | v8i16 _vlow32_16 = (v8i16)__msa_sat_s_w(_vlow32, 15); |
| 168 | v8i16 _vhigh32_16 = (v8i16)__msa_sat_s_w(_vhigh32, 15); |
| 169 | v8i16 _v16 = __msa_pckev_h(_vhigh32_16, _vlow32_16); |
| 170 | _v16 = __msa_maxi_s_h(_v16, 0); |
| 171 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 172 | v2i64 _v8 = (v2i64)__msa_pckev_b(_v16_8, _v16_8); |
| 173 | |
| 174 | return _v8[0]; |
| 175 | } |
| 176 | |
| 177 | static NCNN_FORCEINLINE v16i8 float2int8leakyrelu(v4f32 _v, v4f32 _slope) |
| 178 | { |
| 179 | v4f32 _v_leaky = __msa_fmul_w(_v, _slope); |
| 180 | |
| 181 | // simulate round to nearest via +/-0.5 |
| 182 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 183 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 184 | |
| 185 | v16u8 _sign = __msa_and_v((v16u8)_v, _signmask); |
| 186 | v4f32 _p5s = (v4f32)__msa_or_v((v16u8)_p5, _sign); |
| 187 | v4f32 _v5 = __msa_fadd_w(_v, _p5s); |
| 188 | v4i32 _v32 = __msa_ftrunc_s_w(_v5); |
| 189 | |
| 190 | v16u8 _sign_leaky = __msa_and_v((v16u8)_v_leaky, _signmask); |
| 191 | v4f32 _p5_leaky = (v4f32)__msa_or_v((v16u8)_p5, _sign_leaky); |
| 192 | v4f32 _v5_leaky = __msa_fadd_w(_v_leaky, _p5_leaky); |
| 193 | v4i32 _v32_leaky = __msa_ftrunc_s_w(_v5_leaky); |
| 194 | |
| 195 | v8i16 _v32_16 = (v8i16)__msa_sat_s_w(_v32, 15); |
| 196 | v8i16 _v16 = __msa_pckev_h(_v32_16, _v32_16); |
| 197 | |
| 198 | v8i16 _v32_16_leaky = (v8i16)__msa_sat_s_w(_v32_leaky, 15); |
| 199 | v8i16 _v16_leaky = __msa_pckev_h(_v32_16_leaky, _v32_16_leaky); |
| 200 | |
| 201 | _v16 = __msa_max_s_h(_v16, _v16_leaky); |
| 202 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 203 | v16i8 _v8 = __msa_pckev_b(_v16_8, _v16_8); |
| 204 | |
| 205 | return _v8; |
| 206 | } |
| 207 | |
| 208 | static NCNN_FORCEINLINE int64_t float2int8leakyrelu(v4f32 _vlow, v4f32 _vhigh, v4f32 _slope) |
| 209 | { |
| 210 | v4f32 _vlow_leaky = __msa_fmul_w(_vlow, _slope); |
| 211 | v4f32 _vhigh_leaky = __msa_fmul_w(_vhigh, _slope); |
| 212 | |
| 213 | // simulate round to nearest via +/-0.5 |
| 214 | v4f32 _p5 = (v4f32)__msa_fill_w_f32(0.5f); |
| 215 | v16u8 _signmask = (v16u8)__msa_fill_w(1 << 31); |
| 216 | |
| 217 | v16u8 _signlow = __msa_and_v((v16u8)_vlow, _signmask); |
| 218 | v16u8 _signhigh = __msa_and_v((v16u8)_vhigh, _signmask); |
| 219 | v4f32 _p5low = (v4f32)__msa_or_v((v16u8)_p5, _signlow); |
| 220 | v4f32 _p5high = (v4f32)__msa_or_v((v16u8)_p5, _signhigh); |
| 221 | v4f32 _vlow5 = __msa_fadd_w(_vlow, _p5low); |
| 222 | v4f32 _vhigh5 = __msa_fadd_w(_vhigh, _p5high); |
| 223 | v4i32 _vlow32 = __msa_ftrunc_s_w(_vlow5); |
| 224 | v4i32 _vhigh32 = __msa_ftrunc_s_w(_vhigh5); |
| 225 | |
| 226 | v16u8 _signlow_leaky = __msa_and_v((v16u8)_vlow_leaky, _signmask); |
| 227 | v16u8 _signhigh_leaky = __msa_and_v((v16u8)_vhigh_leaky, _signmask); |
| 228 | v4f32 _p5low_leaky = (v4f32)__msa_or_v((v16u8)_p5, _signlow_leaky); |
| 229 | v4f32 _p5high_leaky = (v4f32)__msa_or_v((v16u8)_p5, _signhigh_leaky); |
| 230 | v4f32 _vlow5_leaky = __msa_fadd_w(_vlow_leaky, _p5low_leaky); |
| 231 | v4f32 _vhigh5_leaky = __msa_fadd_w(_vhigh_leaky, _p5high_leaky); |
| 232 | v4i32 _vlow32_leaky = __msa_ftrunc_s_w(_vlow5_leaky); |
| 233 | v4i32 _vhigh32_leaky = __msa_ftrunc_s_w(_vhigh5_leaky); |
| 234 | |
| 235 | v8i16 _vlow32_16 = (v8i16)__msa_sat_s_w(_vlow32, 15); |
| 236 | v8i16 _vhigh32_16 = (v8i16)__msa_sat_s_w(_vhigh32, 15); |
| 237 | v8i16 _v16 = __msa_pckev_h(_vhigh32_16, _vlow32_16); |
| 238 | |
| 239 | v8i16 _vlow32_16_leaky = (v8i16)__msa_sat_s_w(_vlow32_leaky, 15); |
| 240 | v8i16 _vhigh32_16_leaky = (v8i16)__msa_sat_s_w(_vhigh32_leaky, 15); |
| 241 | v8i16 _v16_leaky = __msa_pckev_h(_vhigh32_16_leaky, _vlow32_16_leaky); |
| 242 | |
| 243 | _v16 = __msa_max_s_h(_v16, _v16_leaky); |
| 244 | v16i8 _v16_8 = (v16i8)__msa_sat_s_h(_v16, 7); |
| 245 | v2i64 _v8 = (v2i64)__msa_pckev_b(_v16_8, _v16_8); |
| 246 | |
| 247 | return _v8[0]; |
| 248 | } |
| 249 | #endif // __mips_msa |
| 250 | |
| 251 | #endif // MIPS_USABILITY_H |