• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 <xnnpack/allocator.h>
7 #include <xnnpack/microparams.h>
8 #include <xnnpack/params.h>
9 #include <xnnpack/post-operation.h>
10 
allocate_and_initialize_post_operation_params(size_t num_post_operations,struct xnn_post_operation * post_operations)11 char* allocate_and_initialize_post_operation_params(
12     size_t num_post_operations,
13     struct xnn_post_operation* post_operations) {
14 
15   union {
16     union xnn_f32_hswish_params hswish_params;
17   } post_op_params;  // Anonymous union to hold params of all valid post operations.
18 
19   // Calculate how much space all post operation params will take.
20   size_t total_size = 0;
21   for (size_t i = 0; i < num_post_operations; i++) {
22     const struct xnn_post_operation post_op = post_operations[i];
23     switch (post_op.op_type) {
24       case xnn_post_operation_type_hardswish:
25         if (xnn_params.f32.hswish.init.f32_hswish != NULL) {
26           total_size += xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params);
27         }
28         break;
29       default:
30         XNN_UNREACHABLE;
31     }
32   }
33   // Copy all params compactly into post_operation_params.
34   char* post_operation_params = xnn_allocate_zero_memory(total_size);
35   char* cur_params = post_operation_params;
36   for (size_t i = 0; i < num_post_operations; i++) {
37     const struct xnn_post_operation post_op = post_operations[i];
38     switch (post_op.op_type) {
39       case xnn_post_operation_type_hardswish:
40         if (xnn_params.f32.hswish.init.f32_hswish != NULL) {
41           const size_t initialized_size = xnn_params.f32.hswish.init.f32_hswish(&post_op_params.hswish_params);
42           memcpy(cur_params, &post_op_params.hswish_params, initialized_size);
43           cur_params += initialized_size;
44         }
45         break;
46       default:
47         XNN_UNREACHABLE;
48     }
49   }
50   return post_operation_params;
51 }
52 
53