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