• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <math.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <xnnpack.h>
11 #include <xnnpack/log.h>
12 #include <xnnpack/params.h>
13 #include <xnnpack/subgraph.h>
14 
15 
xnn_define_divide(xnn_subgraph_t subgraph,float output_min,float output_max,uint32_t input1_id,uint32_t input2_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_divide(
17   xnn_subgraph_t subgraph,
18   float output_min,
19   float output_max,
20   uint32_t input1_id,
21   uint32_t input2_id,
22   uint32_t output_id,
23   uint32_t flags)
24 {
25   if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
26     xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
27       xnn_node_type_to_string(xnn_node_type_divide));
28     return xnn_status_uninitialized;
29   }
30 
31   if (isnan(output_min)) {
32     xnn_log_error(
33       "failed to define %s operator with NaN output lower bound: lower bound must be non-NaN",
34       xnn_node_type_to_string(xnn_node_type_divide));
35     return xnn_status_invalid_parameter;
36   }
37 
38   if (isnan(output_max)) {
39     xnn_log_error(
40       "failed to define %s operator with NaN output upper bound: upper bound must be non-NaN",
41       xnn_node_type_to_string(xnn_node_type_divide));
42     return xnn_status_invalid_parameter;
43   }
44 
45   if (output_min >= output_max) {
46     xnn_log_error(
47       "failed to define %s operator with [%.7g, %.7g] output range: lower bound must be below upper bound",
48       xnn_node_type_to_string(xnn_node_type_divide), output_min, output_max);
49     return xnn_status_invalid_parameter;
50   }
51 
52   if (input1_id >= subgraph->num_values) {
53     xnn_log_error(
54       "failed to define %s operator with the first input ID #%" PRIu32 ": invalid Value ID",
55       xnn_node_type_to_string(xnn_node_type_divide), input1_id);
56     return xnn_status_invalid_parameter;
57   }
58 
59   if (input2_id >= subgraph->num_values) {
60     xnn_log_error(
61       "failed to define %s operator with the second input ID #%" PRIu32 ": invalid Value ID",
62       xnn_node_type_to_string(xnn_node_type_divide), input2_id);
63     return xnn_status_invalid_parameter;
64   }
65 
66   if (output_id >= subgraph->num_values) {
67     xnn_log_error(
68       "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
69       xnn_node_type_to_string(xnn_node_type_divide), output_id);
70     return xnn_status_invalid_parameter;
71   }
72 
73   struct xnn_node* node = xnn_subgraph_new_node(subgraph);
74   if (node == NULL) {
75     return xnn_status_out_of_memory;
76   }
77 
78   node->type = xnn_node_type_divide;
79   node->activation.output_min = output_min;
80   node->activation.output_max = output_max;
81   node->num_inputs = 2;
82   node->inputs[0] = input1_id;
83   node->inputs[1] = input2_id;
84   node->num_outputs = 1;
85   node->outputs[0] = output_id;
86   node->flags = flags;
87 
88   return xnn_status_success;
89 }
90