Coverage Report

Created: 2026-07-12 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Simd/src/Simd/SimdBaseSynetSoftmax32f.cpp
Line
Count
Source
1
/*
2
* Simd Library (http://ermig1979.github.io/Simd).
3
*
4
* Copyright (c) 2011-2026 Yermalayeu Ihar.
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to deal
8
* in the Software without restriction, including without limitation the rights
9
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* SOFTWARE.
23
*/
24
#include "Simd/SimdArray.h"
25
#include "Simd/SimdPow.h"
26
#include "Simd/SimdSynet.h"
27
#include "Simd/SimdAlignment.h"
28
#include "Simd/SimdExp.h"
29
30
namespace Simd
31
{
32
#if defined(SIMD_SYNET_ENABLE)
33
    namespace Base
34
    {
35
        void SynetSoftmax32f(const float * src, size_t outer, size_t count, size_t inner, float * dst)
36
0
        {
37
0
            if (inner == 1)
38
0
            {
39
0
                if (count == 2)
40
0
                {
41
0
                    for (size_t o = 0; o < outer; ++o)
42
0
                    {
43
0
                        float max = Simd::Max(src[0], src[1]);
44
0
                        float exp0 = ::exp(src[0] - max);
45
0
                        float exp1 = ::exp(src[1] - max);
46
0
                        float sum = exp0 + exp1;
47
0
                        dst[0] = exp0 / sum;
48
0
                        dst[1] = exp1 / sum;
49
0
                        src += 2;
50
0
                        dst += 2;
51
0
                    }
52
0
                }
53
0
                else if (count == 3)
54
0
                {
55
0
                    for (size_t o = 0; o < outer; ++o)
56
0
                    {
57
0
                        float max = Simd::Max(Simd::Max(src[0], src[1]), src[2]);
58
0
                        float exp0 = ::exp(src[0] - max);
59
0
                        float exp1 = ::exp(src[1] - max);
60
0
                        float exp2 = ::exp(src[2] - max);
61
0
                        float sum = exp0 + exp1 + exp2;
62
0
                        dst[0] = exp0 / sum;
63
0
                        dst[1] = exp1 / sum;
64
0
                        dst[2] = exp2 / sum;
65
0
                        src += 3;
66
0
                        dst += 3;
67
0
                    }
68
0
                }
69
0
                else
70
0
                {
71
0
                    for (size_t o = 0; o < outer; ++o)
72
0
                    {
73
0
                        float max = src[0];
74
0
                        for (size_t c = 1; c < count; ++c)
75
0
                            max = Simd::Max(max, src[c]);
76
0
                        float sum = 0;
77
0
                        for (size_t c = 0; c < count; ++c)
78
0
                        {
79
0
                            dst[c] = ::exp(src[c] - max);
80
0
                            sum += dst[c];                            
81
0
                        }
82
0
                        float k = 1.0f / sum;
83
0
                        for (size_t c = 0; c < count; ++c)
84
0
                            dst[c] *= k;
85
0
                        src += count;
86
0
                        dst += count;
87
0
                    }
88
0
                }
89
0
            }
90
0
            else
91
0
            {
92
0
                Array32f tmp(inner * 2);
93
0
                const float * s;
94
0
                float * max = tmp.data, *sum = tmp.data + inner, *d;
95
0
                for (size_t o = 0; o < outer; ++o)
96
0
                {
97
0
                    for (size_t i = 0; i < inner; ++i)
98
0
                        max[i] = src[i];
99
0
                    s = src + inner;
100
0
                    for (size_t c = 1; c < count; ++c)
101
0
                    {
102
0
                        for (size_t i = 0; i < inner; ++i)
103
0
                            max[i] = Simd::Max(max[i], s[i]);
104
0
                        s += inner;
105
0
                    }
106
107
0
                    s = src;
108
0
                    d = dst;
109
0
                    for (size_t i = 0; i < inner; ++i)
110
0
                        sum[i] = 0;
111
0
                    for (size_t c = 0; c < count; ++c)
112
0
                    {
113
0
                        for (size_t i = 0; i < inner; ++i)
114
0
                        {
115
0
                            d[i] = ::exp(s[i] - max[i]);
116
0
                            sum[i] += d[i];
117
0
                        }
118
0
                        s += inner;
119
0
                        d += inner;
120
0
                    }
121
122
0
                    d = dst;
123
0
                    for (size_t c = 0; c < count; ++c)
124
0
                    {
125
0
                        for (size_t i = 0; i < inner; ++i)
126
0
                            d[i] /= sum[i];
127
0
                        d += inner;
128
0
                    }
129
0
                    src += count * inner;
130
0
                    dst += count * inner;
131
0
                }
132
0
            }
133
0
        }
134
    }
135
#endif
136
}