xref: /aosp_15_r20/external/XNNPACK/src/f32-f16-vcvt/gen/vcvt-scalar-bitcast-x1.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-f16-vcvt/scalar-bitcast.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 
12 #include <xnnpack/common.h>
13 #include <xnnpack/math.h>
14 #include <xnnpack/vcvt.h>
15 
16 
xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x1(size_t n,const float * input,void * output,const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x1(
18     size_t n,
19     const float* input,
20     void* output,
21     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
22 {
23   assert(n != 0);
24   assert(n % sizeof(float) == 0);
25   assert(input != NULL);
26   assert(output != NULL);
27 
28   const uint32_t vnonsign_mask = params->scalar_bitcast.nonsign_mask;
29   const uint32_t vexp_bias = params->scalar_bitcast.exp_bias;
30   const float vscale_to_inf = params->scalar_bitcast.scale_to_inf;
31   const uint32_t vexpw_max = params->scalar_bitcast.expw_max;
32   const float vscale_to_zero = params->scalar_bitcast.scale_to_zero;
33   const uint32_t vbias_min = params->scalar_bitcast.bias_min;
34   const uint16_t vexph_mask = params->scalar_bitcast.exph_mask;
35   const uint16_t vmanth_mask = params->scalar_bitcast.manth_mask;
36   const uint16_t vnanh = params->scalar_bitcast.nanh;
37 
38   const uint32_t* i = (const uint32_t*) input;
39   uint16_t* o = (uint16_t*) output;
40   do {
41     const uint32_t vw = *i++;
42 
43     const uint32_t vnonsignw = vw & vnonsign_mask;
44 
45     float vf = uint32_as_float(vnonsignw);
46     const uint32_t vsignw = vw ^ vnonsignw;
47     uint32_t vbias = vnonsignw + vexp_bias;
48 
49     vf *= vscale_to_inf;
50     vbias &= vexpw_max;
51 
52     vf *= vscale_to_zero;
53     vbias = math_max_u32(vbias, vbias_min);
54 
55     vf += uint32_as_float(vbias);
56 
57     const uint32_t vbits = float_as_uint32(vf);
58 
59     const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
60     const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
61     const uint16_t vsignh = (uint16_t) (vsignw >> 16);
62 
63     uint16_t vh = vexph + vmanth;
64     if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
65       vh = vnanh;
66     }
67     vh |= vsignh;
68 
69     *o++ = vh;
70 
71     n -= sizeof(float);
72   } while (n != 0);
73 }
74