fast_log.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package brotli
  2. import (
  3. "math"
  4. "math/bits"
  5. )
  6. /* Copyright 2013 Google Inc. All Rights Reserved.
  7. Distributed under MIT license.
  8. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  9. */
  10. /* Utilities for fast computation of logarithms. */
  11. func log2FloorNonZero(n uint) uint32 {
  12. return uint32(bits.Len(n)) - 1
  13. }
  14. /* A lookup table for small values of log2(int) to be used in entropy
  15. computation.
  16. ", ".join(["%.16ff" % x for x in [0.0]+[log2(x) for x in range(1, 256)]]) */
  17. var kLog2Table = []float32{
  18. 0.0000000000000000,
  19. 0.0000000000000000,
  20. 1.0000000000000000,
  21. 1.5849625007211563,
  22. 2.0000000000000000,
  23. 2.3219280948873622,
  24. 2.5849625007211561,
  25. 2.8073549220576042,
  26. 3.0000000000000000,
  27. 3.1699250014423126,
  28. 3.3219280948873626,
  29. 3.4594316186372978,
  30. 3.5849625007211565,
  31. 3.7004397181410922,
  32. 3.8073549220576037,
  33. 3.9068905956085187,
  34. 4.0000000000000000,
  35. 4.0874628412503400,
  36. 4.1699250014423122,
  37. 4.2479275134435852,
  38. 4.3219280948873626,
  39. 4.3923174227787607,
  40. 4.4594316186372973,
  41. 4.5235619560570131,
  42. 4.5849625007211570,
  43. 4.6438561897747244,
  44. 4.7004397181410926,
  45. 4.7548875021634691,
  46. 4.8073549220576037,
  47. 4.8579809951275728,
  48. 4.9068905956085187,
  49. 4.9541963103868758,
  50. 5.0000000000000000,
  51. 5.0443941193584534,
  52. 5.0874628412503400,
  53. 5.1292830169449664,
  54. 5.1699250014423122,
  55. 5.2094533656289501,
  56. 5.2479275134435852,
  57. 5.2854022188622487,
  58. 5.3219280948873626,
  59. 5.3575520046180838,
  60. 5.3923174227787607,
  61. 5.4262647547020979,
  62. 5.4594316186372973,
  63. 5.4918530963296748,
  64. 5.5235619560570131,
  65. 5.5545888516776376,
  66. 5.5849625007211570,
  67. 5.6147098441152083,
  68. 5.6438561897747244,
  69. 5.6724253419714961,
  70. 5.7004397181410926,
  71. 5.7279204545631996,
  72. 5.7548875021634691,
  73. 5.7813597135246599,
  74. 5.8073549220576046,
  75. 5.8328900141647422,
  76. 5.8579809951275719,
  77. 5.8826430493618416,
  78. 5.9068905956085187,
  79. 5.9307373375628867,
  80. 5.9541963103868758,
  81. 5.9772799234999168,
  82. 6.0000000000000000,
  83. 6.0223678130284544,
  84. 6.0443941193584534,
  85. 6.0660891904577721,
  86. 6.0874628412503400,
  87. 6.1085244567781700,
  88. 6.1292830169449672,
  89. 6.1497471195046822,
  90. 6.1699250014423122,
  91. 6.1898245588800176,
  92. 6.2094533656289510,
  93. 6.2288186904958804,
  94. 6.2479275134435861,
  95. 6.2667865406949019,
  96. 6.2854022188622487,
  97. 6.3037807481771031,
  98. 6.3219280948873617,
  99. 6.3398500028846252,
  100. 6.3575520046180847,
  101. 6.3750394313469254,
  102. 6.3923174227787598,
  103. 6.4093909361377026,
  104. 6.4262647547020979,
  105. 6.4429434958487288,
  106. 6.4594316186372982,
  107. 6.4757334309663976,
  108. 6.4918530963296748,
  109. 6.5077946401986964,
  110. 6.5235619560570131,
  111. 6.5391588111080319,
  112. 6.5545888516776376,
  113. 6.5698556083309478,
  114. 6.5849625007211561,
  115. 6.5999128421871278,
  116. 6.6147098441152092,
  117. 6.6293566200796095,
  118. 6.6438561897747253,
  119. 6.6582114827517955,
  120. 6.6724253419714952,
  121. 6.6865005271832185,
  122. 6.7004397181410917,
  123. 6.7142455176661224,
  124. 6.7279204545631988,
  125. 6.7414669864011465,
  126. 6.7548875021634691,
  127. 6.7681843247769260,
  128. 6.7813597135246599,
  129. 6.7944158663501062,
  130. 6.8073549220576037,
  131. 6.8201789624151887,
  132. 6.8328900141647422,
  133. 6.8454900509443757,
  134. 6.8579809951275719,
  135. 6.8703647195834048,
  136. 6.8826430493618416,
  137. 6.8948177633079437,
  138. 6.9068905956085187,
  139. 6.9188632372745955,
  140. 6.9307373375628867,
  141. 6.9425145053392399,
  142. 6.9541963103868758,
  143. 6.9657842846620879,
  144. 6.9772799234999168,
  145. 6.9886846867721664,
  146. 7.0000000000000000,
  147. 7.0112272554232540,
  148. 7.0223678130284544,
  149. 7.0334230015374501,
  150. 7.0443941193584534,
  151. 7.0552824355011898,
  152. 7.0660891904577721,
  153. 7.0768155970508317,
  154. 7.0874628412503400,
  155. 7.0980320829605272,
  156. 7.1085244567781700,
  157. 7.1189410727235076,
  158. 7.1292830169449664,
  159. 7.1395513523987937,
  160. 7.1497471195046822,
  161. 7.1598713367783891,
  162. 7.1699250014423130,
  163. 7.1799090900149345,
  164. 7.1898245588800176,
  165. 7.1996723448363644,
  166. 7.2094533656289492,
  167. 7.2191685204621621,
  168. 7.2288186904958804,
  169. 7.2384047393250794,
  170. 7.2479275134435861,
  171. 7.2573878426926521,
  172. 7.2667865406949019,
  173. 7.2761244052742384,
  174. 7.2854022188622487,
  175. 7.2946207488916270,
  176. 7.3037807481771031,
  177. 7.3128829552843557,
  178. 7.3219280948873617,
  179. 7.3309168781146177,
  180. 7.3398500028846243,
  181. 7.3487281542310781,
  182. 7.3575520046180847,
  183. 7.3663222142458151,
  184. 7.3750394313469254,
  185. 7.3837042924740528,
  186. 7.3923174227787607,
  187. 7.4008794362821844,
  188. 7.4093909361377026,
  189. 7.4178525148858991,
  190. 7.4262647547020979,
  191. 7.4346282276367255,
  192. 7.4429434958487288,
  193. 7.4512111118323299,
  194. 7.4594316186372973,
  195. 7.4676055500829976,
  196. 7.4757334309663976,
  197. 7.4838157772642564,
  198. 7.4918530963296748,
  199. 7.4998458870832057,
  200. 7.5077946401986964,
  201. 7.5156998382840436,
  202. 7.5235619560570131,
  203. 7.5313814605163119,
  204. 7.5391588111080319,
  205. 7.5468944598876373,
  206. 7.5545888516776376,
  207. 7.5622424242210728,
  208. 7.5698556083309478,
  209. 7.5774288280357487,
  210. 7.5849625007211561,
  211. 7.5924570372680806,
  212. 7.5999128421871278,
  213. 7.6073303137496113,
  214. 7.6147098441152075,
  215. 7.6220518194563764,
  216. 7.6293566200796095,
  217. 7.6366246205436488,
  218. 7.6438561897747244,
  219. 7.6510516911789290,
  220. 7.6582114827517955,
  221. 7.6653359171851765,
  222. 7.6724253419714952,
  223. 7.6794800995054464,
  224. 7.6865005271832185,
  225. 7.6934869574993252,
  226. 7.7004397181410926,
  227. 7.7073591320808825,
  228. 7.7142455176661224,
  229. 7.7210991887071856,
  230. 7.7279204545631996,
  231. 7.7347096202258392,
  232. 7.7414669864011465,
  233. 7.7481928495894596,
  234. 7.7548875021634691,
  235. 7.7615512324444795,
  236. 7.7681843247769260,
  237. 7.7747870596011737,
  238. 7.7813597135246608,
  239. 7.7879025593914317,
  240. 7.7944158663501062,
  241. 7.8008998999203047,
  242. 7.8073549220576037,
  243. 7.8137811912170374,
  244. 7.8201789624151887,
  245. 7.8265484872909159,
  246. 7.8328900141647422,
  247. 7.8392037880969445,
  248. 7.8454900509443757,
  249. 7.8517490414160571,
  250. 7.8579809951275719,
  251. 7.8641861446542798,
  252. 7.8703647195834048,
  253. 7.8765169465650002,
  254. 7.8826430493618425,
  255. 7.8887432488982601,
  256. 7.8948177633079446,
  257. 7.9008668079807496,
  258. 7.9068905956085187,
  259. 7.9128893362299619,
  260. 7.9188632372745955,
  261. 7.9248125036057813,
  262. 7.9307373375628867,
  263. 7.9366379390025719,
  264. 7.9425145053392399,
  265. 7.9483672315846778,
  266. 7.9541963103868758,
  267. 7.9600019320680806,
  268. 7.9657842846620870,
  269. 7.9715435539507720,
  270. 7.9772799234999168,
  271. 7.9829935746943104,
  272. 7.9886846867721664,
  273. 7.9943534368588578,
  274. }
  275. /* Faster logarithm for small integers, with the property of log2(0) == 0. */
  276. func fastLog2(v uint) float64 {
  277. if v < uint(len(kLog2Table)) {
  278. return float64(kLog2Table[v])
  279. }
  280. return math.Log2(float64(v))
  281. }