1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-vcvt/scalar-fabsf.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11 #include <math.h>
12
13 #include <xnnpack/common.h>
14 #include <xnnpack/math.h>
15 #include <xnnpack/vcvt.h>
16
17 #include <fp16.h>
18
19
xnn_f32_f16_vcvt_ukernel__scalar_fabsf_x3(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_f32_f16_vcvt_ukernel__scalar_fabsf_x3(
21 size_t n,
22 const float* input,
23 void* output,
24 const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
25 {
26 assert(n != 0);
27 assert(n % sizeof(float) == 0);
28 assert(input != NULL);
29 assert(output != NULL);
30
31 const float vscale_to_inf = params->scalar_fabsf.scale_to_inf;
32 const uint32_t vexp_bias = params->scalar_fabsf.exp_bias;
33 const float vscale_to_zero = params->scalar_fabsf.scale_to_zero;
34 const uint32_t vexpw_max = params->scalar_fabsf.expw_max;
35 const uint32_t vbias_min = params->scalar_fabsf.bias_min;
36 const uint16_t vexph_mask = params->scalar_fabsf.exph_mask;
37 const uint16_t vmanth_mask = params->scalar_fabsf.manth_mask;
38 const uint16_t vnanh = params->scalar_fabsf.nanh;
39
40 uint16_t* o = (uint16_t*) output;
41 for (; n >= 3 * sizeof(float); n -= 3 * sizeof(float)) {
42 const float vx0 = input[0];
43 const float vx1 = input[1];
44 const float vx2 = input[2];
45 input += 3;
46
47 const float vabsx0 = fabsf(vx0);
48 const float vabsx1 = fabsf(vx1);
49 const float vabsx2 = fabsf(vx2);
50 uint32_t vsignw0 = fp32_to_bits(vx0);
51 uint32_t vsignw1 = fp32_to_bits(vx1);
52 uint32_t vsignw2 = fp32_to_bits(vx2);
53
54 const uint32_t vnonsignw0 = fp32_to_bits(vabsx0);
55 const uint32_t vnonsignw1 = fp32_to_bits(vabsx1);
56 const uint32_t vnonsignw2 = fp32_to_bits(vabsx2);
57 float vf0 = vabsx0 * vscale_to_inf;
58 float vf1 = vabsx1 * vscale_to_inf;
59 float vf2 = vabsx2 * vscale_to_inf;
60
61 uint32_t vbias0 = vnonsignw0 + vexp_bias;
62 uint32_t vbias1 = vnonsignw1 + vexp_bias;
63 uint32_t vbias2 = vnonsignw2 + vexp_bias;
64 vsignw0 ^= vnonsignw0;
65 vsignw1 ^= vnonsignw1;
66 vsignw2 ^= vnonsignw2;
67
68 vf0 *= vscale_to_zero;
69 vf1 *= vscale_to_zero;
70 vf2 *= vscale_to_zero;
71 vbias0 &= vexpw_max;
72 vbias1 &= vexpw_max;
73 vbias2 &= vexpw_max;
74
75 vbias0 = math_max_u32(vbias0, vbias_min);
76 vbias1 = math_max_u32(vbias1, vbias_min);
77 vbias2 = math_max_u32(vbias2, vbias_min);
78
79 vf0 += fp32_from_bits(vbias0);
80 vf1 += fp32_from_bits(vbias1);
81 vf2 += fp32_from_bits(vbias2);
82
83 const uint32_t vbits0 = fp32_to_bits(vf0);
84 const uint32_t vbits1 = fp32_to_bits(vf1);
85 const uint32_t vbits2 = fp32_to_bits(vf2);
86
87 const uint16_t vexph0 = (uint16_t) (vbits0 >> 13) & vexph_mask;
88 const uint16_t vexph1 = (uint16_t) (vbits1 >> 13) & vexph_mask;
89 const uint16_t vexph2 = (uint16_t) (vbits2 >> 13) & vexph_mask;
90 const uint16_t vmanth0 = (uint16_t) vbits0 & vmanth_mask;
91 const uint16_t vmanth1 = (uint16_t) vbits1 & vmanth_mask;
92 const uint16_t vmanth2 = (uint16_t) vbits2 & vmanth_mask;
93 const uint16_t vsignh0 = (uint16_t) (vsignw0 >> 16);
94 const uint16_t vsignh1 = (uint16_t) (vsignw1 >> 16);
95 const uint16_t vsignh2 = (uint16_t) (vsignw2 >> 16);
96
97 uint16_t vh0 = vexph0 + vmanth0;
98 uint16_t vh1 = vexph1 + vmanth1;
99 uint16_t vh2 = vexph2 + vmanth2;
100 if XNN_UNPREDICTABLE(vnonsignw0 > vexpw_max) {
101 vh0 = vnanh;
102 }
103 if XNN_UNPREDICTABLE(vnonsignw1 > vexpw_max) {
104 vh1 = vnanh;
105 }
106 if XNN_UNPREDICTABLE(vnonsignw2 > vexpw_max) {
107 vh2 = vnanh;
108 }
109 vh0 |= vsignh0;
110 vh1 |= vsignh1;
111 vh2 |= vsignh2;
112
113 o[0] = vh0;
114 o[1] = vh1;
115 o[2] = vh2;
116 o += 3;
117 }
118 if XNN_UNLIKELY(n != 0) {
119 do {
120 const float vx = *input++;
121
122 const float vabsx = fabsf(vx);
123 uint32_t vsignw = fp32_to_bits(vx);
124
125 const uint32_t vnonsignw = fp32_to_bits(vabsx);
126 float vf = vabsx * vscale_to_inf;
127
128 uint32_t vbias = vnonsignw + vexp_bias;
129 vsignw ^= vnonsignw;
130
131 vf *= vscale_to_zero;
132 vbias &= vexpw_max;
133
134 vbias = math_max_u32(vbias, vbias_min);
135
136 vf += fp32_from_bits(vbias);
137
138 const uint32_t vbits = fp32_to_bits(vf);
139
140 const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
141 const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
142 const uint16_t vsignh = (uint16_t) (vsignw >> 16);
143
144 uint16_t vh = vexph + vmanth;
145 if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
146 vh = vnanh;
147 }
148 vh |= vsignh;
149
150 *o++ = vh;
151
152 n -= sizeof(float);
153 } while (n != 0);
154 }
155 }
156