tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

min_max_operations.c (6510B)


      1 /*
      2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 /*
     12 * This file contains the implementation of functions
     13 * WebRtcSpl_MaxAbsValueW16C()
     14 * WebRtcSpl_MaxAbsValueW32C()
     15 * WebRtcSpl_MaxValueW16C()
     16 * WebRtcSpl_MaxValueW32C()
     17 * WebRtcSpl_MinValueW16C()
     18 * WebRtcSpl_MinValueW32C()
     19 * WebRtcSpl_MaxAbsIndexW16()
     20 * WebRtcSpl_MaxIndexW16()
     21 * WebRtcSpl_MaxIndexW32()
     22 * WebRtcSpl_MinIndexW16()
     23 * WebRtcSpl_MinIndexW32()
     24 *
     25 */
     26 
     27 #include <limits.h>
     28 #include <stdlib.h>
     29 
     30 #include "common_audio/signal_processing/include/signal_processing_library.h"
     31 #include "rtc_base/checks.h"
     32 
     33 // TODO(bjorn/kma): Consolidate function pairs (e.g. combine
     34 //   WebRtcSpl_MaxAbsValueW16C and WebRtcSpl_MaxAbsIndexW16 into a single one.)
     35 // TODO(kma): Move the next six functions into min_max_operations_c.c.
     36 
     37 // Maximum absolute value of word16 vector. C version for generic platforms.
     38 int16_t WebRtcSpl_MaxAbsValueW16C(const int16_t* vector, size_t length) {
     39  size_t i = 0;
     40  int absolute = 0, maximum = 0;
     41 
     42  RTC_DCHECK_GT(length, 0);
     43 
     44  for (i = 0; i < length; i++) {
     45    absolute = abs((int)vector[i]);
     46 
     47    if (absolute > maximum) {
     48      maximum = absolute;
     49    }
     50  }
     51 
     52  // Guard the case for abs(-32768).
     53  if (maximum > WEBRTC_SPL_WORD16_MAX) {
     54    maximum = WEBRTC_SPL_WORD16_MAX;
     55  }
     56 
     57  return (int16_t)maximum;
     58 }
     59 
     60 // Maximum absolute value of word32 vector. C version for generic platforms.
     61 int32_t WebRtcSpl_MaxAbsValueW32C(const int32_t* vector, size_t length) {
     62  // Use uint32_t for the local variables, to accommodate the return value
     63  // of abs(0x80000000), which is 0x80000000.
     64 
     65  uint32_t absolute = 0, maximum = 0;
     66  size_t i = 0;
     67 
     68  RTC_DCHECK_GT(length, 0);
     69 
     70  for (i = 0; i < length; i++) {
     71    absolute =
     72        (vector[i] != INT_MIN) ? abs((int)vector[i]) : INT_MAX + (uint32_t)1;
     73    if (absolute > maximum) {
     74      maximum = absolute;
     75    }
     76  }
     77 
     78  maximum = WEBRTC_SPL_MIN(maximum, WEBRTC_SPL_WORD32_MAX);
     79 
     80  return (int32_t)maximum;
     81 }
     82 
     83 // Maximum value of word16 vector. C version for generic platforms.
     84 int16_t WebRtcSpl_MaxValueW16C(const int16_t* vector, size_t length) {
     85  int16_t maximum = WEBRTC_SPL_WORD16_MIN;
     86  size_t i = 0;
     87 
     88  RTC_DCHECK_GT(length, 0);
     89 
     90  for (i = 0; i < length; i++) {
     91    if (vector[i] > maximum)
     92      maximum = vector[i];
     93  }
     94  return maximum;
     95 }
     96 
     97 // Maximum value of word32 vector. C version for generic platforms.
     98 int32_t WebRtcSpl_MaxValueW32C(const int32_t* vector, size_t length) {
     99  int32_t maximum = WEBRTC_SPL_WORD32_MIN;
    100  size_t i = 0;
    101 
    102  RTC_DCHECK_GT(length, 0);
    103 
    104  for (i = 0; i < length; i++) {
    105    if (vector[i] > maximum)
    106      maximum = vector[i];
    107  }
    108  return maximum;
    109 }
    110 
    111 // Minimum value of word16 vector. C version for generic platforms.
    112 int16_t WebRtcSpl_MinValueW16C(const int16_t* vector, size_t length) {
    113  int16_t minimum = WEBRTC_SPL_WORD16_MAX;
    114  size_t i = 0;
    115 
    116  RTC_DCHECK_GT(length, 0);
    117 
    118  for (i = 0; i < length; i++) {
    119    if (vector[i] < minimum)
    120      minimum = vector[i];
    121  }
    122  return minimum;
    123 }
    124 
    125 // Minimum value of word32 vector. C version for generic platforms.
    126 int32_t WebRtcSpl_MinValueW32C(const int32_t* vector, size_t length) {
    127  int32_t minimum = WEBRTC_SPL_WORD32_MAX;
    128  size_t i = 0;
    129 
    130  RTC_DCHECK_GT(length, 0);
    131 
    132  for (i = 0; i < length; i++) {
    133    if (vector[i] < minimum)
    134      minimum = vector[i];
    135  }
    136  return minimum;
    137 }
    138 
    139 // Index of maximum absolute value in a word16 vector.
    140 size_t WebRtcSpl_MaxAbsIndexW16(const int16_t* vector, size_t length) {
    141  // Use type int for local variables, to accomodate the value of abs(-32768).
    142 
    143  size_t i = 0, index = 0;
    144  int absolute = 0, maximum = 0;
    145 
    146  RTC_DCHECK_GT(length, 0);
    147 
    148  for (i = 0; i < length; i++) {
    149    absolute = abs((int)vector[i]);
    150 
    151    if (absolute > maximum) {
    152      maximum = absolute;
    153      index = i;
    154    }
    155  }
    156 
    157  return index;
    158 }
    159 
    160 int16_t WebRtcSpl_MaxAbsElementW16(const int16_t* vector, size_t length) {
    161  int16_t min_val, max_val;
    162  WebRtcSpl_MinMaxW16(vector, length, &min_val, &max_val);
    163  if (min_val == max_val || min_val < -max_val) {
    164    return min_val;
    165  }
    166  return max_val;
    167 }
    168 
    169 // Index of maximum value in a word16 vector.
    170 size_t WebRtcSpl_MaxIndexW16(const int16_t* vector, size_t length) {
    171  size_t i = 0, index = 0;
    172  int16_t maximum = WEBRTC_SPL_WORD16_MIN;
    173 
    174  RTC_DCHECK_GT(length, 0);
    175 
    176  for (i = 0; i < length; i++) {
    177    if (vector[i] > maximum) {
    178      maximum = vector[i];
    179      index = i;
    180    }
    181  }
    182 
    183  return index;
    184 }
    185 
    186 // Index of maximum value in a word32 vector.
    187 size_t WebRtcSpl_MaxIndexW32(const int32_t* vector, size_t length) {
    188  size_t i = 0, index = 0;
    189  int32_t maximum = WEBRTC_SPL_WORD32_MIN;
    190 
    191  RTC_DCHECK_GT(length, 0);
    192 
    193  for (i = 0; i < length; i++) {
    194    if (vector[i] > maximum) {
    195      maximum = vector[i];
    196      index = i;
    197    }
    198  }
    199 
    200  return index;
    201 }
    202 
    203 // Index of minimum value in a word16 vector.
    204 size_t WebRtcSpl_MinIndexW16(const int16_t* vector, size_t length) {
    205  size_t i = 0, index = 0;
    206  int16_t minimum = WEBRTC_SPL_WORD16_MAX;
    207 
    208  RTC_DCHECK_GT(length, 0);
    209 
    210  for (i = 0; i < length; i++) {
    211    if (vector[i] < minimum) {
    212      minimum = vector[i];
    213      index = i;
    214    }
    215  }
    216 
    217  return index;
    218 }
    219 
    220 // Index of minimum value in a word32 vector.
    221 size_t WebRtcSpl_MinIndexW32(const int32_t* vector, size_t length) {
    222  size_t i = 0, index = 0;
    223  int32_t minimum = WEBRTC_SPL_WORD32_MAX;
    224 
    225  RTC_DCHECK_GT(length, 0);
    226 
    227  for (i = 0; i < length; i++) {
    228    if (vector[i] < minimum) {
    229      minimum = vector[i];
    230      index = i;
    231    }
    232  }
    233 
    234  return index;
    235 }
    236 
    237 // Finds both the minimum and maximum elements in an array of 16-bit integers.
    238 void WebRtcSpl_MinMaxW16(const int16_t* vector,
    239                         size_t length,
    240                         int16_t* min_val,
    241                         int16_t* max_val) {
    242 #if defined(WEBRTC_HAS_NEON)
    243  return WebRtcSpl_MinMaxW16Neon(vector, length, min_val, max_val);
    244 #else
    245  int16_t minimum = WEBRTC_SPL_WORD16_MAX;
    246  int16_t maximum = WEBRTC_SPL_WORD16_MIN;
    247  size_t i = 0;
    248 
    249  RTC_DCHECK_GT(length, 0);
    250 
    251  for (i = 0; i < length; i++) {
    252    if (vector[i] < minimum)
    253      minimum = vector[i];
    254    if (vector[i] > maximum)
    255      maximum = vector[i];
    256  }
    257  *min_val = minimum;
    258  *max_val = maximum;
    259 #endif
    260 }