xref: /aosp_15_r20/external/executorch/kernels/optimized/cpu/op_neg.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/kernels/optimized/vec/functional.h>
10 #include <executorch/kernels/optimized/vec/vec.h>
11 #include <executorch/runtime/kernel/kernel_includes.h>
12 
13 namespace torch {
14 namespace executor {
15 namespace native {
16 
opt_neg_out(KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)17 Tensor& opt_neg_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18   (void)ctx;
19 
20   // Resize for dynamic shape
21   auto error = resize_tensor(out, in.sizes());
22   ET_KERNEL_CHECK_MSG(
23       ctx,
24       error == Error::Ok,
25       InvalidArgument,
26       out,
27       "Failed to resize output tensor.");
28 
29   ET_SWITCH_REAL_TYPES(in.scalar_type(), ctx, "neg.out", CTYPE, [&] {
30     using Vec = executorch::vec::Vectorized<CTYPE>;
31     executorch::vec::map<CTYPE>(
32         [](Vec x) { return x.neg(); },
33         out.mutable_data_ptr<CTYPE>(),
34         in.const_data_ptr<CTYPE>(),
35         in.numel());
36   });
37 
38   return out;
39 }
40 
41 } // namespace native
42 } // namespace executor
43 } // namespace torch
44