• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fp16.h>
17 
18 
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)])19 void xnn_f32_f16_vcvt_ukernel__scalar_bitcast_x1(
20     size_t n,
21     const float* input,
22     void* output,
23     const union xnn_f32_f16_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(input != NULL);
28   assert(output != NULL);
29 
30   const uint32_t vnonsign_mask = params->scalar_bitcast.nonsign_mask;
31   const uint32_t vexp_bias = params->scalar_bitcast.exp_bias;
32   const float vscale_to_inf = params->scalar_bitcast.scale_to_inf;
33   const uint32_t vexpw_max = params->scalar_bitcast.expw_max;
34   const float vscale_to_zero = params->scalar_bitcast.scale_to_zero;
35   const uint32_t vbias_min = params->scalar_bitcast.bias_min;
36   const uint16_t vexph_mask = params->scalar_bitcast.exph_mask;
37   const uint16_t vmanth_mask = params->scalar_bitcast.manth_mask;
38   const uint16_t vnanh = params->scalar_bitcast.nanh;
39 
40   const uint32_t* i = (const uint32_t*) input;
41   uint16_t* o = (uint16_t*) output;
42   do {
43     const uint32_t vw = *i++;
44 
45     const uint32_t vnonsignw = vw & vnonsign_mask;
46 
47     float vf = fp32_from_bits(vnonsignw);
48     const uint32_t vsignw = vw ^ vnonsignw;
49     uint32_t vbias = vnonsignw + vexp_bias;
50 
51     vf *= vscale_to_inf;
52     vbias &= vexpw_max;
53 
54     vf *= vscale_to_zero;
55     vbias = math_max_u32(vbias, vbias_min);
56 
57     vf += fp32_from_bits(vbias);
58 
59     const uint32_t vbits = fp32_to_bits(vf);
60 
61     const uint16_t vexph = (uint16_t) (vbits >> 13) & vexph_mask;
62     const uint16_t vmanth = (uint16_t) vbits & vmanth_mask;
63     const uint16_t vsignh = (uint16_t) (vsignw >> 16);
64 
65     uint16_t vh = vexph + vmanth;
66     if XNN_UNPREDICTABLE(vnonsignw > vexpw_max) {
67       vh = vnanh;
68     }
69     vh |= vsignh;
70 
71     *o++ = vh;
72 
73     n -= sizeof(float);
74   } while (n != 0);
75 }
76