| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "cast_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __AVX__ |
| 20 | #include <immintrin.h> |
| 21 | #endif // __AVX__ |
| 22 | #endif // __SSE2__ |
| 23 | #include "x86_usability.h" |
| 24 | |
| 25 | #include "cpu.h" |
| 26 | |
| 27 | namespace ncnn { |
| 28 | |
| 29 | #include "cast_fp16.h" |
| 30 | #include "cast_bf16.h" |
| 31 | |
| 32 | Cast_x86::Cast_x86() |
| 33 | { |
| 34 | support_packing = true; |
| 35 | } |
| 36 | |
| 37 | int Cast_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 38 | { |
| 39 | if (type_from == type_to) |
| 40 | { |
| 41 | top_blob = bottom_blob; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int w = bottom_blob.w; |
| 46 | int h = bottom_blob.h; |
| 47 | int d = bottom_blob.d; |
| 48 | int channels = bottom_blob.c; |
| 49 | int dims = bottom_blob.dims; |
| 50 | size_t elemsize = bottom_blob.elemsize; |
| 51 | int elempack = bottom_blob.elempack; |
| 52 | |
| 53 | size_t out_elemsize = elemsize; |
| 54 | if (type_to == 1) |
| 55 | { |
| 56 | if (type_from == 3) |
| 57 | { |
| 58 | Cast::forward(bottom_blob, top_blob, opt); |
| 59 | } |
| 60 | |
| 61 | // float32 |
| 62 | out_elemsize = 4 * elempack; |
| 63 | } |
| 64 | else if (type_to == 2) |
| 65 | { |
| 66 | // float16 |
| 67 | out_elemsize = 2 * elempack; |
| 68 | } |
| 69 | else if (type_to == 3) |
| 70 | { |
| 71 | // int8 |
| 72 | out_elemsize = elempack; |
| 73 | } |
| 74 | else if (type_to == 4) |
| 75 | { |
| 76 | // bfloat16 |
| 77 | out_elemsize = 2 * elempack; |
| 78 | } |
| 79 | |
| 80 | if (dims == 1) |
| 81 | { |
| 82 | top_blob.create(w, out_elemsize, elempack, opt.blob_allocator); |
| 83 | } |
| 84 | else if (dims == 2) |
| 85 | { |
| 86 | top_blob.create(w, h, out_elemsize, elempack, opt.blob_allocator); |
| 87 | } |
| 88 | else if (dims == 3) |
| 89 | { |
| 90 | top_blob.create(w, h, channels, out_elemsize, elempack, opt.blob_allocator); |
| 91 | } |
| 92 | else if (dims == 4) |
| 93 | { |
| 94 | top_blob.create(w, h, d, channels, out_elemsize, elempack, opt.blob_allocator); |
| 95 | } |
| 96 | if (top_blob.empty()) |
| 97 | return -100; |
| 98 | |
| 99 | int size = w * h * d * elempack; |
| 100 | |
| 101 | if (type_from == 1 && type_to == 2) |
| 102 | { |
| 103 | cast_fp32_to_fp16_sse(bottom_blob, top_blob, opt); |
| 104 | } |
| 105 | |
| 106 | if (type_from == 2 && type_to == 1) |
| 107 | { |
| 108 | cast_fp16_to_fp32_sse(bottom_blob, top_blob, opt); |
| 109 | } |
| 110 | |
| 111 | if (type_from == 3 && type_to == 1) |
| 112 | { |
| 113 | #pragma omp parallel for num_threads(opt.num_threads) |
| 114 | for (int q = 0; q < channels; q++) |
| 115 | { |
| 116 | const signed char* ptr = bottom_blob.channel(q); |
| 117 | float* outptr = top_blob.channel(q); |
| 118 | |
| 119 | for (int i = 0; i < size; i++) |
| 120 | { |
| 121 | outptr[i] = (float)ptr[i]; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (type_from == 1 && type_to == 4) |
| 127 | { |
| 128 | cast_fp32_to_bf16_sse(bottom_blob, top_blob, opt); |
| 129 | } |
| 130 | |
| 131 | if (type_from == 4 && type_to == 1) |
| 132 | { |
| 133 | cast_bf16_to_fp32_sse(bottom_blob, top_blob, opt); |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | } // namespace ncnn |