Avoid overflow in WebRtcSpl_DotProductWithScale

BUG=chromium:676935

Review-Url: https://codereview.webrtc.org/2717123004
Cr-Commit-Position: refs/heads/master@{#17091}
This commit is contained in:
henrik.lundin
2017-03-07 04:02:47 -08:00
committed by Commit bot
parent 57f19cc0cd
commit d461ffce2a
4 changed files with 63 additions and 20 deletions

View File

@ -116,7 +116,6 @@ rtc_source_set("common_audio_c") {
"signal_processing/copy_set_operations.c",
"signal_processing/cross_correlation.c",
"signal_processing/division_operations.c",
"signal_processing/dot_product_with_scale.c",
"signal_processing/downsample_fast.c",
"signal_processing/energy.c",
"signal_processing/filter_ar.c",
@ -201,6 +200,22 @@ rtc_source_set("common_audio_c") {
cflags = [ "/wd4334" ] # Ignore warning on shift operator promotion.
}
public_configs = [ ":common_audio_config" ]
deps = [
":common_audio_cc",
"..:webrtc_common",
"../base:rtc_base_approved",
"../system_wrappers:system_wrappers",
]
}
rtc_source_set("common_audio_cc") {
visibility = [ ":*" ] # Only targets in this file can depend on this.
sources = [
"signal_processing/dot_product_with_scale.cc",
"signal_processing/dot_product_with_scale.h",
]
public_configs = [ ":common_audio_config" ]
deps = [
"..:webrtc_common",

View File

@ -8,13 +8,15 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
#include "webrtc/common_audio/signal_processing/dot_product_with_scale.h"
#include "webrtc/base/safe_conversions.h"
int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
const int16_t* vector2,
size_t length,
int scaling) {
int32_t sum = 0;
int64_t sum = 0;
size_t i = 0;
/* Unroll the loop to improve performance. */
@ -28,5 +30,5 @@ int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
sum += (vector1[i] * vector2[i]) >> scaling;
}
return sum;
return rtc::saturated_cast<int32_t>(sum);
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_
#define WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_
#include <string.h>
#include "webrtc/typedefs.h"
#ifdef __cplusplus
extern "C" {
#endif
// Calculates the dot product between two (int16_t) vectors.
//
// Input:
// - vector1 : Vector 1
// - vector2 : Vector 2
// - vector_length : Number of samples used in the dot product
// - scaling : The number of right bit shifts to apply on each term
// during calculation to avoid overflow, i.e., the
// output will be in Q(-|scaling|)
//
// Return value : The dot product in Q(-scaling)
int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
const int16_t* vector2,
size_t length,
int scaling);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_

View File

@ -19,6 +19,7 @@
#define WEBRTC_SPL_SIGNAL_PROCESSING_LIBRARY_H_
#include <string.h>
#include "webrtc/common_audio/signal_processing/dot_product_with_scale.h"
#include "webrtc/typedefs.h"
// Macros specific for the fixed point implementation
@ -597,22 +598,6 @@ int32_t WebRtcSpl_Energy(int16_t* vector,
size_t vector_length,
int* scale_factor);
// Calculates the dot product between two (int16_t) vectors.
//
// Input:
// - vector1 : Vector 1
// - vector2 : Vector 2
// - vector_length : Number of samples used in the dot product
// - scaling : The number of right bit shifts to apply on each term
// during calculation to avoid overflow, i.e., the
// output will be in Q(-|scaling|)
//
// Return value : The dot product in Q(-scaling)
int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
const int16_t* vector2,
size_t length,
int scaling);
// Filter operations.
size_t WebRtcSpl_FilterAR(const int16_t* ar_coef,
size_t ar_coef_length,