From 400b6ae14eb2e2668e60c0a74aef04af16dc9b76 Mon Sep 17 00:00:00 2001 From: skykongkong8 Date: Mon, 26 Feb 2024 08:52:43 +0900 Subject: [PATCH] [ util ] Use proper STL in max element comparison - For cleaner code use std::max_element instead of for-loop **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: skykongkong8 --- nntrainer/tensor/tensor.cpp | 3 +-- nntrainer/utils/util_simd.cpp | 18 ++++-------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/nntrainer/tensor/tensor.cpp b/nntrainer/tensor/tensor.cpp index 36014201cb..a471d84d99 100644 --- a/nntrainer/tensor/tensor.cpp +++ b/nntrainer/tensor/tensor.cpp @@ -127,8 +127,7 @@ class SrcSharedTensor { SrcSharedTensor() : src(nullptr), off(0) {} SrcSharedTensor(const Tensor *tensor, size_t offset) : - src(tensor), - off(offset) {} + src(tensor), off(offset) {} /** * @brief Get the allocated src tensor diff --git a/nntrainer/utils/util_simd.cpp b/nntrainer/utils/util_simd.cpp index 98e2a78b13..2ec89872e1 100644 --- a/nntrainer/utils/util_simd.cpp +++ b/nntrainer/utils/util_simd.cpp @@ -44,13 +44,8 @@ float max(const unsigned int N, float *X) { #ifdef USE_NEON return nntrainer::neon::max(N, X); #else - float ret = X[0]; - unsigned int i = 1; - while (i < N) { - ret = std::fmax(ret, X[i]); - ++i; - } - return ret; + std::vector v(X, X + N); + return *std::max_element(v.begin(), v.end()); #endif } @@ -106,13 +101,8 @@ _FP16 max(const unsigned int N, _FP16 *X) { #ifdef USE_NEON return nntrainer::neon::max(N, X); #else - _FP16 ret = X[0]; - unsigned int i = 1; - while (i < N) { - ret = (ret > X[i]) ? ret : X[i]; - ++i; - } - return ret; + std::vector<_FP16> v(X, X + N); + return *std::max_element(v.begin(), v.end()); #endif }