From 71dd20a99530f22c86a987088484db8f4f227e52 Mon Sep 17 00:00:00 2001 From: Lamar <rlamarrr@gmail.com> Date: Thu, 9 Jan 2020 20:20:12 +0100 Subject: [PATCH] fixed static sized arrays with variable length using const int or int for the size of an array implies that it has variable length (ill-formed, https://en.cppreference.com/w/cpp/language/ub), static arrays' lengths should be constexpr or a macro constant --- tensorflow/lite/micro/micro_utils_test.cc | 6 +++--- tensorflow/lite/micro/testing_helpers_test.cc | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow/lite/micro/micro_utils_test.cc b/tensorflow/lite/micro/micro_utils_test.cc index e33d53b1c48..7aa31130595 100644 --- a/tensorflow/lite/micro/micro_utils_test.cc +++ b/tensorflow/lite/micro/micro_utils_test.cc @@ -82,7 +82,7 @@ TF_LITE_MICRO_TEST(FloatToAsymmetricQuantizedInt32Test) { TF_LITE_MICRO_TEST(AsymmetricQuantizeInt8) { float values[] = {-10.3, -3.1, -2.1, -1.9, -0.9, 0.1, 0.9, 1.85, 2.9, 4.1}; int8_t goldens[] = {-20, -5, -3, -3, -1, 1, 3, 5, 7, 9}; - const int length = sizeof(values) / sizeof(float); + constexpr int length = sizeof(values) / sizeof(float); int8_t quantized[length]; tflite::AsymmetricQuantize(values, quantized, length, 0.5, 1); for (int i = 0; i < length; i++) { @@ -93,7 +93,7 @@ TF_LITE_MICRO_TEST(AsymmetricQuantizeInt8) { TF_LITE_MICRO_TEST(AsymmetricQuantizeUInt8) { float values[] = {-10.3, -3.1, -2.1, -1.9, -0.9, 0.1, 0.9, 1.85, 2.9, 4.1}; uint8_t goldens[] = {106, 121, 123, 123, 125, 127, 129, 131, 133, 135}; - const int length = sizeof(values) / sizeof(float); + constexpr int length = sizeof(values) / sizeof(float); uint8_t quantized[length]; tflite::AsymmetricQuantize(values, quantized, length, 0.5, 127); for (int i = 0; i < length; i++) { @@ -104,7 +104,7 @@ TF_LITE_MICRO_TEST(AsymmetricQuantizeUInt8) { TF_LITE_MICRO_TEST(SymmetricQuantizeInt32) { float values[] = {-10.3, -3.1, -2.1, -1.9, -0.9, 0.1, 0.9, 1.85, 2.9, 4.1}; int32_t goldens[] = {-21, -6, -4, -4, -2, 0, 2, 4, 6, 8}; - const int length = sizeof(values) / sizeof(float); + constexpr int length = sizeof(values) / sizeof(float); int32_t quantized[length]; tflite::SymmetricQuantize(values, quantized, length, 0.5); for (int i = 0; i < length; i++) { diff --git a/tensorflow/lite/micro/testing_helpers_test.cc b/tensorflow/lite/micro/testing_helpers_test.cc index a7fc2996eb9..478f5ae6336 100644 --- a/tensorflow/lite/micro/testing_helpers_test.cc +++ b/tensorflow/lite/micro/testing_helpers_test.cc @@ -21,7 +21,7 @@ TF_LITE_MICRO_TESTS_BEGIN TF_LITE_MICRO_TEST(CreateQuantizedBiasTensor) { float input_scale = 0.5; float weight_scale = 0.5; - const int tensor_size = 12; + constexpr int tensor_size = 12; int dims_arr[] = {4, 2, 3, 2, 1}; const char* tensor_name = "test_tensor"; int32_t quantized[tensor_size]; @@ -45,7 +45,7 @@ TF_LITE_MICRO_TEST(CreateQuantizedBiasTensor) { TF_LITE_MICRO_TEST(CreatePerChannelQuantizedBiasTensor) { float input_scale = 0.5; float weight_scales[] = {0.5, 1, 2, 4}; - const int tensor_size = 12; + constexpr int tensor_size = 12; const int channels = 4; int dims_arr[] = {4, 4, 3, 1, 1}; const char* tensor_name = "test_tensor"; @@ -78,7 +78,7 @@ TF_LITE_MICRO_TEST(CreatePerChannelQuantizedBiasTensor) { TF_LITE_MICRO_TEST(CreateSymmetricPerChannelQuantizedTensor) { const int tensor_size = 12; - const int channels = 2; + constexpr int channels = 2; const int dims_arr[] = {4, channels, 3, 2, 1}; const char* tensor_name = "test_tensor"; int8_t quantized[12];