compress.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright 2018 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
  5. package fse
  6. import (
  7. "errors"
  8. "fmt"
  9. )
  10. // Compress the input bytes. Input must be < 2GB.
  11. // Provide a Scratch buffer to avoid memory allocations.
  12. // Note that the output is also kept in the scratch buffer.
  13. // If input is too hard to compress, ErrIncompressible is returned.
  14. // If input is a single byte value repeated ErrUseRLE is returned.
  15. func Compress(in []byte, s *Scratch) ([]byte, error) {
  16. if len(in) <= 1 {
  17. return nil, ErrIncompressible
  18. }
  19. if len(in) > (2<<30)-1 {
  20. return nil, errors.New("input too big, must be < 2GB")
  21. }
  22. s, err := s.prepare(in)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // Create histogram, if none was provided.
  27. maxCount := s.maxCount
  28. if maxCount == 0 {
  29. maxCount = s.countSimple(in)
  30. }
  31. // Reset for next run.
  32. s.clearCount = true
  33. s.maxCount = 0
  34. if maxCount == len(in) {
  35. // One symbol, use RLE
  36. return nil, ErrUseRLE
  37. }
  38. if maxCount == 1 || maxCount < (len(in)>>7) {
  39. // Each symbol present maximum once or too well distributed.
  40. return nil, ErrIncompressible
  41. }
  42. s.optimalTableLog()
  43. err = s.normalizeCount()
  44. if err != nil {
  45. return nil, err
  46. }
  47. err = s.writeCount()
  48. if err != nil {
  49. return nil, err
  50. }
  51. if false {
  52. err = s.validateNorm()
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. err = s.buildCTable()
  58. if err != nil {
  59. return nil, err
  60. }
  61. err = s.compress(in)
  62. if err != nil {
  63. return nil, err
  64. }
  65. s.Out = s.bw.out
  66. // Check if we compressed.
  67. if len(s.Out) >= len(in) {
  68. return nil, ErrIncompressible
  69. }
  70. return s.Out, nil
  71. }
  72. // cState contains the compression state of a stream.
  73. type cState struct {
  74. bw *bitWriter
  75. stateTable []uint16
  76. state uint16
  77. }
  78. // init will initialize the compression state to the first symbol of the stream.
  79. func (c *cState) init(bw *bitWriter, ct *cTable, tableLog uint8, first symbolTransform) {
  80. c.bw = bw
  81. c.stateTable = ct.stateTable
  82. nbBitsOut := (first.deltaNbBits + (1 << 15)) >> 16
  83. im := int32((nbBitsOut << 16) - first.deltaNbBits)
  84. lu := (im >> nbBitsOut) + first.deltaFindState
  85. c.state = c.stateTable[lu]
  86. }
  87. // encode the output symbol provided and write it to the bitstream.
  88. func (c *cState) encode(symbolTT symbolTransform) {
  89. nbBitsOut := (uint32(c.state) + symbolTT.deltaNbBits) >> 16
  90. dstState := int32(c.state>>(nbBitsOut&15)) + symbolTT.deltaFindState
  91. c.bw.addBits16NC(c.state, uint8(nbBitsOut))
  92. c.state = c.stateTable[dstState]
  93. }
  94. // encode the output symbol provided and write it to the bitstream.
  95. func (c *cState) encodeZero(symbolTT symbolTransform) {
  96. nbBitsOut := (uint32(c.state) + symbolTT.deltaNbBits) >> 16
  97. dstState := int32(c.state>>(nbBitsOut&15)) + symbolTT.deltaFindState
  98. c.bw.addBits16ZeroNC(c.state, uint8(nbBitsOut))
  99. c.state = c.stateTable[dstState]
  100. }
  101. // flush will write the tablelog to the output and flush the remaining full bytes.
  102. func (c *cState) flush(tableLog uint8) {
  103. c.bw.flush32()
  104. c.bw.addBits16NC(c.state, tableLog)
  105. c.bw.flush()
  106. }
  107. // compress is the main compression loop that will encode the input from the last byte to the first.
  108. func (s *Scratch) compress(src []byte) error {
  109. if len(src) <= 2 {
  110. return errors.New("compress: src too small")
  111. }
  112. tt := s.ct.symbolTT[:256]
  113. s.bw.reset(s.Out)
  114. // Our two states each encodes every second byte.
  115. // Last byte encoded (first byte decoded) will always be encoded by c1.
  116. var c1, c2 cState
  117. // Encode so remaining size is divisible by 4.
  118. ip := len(src)
  119. if ip&1 == 1 {
  120. c1.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-1]])
  121. c2.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-2]])
  122. c1.encodeZero(tt[src[ip-3]])
  123. ip -= 3
  124. } else {
  125. c2.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-1]])
  126. c1.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-2]])
  127. ip -= 2
  128. }
  129. if ip&2 != 0 {
  130. c2.encodeZero(tt[src[ip-1]])
  131. c1.encodeZero(tt[src[ip-2]])
  132. ip -= 2
  133. }
  134. src = src[:ip]
  135. // Main compression loop.
  136. switch {
  137. case !s.zeroBits && s.actualTableLog <= 8:
  138. // We can encode 4 symbols without requiring a flush.
  139. // We do not need to check if any output is 0 bits.
  140. for ; len(src) >= 4; src = src[:len(src)-4] {
  141. s.bw.flush32()
  142. v3, v2, v1, v0 := src[len(src)-4], src[len(src)-3], src[len(src)-2], src[len(src)-1]
  143. c2.encode(tt[v0])
  144. c1.encode(tt[v1])
  145. c2.encode(tt[v2])
  146. c1.encode(tt[v3])
  147. }
  148. case !s.zeroBits:
  149. // We do not need to check if any output is 0 bits.
  150. for ; len(src) >= 4; src = src[:len(src)-4] {
  151. s.bw.flush32()
  152. v3, v2, v1, v0 := src[len(src)-4], src[len(src)-3], src[len(src)-2], src[len(src)-1]
  153. c2.encode(tt[v0])
  154. c1.encode(tt[v1])
  155. s.bw.flush32()
  156. c2.encode(tt[v2])
  157. c1.encode(tt[v3])
  158. }
  159. case s.actualTableLog <= 8:
  160. // We can encode 4 symbols without requiring a flush
  161. for ; len(src) >= 4; src = src[:len(src)-4] {
  162. s.bw.flush32()
  163. v3, v2, v1, v0 := src[len(src)-4], src[len(src)-3], src[len(src)-2], src[len(src)-1]
  164. c2.encodeZero(tt[v0])
  165. c1.encodeZero(tt[v1])
  166. c2.encodeZero(tt[v2])
  167. c1.encodeZero(tt[v3])
  168. }
  169. default:
  170. for ; len(src) >= 4; src = src[:len(src)-4] {
  171. s.bw.flush32()
  172. v3, v2, v1, v0 := src[len(src)-4], src[len(src)-3], src[len(src)-2], src[len(src)-1]
  173. c2.encodeZero(tt[v0])
  174. c1.encodeZero(tt[v1])
  175. s.bw.flush32()
  176. c2.encodeZero(tt[v2])
  177. c1.encodeZero(tt[v3])
  178. }
  179. }
  180. // Flush final state.
  181. // Used to initialize state when decoding.
  182. c2.flush(s.actualTableLog)
  183. c1.flush(s.actualTableLog)
  184. return s.bw.close()
  185. }
  186. // writeCount will write the normalized histogram count to header.
  187. // This is read back by readNCount.
  188. func (s *Scratch) writeCount() error {
  189. var (
  190. tableLog = s.actualTableLog
  191. tableSize = 1 << tableLog
  192. previous0 bool
  193. charnum uint16
  194. maxHeaderSize = ((int(s.symbolLen) * int(tableLog)) >> 3) + 3
  195. // Write Table Size
  196. bitStream = uint32(tableLog - minTablelog)
  197. bitCount = uint(4)
  198. remaining = int16(tableSize + 1) /* +1 for extra accuracy */
  199. threshold = int16(tableSize)
  200. nbBits = uint(tableLog + 1)
  201. )
  202. if cap(s.Out) < maxHeaderSize {
  203. s.Out = make([]byte, 0, s.br.remain()+maxHeaderSize)
  204. }
  205. outP := uint(0)
  206. out := s.Out[:maxHeaderSize]
  207. // stops at 1
  208. for remaining > 1 {
  209. if previous0 {
  210. start := charnum
  211. for s.norm[charnum] == 0 {
  212. charnum++
  213. }
  214. for charnum >= start+24 {
  215. start += 24
  216. bitStream += uint32(0xFFFF) << bitCount
  217. out[outP] = byte(bitStream)
  218. out[outP+1] = byte(bitStream >> 8)
  219. outP += 2
  220. bitStream >>= 16
  221. }
  222. for charnum >= start+3 {
  223. start += 3
  224. bitStream += 3 << bitCount
  225. bitCount += 2
  226. }
  227. bitStream += uint32(charnum-start) << bitCount
  228. bitCount += 2
  229. if bitCount > 16 {
  230. out[outP] = byte(bitStream)
  231. out[outP+1] = byte(bitStream >> 8)
  232. outP += 2
  233. bitStream >>= 16
  234. bitCount -= 16
  235. }
  236. }
  237. count := s.norm[charnum]
  238. charnum++
  239. max := (2*threshold - 1) - remaining
  240. if count < 0 {
  241. remaining += count
  242. } else {
  243. remaining -= count
  244. }
  245. count++ // +1 for extra accuracy
  246. if count >= threshold {
  247. count += max // [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[
  248. }
  249. bitStream += uint32(count) << bitCount
  250. bitCount += nbBits
  251. if count < max {
  252. bitCount--
  253. }
  254. previous0 = count == 1
  255. if remaining < 1 {
  256. return errors.New("internal error: remaining<1")
  257. }
  258. for remaining < threshold {
  259. nbBits--
  260. threshold >>= 1
  261. }
  262. if bitCount > 16 {
  263. out[outP] = byte(bitStream)
  264. out[outP+1] = byte(bitStream >> 8)
  265. outP += 2
  266. bitStream >>= 16
  267. bitCount -= 16
  268. }
  269. }
  270. out[outP] = byte(bitStream)
  271. out[outP+1] = byte(bitStream >> 8)
  272. outP += (bitCount + 7) / 8
  273. if charnum > s.symbolLen {
  274. return errors.New("internal error: charnum > s.symbolLen")
  275. }
  276. s.Out = out[:outP]
  277. return nil
  278. }
  279. // symbolTransform contains the state transform for a symbol.
  280. type symbolTransform struct {
  281. deltaFindState int32
  282. deltaNbBits uint32
  283. }
  284. // String prints values as a human readable string.
  285. func (s symbolTransform) String() string {
  286. return fmt.Sprintf("dnbits: %08x, fs:%d", s.deltaNbBits, s.deltaFindState)
  287. }
  288. // cTable contains tables used for compression.
  289. type cTable struct {
  290. tableSymbol []byte
  291. stateTable []uint16
  292. symbolTT []symbolTransform
  293. }
  294. // allocCtable will allocate tables needed for compression.
  295. // If existing tables a re big enough, they are simply re-used.
  296. func (s *Scratch) allocCtable() {
  297. tableSize := 1 << s.actualTableLog
  298. // get tableSymbol that is big enough.
  299. if cap(s.ct.tableSymbol) < tableSize {
  300. s.ct.tableSymbol = make([]byte, tableSize)
  301. }
  302. s.ct.tableSymbol = s.ct.tableSymbol[:tableSize]
  303. ctSize := tableSize
  304. if cap(s.ct.stateTable) < ctSize {
  305. s.ct.stateTable = make([]uint16, ctSize)
  306. }
  307. s.ct.stateTable = s.ct.stateTable[:ctSize]
  308. if cap(s.ct.symbolTT) < 256 {
  309. s.ct.symbolTT = make([]symbolTransform, 256)
  310. }
  311. s.ct.symbolTT = s.ct.symbolTT[:256]
  312. }
  313. // buildCTable will populate the compression table so it is ready to be used.
  314. func (s *Scratch) buildCTable() error {
  315. tableSize := uint32(1 << s.actualTableLog)
  316. highThreshold := tableSize - 1
  317. var cumul [maxSymbolValue + 2]int16
  318. s.allocCtable()
  319. tableSymbol := s.ct.tableSymbol[:tableSize]
  320. // symbol start positions
  321. {
  322. cumul[0] = 0
  323. for ui, v := range s.norm[:s.symbolLen-1] {
  324. u := byte(ui) // one less than reference
  325. if v == -1 {
  326. // Low proba symbol
  327. cumul[u+1] = cumul[u] + 1
  328. tableSymbol[highThreshold] = u
  329. highThreshold--
  330. } else {
  331. cumul[u+1] = cumul[u] + v
  332. }
  333. }
  334. // Encode last symbol separately to avoid overflowing u
  335. u := int(s.symbolLen - 1)
  336. v := s.norm[s.symbolLen-1]
  337. if v == -1 {
  338. // Low proba symbol
  339. cumul[u+1] = cumul[u] + 1
  340. tableSymbol[highThreshold] = byte(u)
  341. highThreshold--
  342. } else {
  343. cumul[u+1] = cumul[u] + v
  344. }
  345. if uint32(cumul[s.symbolLen]) != tableSize {
  346. return fmt.Errorf("internal error: expected cumul[s.symbolLen] (%d) == tableSize (%d)", cumul[s.symbolLen], tableSize)
  347. }
  348. cumul[s.symbolLen] = int16(tableSize) + 1
  349. }
  350. // Spread symbols
  351. s.zeroBits = false
  352. {
  353. step := tableStep(tableSize)
  354. tableMask := tableSize - 1
  355. var position uint32
  356. // if any symbol > largeLimit, we may have 0 bits output.
  357. largeLimit := int16(1 << (s.actualTableLog - 1))
  358. for ui, v := range s.norm[:s.symbolLen] {
  359. symbol := byte(ui)
  360. if v > largeLimit {
  361. s.zeroBits = true
  362. }
  363. for nbOccurrences := int16(0); nbOccurrences < v; nbOccurrences++ {
  364. tableSymbol[position] = symbol
  365. position = (position + step) & tableMask
  366. for position > highThreshold {
  367. position = (position + step) & tableMask
  368. } /* Low proba area */
  369. }
  370. }
  371. // Check if we have gone through all positions
  372. if position != 0 {
  373. return errors.New("position!=0")
  374. }
  375. }
  376. // Build table
  377. table := s.ct.stateTable
  378. {
  379. tsi := int(tableSize)
  380. for u, v := range tableSymbol {
  381. // TableU16 : sorted by symbol order; gives next state value
  382. table[cumul[v]] = uint16(tsi + u)
  383. cumul[v]++
  384. }
  385. }
  386. // Build Symbol Transformation Table
  387. {
  388. total := int16(0)
  389. symbolTT := s.ct.symbolTT[:s.symbolLen]
  390. tableLog := s.actualTableLog
  391. tl := (uint32(tableLog) << 16) - (1 << tableLog)
  392. for i, v := range s.norm[:s.symbolLen] {
  393. switch v {
  394. case 0:
  395. case -1, 1:
  396. symbolTT[i].deltaNbBits = tl
  397. symbolTT[i].deltaFindState = int32(total - 1)
  398. total++
  399. default:
  400. maxBitsOut := uint32(tableLog) - highBits(uint32(v-1))
  401. minStatePlus := uint32(v) << maxBitsOut
  402. symbolTT[i].deltaNbBits = (maxBitsOut << 16) - minStatePlus
  403. symbolTT[i].deltaFindState = int32(total - v)
  404. total += v
  405. }
  406. }
  407. if total != int16(tableSize) {
  408. return fmt.Errorf("total mismatch %d (got) != %d (want)", total, tableSize)
  409. }
  410. }
  411. return nil
  412. }
  413. // countSimple will create a simple histogram in s.count.
  414. // Returns the biggest count.
  415. // Does not update s.clearCount.
  416. func (s *Scratch) countSimple(in []byte) (max int) {
  417. for _, v := range in {
  418. s.count[v]++
  419. }
  420. m, symlen := uint32(0), s.symbolLen
  421. for i, v := range s.count[:] {
  422. if v == 0 {
  423. continue
  424. }
  425. if v > m {
  426. m = v
  427. }
  428. symlen = uint16(i) + 1
  429. }
  430. s.symbolLen = symlen
  431. return int(m)
  432. }
  433. // minTableLog provides the minimum logSize to safely represent a distribution.
  434. func (s *Scratch) minTableLog() uint8 {
  435. minBitsSrc := highBits(uint32(s.br.remain()-1)) + 1
  436. minBitsSymbols := highBits(uint32(s.symbolLen-1)) + 2
  437. if minBitsSrc < minBitsSymbols {
  438. return uint8(minBitsSrc)
  439. }
  440. return uint8(minBitsSymbols)
  441. }
  442. // optimalTableLog calculates and sets the optimal tableLog in s.actualTableLog
  443. func (s *Scratch) optimalTableLog() {
  444. tableLog := s.TableLog
  445. minBits := s.minTableLog()
  446. maxBitsSrc := uint8(highBits(uint32(s.br.remain()-1))) - 2
  447. if maxBitsSrc < tableLog {
  448. // Accuracy can be reduced
  449. tableLog = maxBitsSrc
  450. }
  451. if minBits > tableLog {
  452. tableLog = minBits
  453. }
  454. // Need a minimum to safely represent all symbol values
  455. if tableLog < minTablelog {
  456. tableLog = minTablelog
  457. }
  458. if tableLog > maxTableLog {
  459. tableLog = maxTableLog
  460. }
  461. s.actualTableLog = tableLog
  462. }
  463. var rtbTable = [...]uint32{0, 473195, 504333, 520860, 550000, 700000, 750000, 830000}
  464. // normalizeCount will normalize the count of the symbols so
  465. // the total is equal to the table size.
  466. func (s *Scratch) normalizeCount() error {
  467. var (
  468. tableLog = s.actualTableLog
  469. scale = 62 - uint64(tableLog)
  470. step = (1 << 62) / uint64(s.br.remain())
  471. vStep = uint64(1) << (scale - 20)
  472. stillToDistribute = int16(1 << tableLog)
  473. largest int
  474. largestP int16
  475. lowThreshold = (uint32)(s.br.remain() >> tableLog)
  476. )
  477. for i, cnt := range s.count[:s.symbolLen] {
  478. // already handled
  479. // if (count[s] == s.length) return 0; /* rle special case */
  480. if cnt == 0 {
  481. s.norm[i] = 0
  482. continue
  483. }
  484. if cnt <= lowThreshold {
  485. s.norm[i] = -1
  486. stillToDistribute--
  487. } else {
  488. proba := (int16)((uint64(cnt) * step) >> scale)
  489. if proba < 8 {
  490. restToBeat := vStep * uint64(rtbTable[proba])
  491. v := uint64(cnt)*step - (uint64(proba) << scale)
  492. if v > restToBeat {
  493. proba++
  494. }
  495. }
  496. if proba > largestP {
  497. largestP = proba
  498. largest = i
  499. }
  500. s.norm[i] = proba
  501. stillToDistribute -= proba
  502. }
  503. }
  504. if -stillToDistribute >= (s.norm[largest] >> 1) {
  505. // corner case, need another normalization method
  506. return s.normalizeCount2()
  507. }
  508. s.norm[largest] += stillToDistribute
  509. return nil
  510. }
  511. // Secondary normalization method.
  512. // To be used when primary method fails.
  513. func (s *Scratch) normalizeCount2() error {
  514. const notYetAssigned = -2
  515. var (
  516. distributed uint32
  517. total = uint32(s.br.remain())
  518. tableLog = s.actualTableLog
  519. lowThreshold = total >> tableLog
  520. lowOne = (total * 3) >> (tableLog + 1)
  521. )
  522. for i, cnt := range s.count[:s.symbolLen] {
  523. if cnt == 0 {
  524. s.norm[i] = 0
  525. continue
  526. }
  527. if cnt <= lowThreshold {
  528. s.norm[i] = -1
  529. distributed++
  530. total -= cnt
  531. continue
  532. }
  533. if cnt <= lowOne {
  534. s.norm[i] = 1
  535. distributed++
  536. total -= cnt
  537. continue
  538. }
  539. s.norm[i] = notYetAssigned
  540. }
  541. toDistribute := (1 << tableLog) - distributed
  542. if (total / toDistribute) > lowOne {
  543. // risk of rounding to zero
  544. lowOne = (total * 3) / (toDistribute * 2)
  545. for i, cnt := range s.count[:s.symbolLen] {
  546. if (s.norm[i] == notYetAssigned) && (cnt <= lowOne) {
  547. s.norm[i] = 1
  548. distributed++
  549. total -= cnt
  550. continue
  551. }
  552. }
  553. toDistribute = (1 << tableLog) - distributed
  554. }
  555. if distributed == uint32(s.symbolLen)+1 {
  556. // all values are pretty poor;
  557. // probably incompressible data (should have already been detected);
  558. // find max, then give all remaining points to max
  559. var maxV int
  560. var maxC uint32
  561. for i, cnt := range s.count[:s.symbolLen] {
  562. if cnt > maxC {
  563. maxV = i
  564. maxC = cnt
  565. }
  566. }
  567. s.norm[maxV] += int16(toDistribute)
  568. return nil
  569. }
  570. if total == 0 {
  571. // all of the symbols were low enough for the lowOne or lowThreshold
  572. for i := uint32(0); toDistribute > 0; i = (i + 1) % (uint32(s.symbolLen)) {
  573. if s.norm[i] > 0 {
  574. toDistribute--
  575. s.norm[i]++
  576. }
  577. }
  578. return nil
  579. }
  580. var (
  581. vStepLog = 62 - uint64(tableLog)
  582. mid = uint64((1 << (vStepLog - 1)) - 1)
  583. rStep = (((1 << vStepLog) * uint64(toDistribute)) + mid) / uint64(total) // scale on remaining
  584. tmpTotal = mid
  585. )
  586. for i, cnt := range s.count[:s.symbolLen] {
  587. if s.norm[i] == notYetAssigned {
  588. var (
  589. end = tmpTotal + uint64(cnt)*rStep
  590. sStart = uint32(tmpTotal >> vStepLog)
  591. sEnd = uint32(end >> vStepLog)
  592. weight = sEnd - sStart
  593. )
  594. if weight < 1 {
  595. return errors.New("weight < 1")
  596. }
  597. s.norm[i] = int16(weight)
  598. tmpTotal = end
  599. }
  600. }
  601. return nil
  602. }
  603. // validateNorm validates the normalized histogram table.
  604. func (s *Scratch) validateNorm() (err error) {
  605. var total int
  606. for _, v := range s.norm[:s.symbolLen] {
  607. if v >= 0 {
  608. total += int(v)
  609. } else {
  610. total -= int(v)
  611. }
  612. }
  613. defer func() {
  614. if err == nil {
  615. return
  616. }
  617. fmt.Printf("selected TableLog: %d, Symbol length: %d\n", s.actualTableLog, s.symbolLen)
  618. for i, v := range s.norm[:s.symbolLen] {
  619. fmt.Printf("%3d: %5d -> %4d \n", i, s.count[i], v)
  620. }
  621. }()
  622. if total != (1 << s.actualTableLog) {
  623. return fmt.Errorf("warning: Total == %d != %d", total, 1<<s.actualTableLog)
  624. }
  625. for i, v := range s.count[s.symbolLen:] {
  626. if v != 0 {
  627. return fmt.Errorf("warning: Found symbol out of range, %d after cut", i)
  628. }
  629. }
  630. return nil
  631. }