/*
 * Copyright (C) 2010, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_VECTOR_MATH_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_VECTOR_MATH_H_

#include <cstddef>

#include "base/containers/span.h"
#include "third_party/blink/renderer/platform/audio/audio_array.h"
#include "third_party/blink/renderer/platform/platform_export.h"

// Defines the interface for several vector math functions whose implementation
// will ideally be optimized.

namespace blink::vector_math {

// Direct vector convolution:
//
// dest[k] = sum(source[k+m]*filter[filter.size()-1-m]) for all m
//
// Note: `filter` is read back to front
PLATFORM_EXPORT void Conv(base::span<const float> source,
                          base::span<const float> filter,
                          base::span<float> dest,
                          size_t frames_to_process,
                          const AudioFloatArray& prepared_filter);

// Prepare filter for Conv for faster processing.
PLATFORM_EXPORT void PrepareFilterForConv(base::span<const float> filter,
                                          AudioFloatArray& prepared_filter);

// Vector scalar multiply and then add.
//
// dest[k] += scale * source[k]
//
// Note: Mac has a different implementation, and it may produce slightly
// different results from what linux and windows would do.
PLATFORM_EXPORT void Vsma(base::span<const float> source,
                          float scale,
                          base::span<float> dest,
                          size_t frames_to_process);

// Vector scalar multiply:
//
// dest[k] = scale * source[k]
PLATFORM_EXPORT void Vsmul(base::span<const float> source,
                           float scale,
                           base::span<float> dest,
                           size_t frames_to_process);

// Vector scalar add:
//
// dest[k] = source[k] + addend
PLATFORM_EXPORT void Vsadd(base::span<const float> source,
                           float addend,
                           base::span<float> dest,
                           size_t frames_to_process);
// Vector add:
//
// dest[k] = source1[k] + source2[k]
PLATFORM_EXPORT void Vadd(base::span<const float> source1,
                          base::span<const float> source2,
                          base::span<float> dest,
                          size_t frames_to_process);

// Vector subtract:
//
// dest[k] = source1[k] - source2[k]
PLATFORM_EXPORT void Vsub(base::span<const float> source1,
                          base::span<const float> source2,
                          base::span<float> dest,
                          size_t frames_to_process);

// Finds the maximum magnitude of a float vector:
//
// max = max(abs(source[k])) for all k.
PLATFORM_EXPORT float Vmaxmgv(base::span<const float> source,
                              size_t frames_to_process);

// Sums the squares of a float vector's elements:
//
// sum = sum(source[k]^2, k = 0, frames_to_process);
PLATFORM_EXPORT float Vsvesq(base::span<const float> source,
                             size_t frames_to_process);

// For an element-by-element multiply of two float vectors:
//
// dest[k] = source1[k] * source2[k]
PLATFORM_EXPORT void Vmul(base::span<const float> source1,
                          base::span<const float> source2,
                          base::span<float> dest,
                          size_t frames_to_process);

// Multiplies two complex vectors.  Complex version of Vmul where |real1| and
// |imag1| forms the real and complex components of source1; |real2| and
// |imag2| the components of source2, and |real_dest| and |imag_dest|, the
// components of the destination.
PLATFORM_EXPORT void Zvmul(base::span<const float> real1,
                           base::span<const float> imag1,
                           base::span<const float> real2,
                           base::span<const float> imag2,
                           base::span<float> real_dest,
                           base::span<float> imag_dest,
                           size_t frames_to_process);

// Copies elements while clipping values to the threshold inputs.
//
// dest[k] = clip(source[k], low, high)
//
// where y = clip(x, low, high) = max(low, min(x, high)), effectively making
// low <= y <= high.
PLATFORM_EXPORT void Vclip(base::span<const float> source,
                           float low_threshold,
                           float high_threshold,
                           base::span<float> dest);

PLATFORM_EXPORT void Vclip(base::span<const float> source,
                           float low_threshold,
                           float high_threshold,
                           base::span<float> dest,
                           size_t frames_to_process);

}  // namespace blink::vector_math

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_AUDIO_VECTOR_MATH_H_
