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_clamp(xnn_subgraph_t subgraph,float output_min,float output_max,uint32_t input_id,uint32_t output_id,uint32_t flags)16 enum xnn_status xnn_define_clamp(
17 xnn_subgraph_t subgraph,
18 float output_min,
19 float output_max,
20 uint32_t input_id,
21 uint32_t output_id,
22 uint32_t flags)
23 {
24 if ((xnn_params.init_flags & XNN_INIT_FLAG_XNNPACK) == 0) {
25 xnn_log_error("failed to define %s operator: XNNPACK is not initialized",
26 xnn_node_type_to_string(xnn_node_type_clamp));
27 return xnn_status_uninitialized;
28 }
29
30 if (input_id >= subgraph->num_values) {
31 xnn_log_error(
32 "failed to define %s operator with input ID #%" PRIu32 ": invalid Value ID",
33 xnn_node_type_to_string(xnn_node_type_clamp), input_id);
34 return xnn_status_invalid_parameter;
35 }
36
37 if (output_id >= subgraph->num_values) {
38 xnn_log_error(
39 "failed to define %s operator with output ID #%" PRIu32 ": invalid Value ID",
40 xnn_node_type_to_string(xnn_node_type_clamp), output_id);
41 return xnn_status_invalid_parameter;
42 }
43
44 struct xnn_node* node = xnn_subgraph_new_node(subgraph);
45 if (node == NULL) {
46 return xnn_status_out_of_memory;
47 }
48
49 node->type = xnn_node_type_clamp;
50 node->activation.output_min = output_min;
51 node->activation.output_max = output_max;
52 node->num_inputs = 1;
53 node->inputs[0] = input_id;
54 node->num_outputs = 1;
55 node->outputs[0] = output_id;
56 node->flags = flags;
57
58 return xnn_status_success;
59 }
60