Blame view

ios/cocos2d/cocos/audio/android/audio_utils/minifloat.cpp 1.97 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  /*
   * Copyright (C) 2014 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  #include <cmath>
  #include "audio/android/audio_utils/include/audio_utils/minifloat.h"
  
  #define EXPONENT_BITS   3
  #define EXPONENT_MAX    ((1 << EXPONENT_BITS) - 1)
  #define EXCESS          ((1 << EXPONENT_BITS) - 2)
  
  #define MANTISSA_BITS   13
  #define MANTISSA_MAX    ((1 << MANTISSA_BITS) - 1)
  #define HIDDEN_BIT      (1 << MANTISSA_BITS)
  #define ONE_FLOAT       ((float) (1 << (MANTISSA_BITS + 1)))
  
  #define MINIFLOAT_MAX   ((EXPONENT_MAX << MANTISSA_BITS) | MANTISSA_MAX)
  
  #if EXPONENT_BITS + MANTISSA_BITS != 16
  #error EXPONENT_BITS and MANTISSA_BITS must sum to 16
  #endif
  
  extern "C" {
  
  gain_minifloat_t gain_from_float(float v)
  {
      if (std::isnan(v) || v <= 0.0f)
      {
          return 0;
      }
      if (v >= 2.0f)
      {
          return MINIFLOAT_MAX;
      }
      int exp;
      float r = frexpf(v, &exp);
      if ((exp += EXCESS) > EXPONENT_MAX)
      {
          return MINIFLOAT_MAX;
      }
      if (-exp >= MANTISSA_BITS)
      {
          return 0;
      }
      int mantissa = (int) (r * ONE_FLOAT);
      return exp > 0 ? (exp << MANTISSA_BITS) | (mantissa & ~HIDDEN_BIT) :
             (mantissa >> (1 - exp)) & MANTISSA_MAX;
  }
  
  float float_from_gain(gain_minifloat_t a)
  {
      int mantissa = a & MANTISSA_MAX;
      int exponent = (a >> MANTISSA_BITS) & EXPONENT_MAX;
      return ldexpf((exponent > 0 ? HIDDEN_BIT | mantissa : mantissa << 1) / ONE_FLOAT,
                    exponent - EXCESS);
  }
  
  } // extern "C" {