compress.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package huff0
  2. import (
  3. "fmt"
  4. "math"
  5. "runtime"
  6. "sync"
  7. )
  8. // Compress1X will compress the input.
  9. // The output can be decoded using Decompress1X.
  10. // Supply a Scratch object. The scratch object contains state about re-use,
  11. // So when sharing across independent encodes, be sure to set the re-use policy.
  12. func Compress1X(in []byte, s *Scratch) (out []byte, reUsed bool, err error) {
  13. s, err = s.prepare(in)
  14. if err != nil {
  15. return nil, false, err
  16. }
  17. return compress(in, s, s.compress1X)
  18. }
  19. // Compress4X will compress the input. The input is split into 4 independent blocks
  20. // and compressed similar to Compress1X.
  21. // The output can be decoded using Decompress4X.
  22. // Supply a Scratch object. The scratch object contains state about re-use,
  23. // So when sharing across independent encodes, be sure to set the re-use policy.
  24. func Compress4X(in []byte, s *Scratch) (out []byte, reUsed bool, err error) {
  25. s, err = s.prepare(in)
  26. if err != nil {
  27. return nil, false, err
  28. }
  29. if false {
  30. // TODO: compress4Xp only slightly faster.
  31. const parallelThreshold = 8 << 10
  32. if len(in) < parallelThreshold || runtime.GOMAXPROCS(0) == 1 {
  33. return compress(in, s, s.compress4X)
  34. }
  35. return compress(in, s, s.compress4Xp)
  36. }
  37. return compress(in, s, s.compress4X)
  38. }
  39. func compress(in []byte, s *Scratch, compressor func(src []byte) ([]byte, error)) (out []byte, reUsed bool, err error) {
  40. // Nuke previous table if we cannot reuse anyway.
  41. if s.Reuse == ReusePolicyNone {
  42. s.prevTable = s.prevTable[:0]
  43. }
  44. // Create histogram, if none was provided.
  45. maxCount := s.maxCount
  46. var canReuse = false
  47. if maxCount == 0 {
  48. maxCount, canReuse = s.countSimple(in)
  49. } else {
  50. canReuse = s.canUseTable(s.prevTable)
  51. }
  52. // We want the output size to be less than this:
  53. wantSize := len(in)
  54. if s.WantLogLess > 0 {
  55. wantSize -= wantSize >> s.WantLogLess
  56. }
  57. // Reset for next run.
  58. s.clearCount = true
  59. s.maxCount = 0
  60. if maxCount >= len(in) {
  61. if maxCount > len(in) {
  62. return nil, false, fmt.Errorf("maxCount (%d) > length (%d)", maxCount, len(in))
  63. }
  64. if len(in) == 1 {
  65. return nil, false, ErrIncompressible
  66. }
  67. // One symbol, use RLE
  68. return nil, false, ErrUseRLE
  69. }
  70. if maxCount == 1 || maxCount < (len(in)>>7) {
  71. // Each symbol present maximum once or too well distributed.
  72. return nil, false, ErrIncompressible
  73. }
  74. if s.Reuse == ReusePolicyMust && !canReuse {
  75. // We must reuse, but we can't.
  76. return nil, false, ErrIncompressible
  77. }
  78. if (s.Reuse == ReusePolicyPrefer || s.Reuse == ReusePolicyMust) && canReuse {
  79. keepTable := s.cTable
  80. keepTL := s.actualTableLog
  81. s.cTable = s.prevTable
  82. s.actualTableLog = s.prevTableLog
  83. s.Out, err = compressor(in)
  84. s.cTable = keepTable
  85. s.actualTableLog = keepTL
  86. if err == nil && len(s.Out) < wantSize {
  87. s.OutData = s.Out
  88. return s.Out, true, nil
  89. }
  90. if s.Reuse == ReusePolicyMust {
  91. return nil, false, ErrIncompressible
  92. }
  93. // Do not attempt to re-use later.
  94. s.prevTable = s.prevTable[:0]
  95. }
  96. // Calculate new table.
  97. err = s.buildCTable()
  98. if err != nil {
  99. return nil, false, err
  100. }
  101. if false && !s.canUseTable(s.cTable) {
  102. panic("invalid table generated")
  103. }
  104. if s.Reuse == ReusePolicyAllow && canReuse {
  105. hSize := len(s.Out)
  106. oldSize := s.prevTable.estimateSize(s.count[:s.symbolLen])
  107. newSize := s.cTable.estimateSize(s.count[:s.symbolLen])
  108. if oldSize <= hSize+newSize || hSize+12 >= wantSize {
  109. // Retain cTable even if we re-use.
  110. keepTable := s.cTable
  111. keepTL := s.actualTableLog
  112. s.cTable = s.prevTable
  113. s.actualTableLog = s.prevTableLog
  114. s.Out, err = compressor(in)
  115. // Restore ctable.
  116. s.cTable = keepTable
  117. s.actualTableLog = keepTL
  118. if err != nil {
  119. return nil, false, err
  120. }
  121. if len(s.Out) >= wantSize {
  122. return nil, false, ErrIncompressible
  123. }
  124. s.OutData = s.Out
  125. return s.Out, true, nil
  126. }
  127. }
  128. // Use new table
  129. err = s.cTable.write(s)
  130. if err != nil {
  131. s.OutTable = nil
  132. return nil, false, err
  133. }
  134. s.OutTable = s.Out
  135. // Compress using new table
  136. s.Out, err = compressor(in)
  137. if err != nil {
  138. s.OutTable = nil
  139. return nil, false, err
  140. }
  141. if len(s.Out) >= wantSize {
  142. s.OutTable = nil
  143. return nil, false, ErrIncompressible
  144. }
  145. // Move current table into previous.
  146. s.prevTable, s.prevTableLog, s.cTable = s.cTable, s.actualTableLog, s.prevTable[:0]
  147. s.OutData = s.Out[len(s.OutTable):]
  148. return s.Out, false, nil
  149. }
  150. // EstimateSizes will estimate the data sizes
  151. func EstimateSizes(in []byte, s *Scratch) (tableSz, dataSz, reuseSz int, err error) {
  152. s, err = s.prepare(in)
  153. if err != nil {
  154. return 0, 0, 0, err
  155. }
  156. // Create histogram, if none was provided.
  157. tableSz, dataSz, reuseSz = -1, -1, -1
  158. maxCount := s.maxCount
  159. var canReuse = false
  160. if maxCount == 0 {
  161. maxCount, canReuse = s.countSimple(in)
  162. } else {
  163. canReuse = s.canUseTable(s.prevTable)
  164. }
  165. // We want the output size to be less than this:
  166. wantSize := len(in)
  167. if s.WantLogLess > 0 {
  168. wantSize -= wantSize >> s.WantLogLess
  169. }
  170. // Reset for next run.
  171. s.clearCount = true
  172. s.maxCount = 0
  173. if maxCount >= len(in) {
  174. if maxCount > len(in) {
  175. return 0, 0, 0, fmt.Errorf("maxCount (%d) > length (%d)", maxCount, len(in))
  176. }
  177. if len(in) == 1 {
  178. return 0, 0, 0, ErrIncompressible
  179. }
  180. // One symbol, use RLE
  181. return 0, 0, 0, ErrUseRLE
  182. }
  183. if maxCount == 1 || maxCount < (len(in)>>7) {
  184. // Each symbol present maximum once or too well distributed.
  185. return 0, 0, 0, ErrIncompressible
  186. }
  187. // Calculate new table.
  188. err = s.buildCTable()
  189. if err != nil {
  190. return 0, 0, 0, err
  191. }
  192. if false && !s.canUseTable(s.cTable) {
  193. panic("invalid table generated")
  194. }
  195. tableSz, err = s.cTable.estTableSize(s)
  196. if err != nil {
  197. return 0, 0, 0, err
  198. }
  199. if canReuse {
  200. reuseSz = s.prevTable.estimateSize(s.count[:s.symbolLen])
  201. }
  202. dataSz = s.cTable.estimateSize(s.count[:s.symbolLen])
  203. // Restore
  204. return tableSz, dataSz, reuseSz, nil
  205. }
  206. func (s *Scratch) compress1X(src []byte) ([]byte, error) {
  207. return s.compress1xDo(s.Out, src), nil
  208. }
  209. func (s *Scratch) compress1xDo(dst, src []byte) []byte {
  210. var bw = bitWriter{out: dst}
  211. // N is length divisible by 4.
  212. n := len(src)
  213. n -= n & 3
  214. cTable := s.cTable[:256]
  215. // Encode last bytes.
  216. for i := len(src) & 3; i > 0; i-- {
  217. bw.encSymbol(cTable, src[n+i-1])
  218. }
  219. n -= 4
  220. if s.actualTableLog <= 8 {
  221. for ; n >= 0; n -= 4 {
  222. tmp := src[n : n+4]
  223. // tmp should be len 4
  224. bw.flush32()
  225. bw.encFourSymbols(cTable[tmp[3]], cTable[tmp[2]], cTable[tmp[1]], cTable[tmp[0]])
  226. }
  227. } else {
  228. for ; n >= 0; n -= 4 {
  229. tmp := src[n : n+4]
  230. // tmp should be len 4
  231. bw.flush32()
  232. bw.encTwoSymbols(cTable, tmp[3], tmp[2])
  233. bw.flush32()
  234. bw.encTwoSymbols(cTable, tmp[1], tmp[0])
  235. }
  236. }
  237. bw.close()
  238. return bw.out
  239. }
  240. var sixZeros [6]byte
  241. func (s *Scratch) compress4X(src []byte) ([]byte, error) {
  242. if len(src) < 12 {
  243. return nil, ErrIncompressible
  244. }
  245. segmentSize := (len(src) + 3) / 4
  246. // Add placeholder for output length
  247. offsetIdx := len(s.Out)
  248. s.Out = append(s.Out, sixZeros[:]...)
  249. for i := 0; i < 4; i++ {
  250. toDo := src
  251. if len(toDo) > segmentSize {
  252. toDo = toDo[:segmentSize]
  253. }
  254. src = src[len(toDo):]
  255. idx := len(s.Out)
  256. s.Out = s.compress1xDo(s.Out, toDo)
  257. if len(s.Out)-idx > math.MaxUint16 {
  258. // We cannot store the size in the jump table
  259. return nil, ErrIncompressible
  260. }
  261. // Write compressed length as little endian before block.
  262. if i < 3 {
  263. // Last length is not written.
  264. length := len(s.Out) - idx
  265. s.Out[i*2+offsetIdx] = byte(length)
  266. s.Out[i*2+offsetIdx+1] = byte(length >> 8)
  267. }
  268. }
  269. return s.Out, nil
  270. }
  271. // compress4Xp will compress 4 streams using separate goroutines.
  272. func (s *Scratch) compress4Xp(src []byte) ([]byte, error) {
  273. if len(src) < 12 {
  274. return nil, ErrIncompressible
  275. }
  276. // Add placeholder for output length
  277. s.Out = s.Out[:6]
  278. segmentSize := (len(src) + 3) / 4
  279. var wg sync.WaitGroup
  280. wg.Add(4)
  281. for i := 0; i < 4; i++ {
  282. toDo := src
  283. if len(toDo) > segmentSize {
  284. toDo = toDo[:segmentSize]
  285. }
  286. src = src[len(toDo):]
  287. // Separate goroutine for each block.
  288. go func(i int) {
  289. s.tmpOut[i] = s.compress1xDo(s.tmpOut[i][:0], toDo)
  290. wg.Done()
  291. }(i)
  292. }
  293. wg.Wait()
  294. for i := 0; i < 4; i++ {
  295. o := s.tmpOut[i]
  296. if len(o) > math.MaxUint16 {
  297. // We cannot store the size in the jump table
  298. return nil, ErrIncompressible
  299. }
  300. // Write compressed length as little endian before block.
  301. if i < 3 {
  302. // Last length is not written.
  303. s.Out[i*2] = byte(len(o))
  304. s.Out[i*2+1] = byte(len(o) >> 8)
  305. }
  306. // Write output.
  307. s.Out = append(s.Out, o...)
  308. }
  309. return s.Out, nil
  310. }
  311. // countSimple will create a simple histogram in s.count.
  312. // Returns the biggest count.
  313. // Does not update s.clearCount.
  314. func (s *Scratch) countSimple(in []byte) (max int, reuse bool) {
  315. reuse = true
  316. for _, v := range in {
  317. s.count[v]++
  318. }
  319. m := uint32(0)
  320. if len(s.prevTable) > 0 {
  321. for i, v := range s.count[:] {
  322. if v == 0 {
  323. continue
  324. }
  325. if v > m {
  326. m = v
  327. }
  328. s.symbolLen = uint16(i) + 1
  329. if i >= len(s.prevTable) {
  330. reuse = false
  331. } else if s.prevTable[i].nBits == 0 {
  332. reuse = false
  333. }
  334. }
  335. return int(m), reuse
  336. }
  337. for i, v := range s.count[:] {
  338. if v == 0 {
  339. continue
  340. }
  341. if v > m {
  342. m = v
  343. }
  344. s.symbolLen = uint16(i) + 1
  345. }
  346. return int(m), false
  347. }
  348. func (s *Scratch) canUseTable(c cTable) bool {
  349. if len(c) < int(s.symbolLen) {
  350. return false
  351. }
  352. for i, v := range s.count[:s.symbolLen] {
  353. if v != 0 && c[i].nBits == 0 {
  354. return false
  355. }
  356. }
  357. return true
  358. }
  359. //lint:ignore U1000 used for debugging
  360. func (s *Scratch) validateTable(c cTable) bool {
  361. if len(c) < int(s.symbolLen) {
  362. return false
  363. }
  364. for i, v := range s.count[:s.symbolLen] {
  365. if v != 0 {
  366. if c[i].nBits == 0 {
  367. return false
  368. }
  369. if c[i].nBits > s.actualTableLog {
  370. return false
  371. }
  372. }
  373. }
  374. return true
  375. }
  376. // minTableLog provides the minimum logSize to safely represent a distribution.
  377. func (s *Scratch) minTableLog() uint8 {
  378. minBitsSrc := highBit32(uint32(s.br.remain())) + 1
  379. minBitsSymbols := highBit32(uint32(s.symbolLen-1)) + 2
  380. if minBitsSrc < minBitsSymbols {
  381. return uint8(minBitsSrc)
  382. }
  383. return uint8(minBitsSymbols)
  384. }
  385. // optimalTableLog calculates and sets the optimal tableLog in s.actualTableLog
  386. func (s *Scratch) optimalTableLog() {
  387. tableLog := s.TableLog
  388. minBits := s.minTableLog()
  389. maxBitsSrc := uint8(highBit32(uint32(s.br.remain()-1))) - 1
  390. if maxBitsSrc < tableLog {
  391. // Accuracy can be reduced
  392. tableLog = maxBitsSrc
  393. }
  394. if minBits > tableLog {
  395. tableLog = minBits
  396. }
  397. // Need a minimum to safely represent all symbol values
  398. if tableLog < minTablelog {
  399. tableLog = minTablelog
  400. }
  401. if tableLog > tableLogMax {
  402. tableLog = tableLogMax
  403. }
  404. s.actualTableLog = tableLog
  405. }
  406. type cTableEntry struct {
  407. val uint16
  408. nBits uint8
  409. // We have 8 bits extra
  410. }
  411. const huffNodesMask = huffNodesLen - 1
  412. func (s *Scratch) buildCTable() error {
  413. s.optimalTableLog()
  414. s.huffSort()
  415. if cap(s.cTable) < maxSymbolValue+1 {
  416. s.cTable = make([]cTableEntry, s.symbolLen, maxSymbolValue+1)
  417. } else {
  418. s.cTable = s.cTable[:s.symbolLen]
  419. for i := range s.cTable {
  420. s.cTable[i] = cTableEntry{}
  421. }
  422. }
  423. var startNode = int16(s.symbolLen)
  424. nonNullRank := s.symbolLen - 1
  425. nodeNb := startNode
  426. huffNode := s.nodes[1 : huffNodesLen+1]
  427. // This overlays the slice above, but allows "-1" index lookups.
  428. // Different from reference implementation.
  429. huffNode0 := s.nodes[0 : huffNodesLen+1]
  430. for huffNode[nonNullRank].count() == 0 {
  431. nonNullRank--
  432. }
  433. lowS := int16(nonNullRank)
  434. nodeRoot := nodeNb + lowS - 1
  435. lowN := nodeNb
  436. huffNode[nodeNb].setCount(huffNode[lowS].count() + huffNode[lowS-1].count())
  437. huffNode[lowS].setParent(nodeNb)
  438. huffNode[lowS-1].setParent(nodeNb)
  439. nodeNb++
  440. lowS -= 2
  441. for n := nodeNb; n <= nodeRoot; n++ {
  442. huffNode[n].setCount(1 << 30)
  443. }
  444. // fake entry, strong barrier
  445. huffNode0[0].setCount(1 << 31)
  446. // create parents
  447. for nodeNb <= nodeRoot {
  448. var n1, n2 int16
  449. if huffNode0[lowS+1].count() < huffNode0[lowN+1].count() {
  450. n1 = lowS
  451. lowS--
  452. } else {
  453. n1 = lowN
  454. lowN++
  455. }
  456. if huffNode0[lowS+1].count() < huffNode0[lowN+1].count() {
  457. n2 = lowS
  458. lowS--
  459. } else {
  460. n2 = lowN
  461. lowN++
  462. }
  463. huffNode[nodeNb].setCount(huffNode0[n1+1].count() + huffNode0[n2+1].count())
  464. huffNode0[n1+1].setParent(nodeNb)
  465. huffNode0[n2+1].setParent(nodeNb)
  466. nodeNb++
  467. }
  468. // distribute weights (unlimited tree height)
  469. huffNode[nodeRoot].setNbBits(0)
  470. for n := nodeRoot - 1; n >= startNode; n-- {
  471. huffNode[n].setNbBits(huffNode[huffNode[n].parent()].nbBits() + 1)
  472. }
  473. for n := uint16(0); n <= nonNullRank; n++ {
  474. huffNode[n].setNbBits(huffNode[huffNode[n].parent()].nbBits() + 1)
  475. }
  476. s.actualTableLog = s.setMaxHeight(int(nonNullRank))
  477. maxNbBits := s.actualTableLog
  478. // fill result into tree (val, nbBits)
  479. if maxNbBits > tableLogMax {
  480. return fmt.Errorf("internal error: maxNbBits (%d) > tableLogMax (%d)", maxNbBits, tableLogMax)
  481. }
  482. var nbPerRank [tableLogMax + 1]uint16
  483. var valPerRank [16]uint16
  484. for _, v := range huffNode[:nonNullRank+1] {
  485. nbPerRank[v.nbBits()]++
  486. }
  487. // determine stating value per rank
  488. {
  489. min := uint16(0)
  490. for n := maxNbBits; n > 0; n-- {
  491. // get starting value within each rank
  492. valPerRank[n] = min
  493. min += nbPerRank[n]
  494. min >>= 1
  495. }
  496. }
  497. // push nbBits per symbol, symbol order
  498. for _, v := range huffNode[:nonNullRank+1] {
  499. s.cTable[v.symbol()].nBits = v.nbBits()
  500. }
  501. // assign value within rank, symbol order
  502. t := s.cTable[:s.symbolLen]
  503. for n, val := range t {
  504. nbits := val.nBits & 15
  505. v := valPerRank[nbits]
  506. t[n].val = v
  507. valPerRank[nbits] = v + 1
  508. }
  509. return nil
  510. }
  511. // huffSort will sort symbols, decreasing order.
  512. func (s *Scratch) huffSort() {
  513. type rankPos struct {
  514. base uint32
  515. current uint32
  516. }
  517. // Clear nodes
  518. nodes := s.nodes[:huffNodesLen+1]
  519. s.nodes = nodes
  520. nodes = nodes[1 : huffNodesLen+1]
  521. // Sort into buckets based on length of symbol count.
  522. var rank [32]rankPos
  523. for _, v := range s.count[:s.symbolLen] {
  524. r := highBit32(v+1) & 31
  525. rank[r].base++
  526. }
  527. // maxBitLength is log2(BlockSizeMax) + 1
  528. const maxBitLength = 18 + 1
  529. for n := maxBitLength; n > 0; n-- {
  530. rank[n-1].base += rank[n].base
  531. }
  532. for n := range rank[:maxBitLength] {
  533. rank[n].current = rank[n].base
  534. }
  535. for n, c := range s.count[:s.symbolLen] {
  536. r := (highBit32(c+1) + 1) & 31
  537. pos := rank[r].current
  538. rank[r].current++
  539. prev := nodes[(pos-1)&huffNodesMask]
  540. for pos > rank[r].base && c > prev.count() {
  541. nodes[pos&huffNodesMask] = prev
  542. pos--
  543. prev = nodes[(pos-1)&huffNodesMask]
  544. }
  545. nodes[pos&huffNodesMask] = makeNodeElt(c, byte(n))
  546. }
  547. }
  548. func (s *Scratch) setMaxHeight(lastNonNull int) uint8 {
  549. maxNbBits := s.actualTableLog
  550. huffNode := s.nodes[1 : huffNodesLen+1]
  551. //huffNode = huffNode[: huffNodesLen]
  552. largestBits := huffNode[lastNonNull].nbBits()
  553. // early exit : no elt > maxNbBits
  554. if largestBits <= maxNbBits {
  555. return largestBits
  556. }
  557. totalCost := int(0)
  558. baseCost := int(1) << (largestBits - maxNbBits)
  559. n := uint32(lastNonNull)
  560. for huffNode[n].nbBits() > maxNbBits {
  561. totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits()))
  562. huffNode[n].setNbBits(maxNbBits)
  563. n--
  564. }
  565. // n stops at huffNode[n].nbBits <= maxNbBits
  566. for huffNode[n].nbBits() == maxNbBits {
  567. n--
  568. }
  569. // n end at index of smallest symbol using < maxNbBits
  570. // renorm totalCost
  571. totalCost >>= largestBits - maxNbBits /* note : totalCost is necessarily a multiple of baseCost */
  572. // repay normalized cost
  573. {
  574. const noSymbol = 0xF0F0F0F0
  575. var rankLast [tableLogMax + 2]uint32
  576. for i := range rankLast[:] {
  577. rankLast[i] = noSymbol
  578. }
  579. // Get pos of last (smallest) symbol per rank
  580. {
  581. currentNbBits := maxNbBits
  582. for pos := int(n); pos >= 0; pos-- {
  583. if huffNode[pos].nbBits() >= currentNbBits {
  584. continue
  585. }
  586. currentNbBits = huffNode[pos].nbBits() // < maxNbBits
  587. rankLast[maxNbBits-currentNbBits] = uint32(pos)
  588. }
  589. }
  590. for totalCost > 0 {
  591. nBitsToDecrease := uint8(highBit32(uint32(totalCost))) + 1
  592. for ; nBitsToDecrease > 1; nBitsToDecrease-- {
  593. highPos := rankLast[nBitsToDecrease]
  594. lowPos := rankLast[nBitsToDecrease-1]
  595. if highPos == noSymbol {
  596. continue
  597. }
  598. if lowPos == noSymbol {
  599. break
  600. }
  601. highTotal := huffNode[highPos].count()
  602. lowTotal := 2 * huffNode[lowPos].count()
  603. if highTotal <= lowTotal {
  604. break
  605. }
  606. }
  607. // only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !)
  608. // HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary
  609. // FIXME: try to remove
  610. for (nBitsToDecrease <= tableLogMax) && (rankLast[nBitsToDecrease] == noSymbol) {
  611. nBitsToDecrease++
  612. }
  613. totalCost -= 1 << (nBitsToDecrease - 1)
  614. if rankLast[nBitsToDecrease-1] == noSymbol {
  615. // this rank is no longer empty
  616. rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]
  617. }
  618. huffNode[rankLast[nBitsToDecrease]].setNbBits(1 +
  619. huffNode[rankLast[nBitsToDecrease]].nbBits())
  620. if rankLast[nBitsToDecrease] == 0 {
  621. /* special case, reached largest symbol */
  622. rankLast[nBitsToDecrease] = noSymbol
  623. } else {
  624. rankLast[nBitsToDecrease]--
  625. if huffNode[rankLast[nBitsToDecrease]].nbBits() != maxNbBits-nBitsToDecrease {
  626. rankLast[nBitsToDecrease] = noSymbol /* this rank is now empty */
  627. }
  628. }
  629. }
  630. for totalCost < 0 { /* Sometimes, cost correction overshoot */
  631. if rankLast[1] == noSymbol { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
  632. for huffNode[n].nbBits() == maxNbBits {
  633. n--
  634. }
  635. huffNode[n+1].setNbBits(huffNode[n+1].nbBits() - 1)
  636. rankLast[1] = n + 1
  637. totalCost++
  638. continue
  639. }
  640. huffNode[rankLast[1]+1].setNbBits(huffNode[rankLast[1]+1].nbBits() - 1)
  641. rankLast[1]++
  642. totalCost++
  643. }
  644. }
  645. return maxNbBits
  646. }
  647. // A nodeElt is the fields
  648. //
  649. // count uint32
  650. // parent uint16
  651. // symbol byte
  652. // nbBits uint8
  653. //
  654. // in some order, all squashed into an integer so that the compiler
  655. // always loads and stores entire nodeElts instead of separate fields.
  656. type nodeElt uint64
  657. func makeNodeElt(count uint32, symbol byte) nodeElt {
  658. return nodeElt(count) | nodeElt(symbol)<<48
  659. }
  660. func (e *nodeElt) count() uint32 { return uint32(*e) }
  661. func (e *nodeElt) parent() uint16 { return uint16(*e >> 32) }
  662. func (e *nodeElt) symbol() byte { return byte(*e >> 48) }
  663. func (e *nodeElt) nbBits() uint8 { return uint8(*e >> 56) }
  664. func (e *nodeElt) setCount(c uint32) { *e = (*e)&0xffffffff00000000 | nodeElt(c) }
  665. func (e *nodeElt) setParent(p int16) { *e = (*e)&0xffff0000ffffffff | nodeElt(uint16(p))<<32 }
  666. func (e *nodeElt) setNbBits(n uint8) { *e = (*e)&0x00ffffffffffffff | nodeElt(n)<<56 }