compress.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. _ = s.count // Assert that s != nil to speed up the following loop.
  317. for _, v := range in {
  318. s.count[v]++
  319. }
  320. m := uint32(0)
  321. if len(s.prevTable) > 0 {
  322. for i, v := range s.count[:] {
  323. if v == 0 {
  324. continue
  325. }
  326. if v > m {
  327. m = v
  328. }
  329. s.symbolLen = uint16(i) + 1
  330. if i >= len(s.prevTable) {
  331. reuse = false
  332. } else if s.prevTable[i].nBits == 0 {
  333. reuse = false
  334. }
  335. }
  336. return int(m), reuse
  337. }
  338. for i, v := range s.count[:] {
  339. if v == 0 {
  340. continue
  341. }
  342. if v > m {
  343. m = v
  344. }
  345. s.symbolLen = uint16(i) + 1
  346. }
  347. return int(m), false
  348. }
  349. func (s *Scratch) canUseTable(c cTable) bool {
  350. if len(c) < int(s.symbolLen) {
  351. return false
  352. }
  353. for i, v := range s.count[:s.symbolLen] {
  354. if v != 0 && c[i].nBits == 0 {
  355. return false
  356. }
  357. }
  358. return true
  359. }
  360. //lint:ignore U1000 used for debugging
  361. func (s *Scratch) validateTable(c cTable) bool {
  362. if len(c) < int(s.symbolLen) {
  363. return false
  364. }
  365. for i, v := range s.count[:s.symbolLen] {
  366. if v != 0 {
  367. if c[i].nBits == 0 {
  368. return false
  369. }
  370. if c[i].nBits > s.actualTableLog {
  371. return false
  372. }
  373. }
  374. }
  375. return true
  376. }
  377. // minTableLog provides the minimum logSize to safely represent a distribution.
  378. func (s *Scratch) minTableLog() uint8 {
  379. minBitsSrc := highBit32(uint32(s.srcLen)) + 1
  380. minBitsSymbols := highBit32(uint32(s.symbolLen-1)) + 2
  381. if minBitsSrc < minBitsSymbols {
  382. return uint8(minBitsSrc)
  383. }
  384. return uint8(minBitsSymbols)
  385. }
  386. // optimalTableLog calculates and sets the optimal tableLog in s.actualTableLog
  387. func (s *Scratch) optimalTableLog() {
  388. tableLog := s.TableLog
  389. minBits := s.minTableLog()
  390. maxBitsSrc := uint8(highBit32(uint32(s.srcLen-1))) - 1
  391. if maxBitsSrc < tableLog {
  392. // Accuracy can be reduced
  393. tableLog = maxBitsSrc
  394. }
  395. if minBits > tableLog {
  396. tableLog = minBits
  397. }
  398. // Need a minimum to safely represent all symbol values
  399. if tableLog < minTablelog {
  400. tableLog = minTablelog
  401. }
  402. if tableLog > tableLogMax {
  403. tableLog = tableLogMax
  404. }
  405. s.actualTableLog = tableLog
  406. }
  407. type cTableEntry struct {
  408. val uint16
  409. nBits uint8
  410. // We have 8 bits extra
  411. }
  412. const huffNodesMask = huffNodesLen - 1
  413. func (s *Scratch) buildCTable() error {
  414. s.optimalTableLog()
  415. s.huffSort()
  416. if cap(s.cTable) < maxSymbolValue+1 {
  417. s.cTable = make([]cTableEntry, s.symbolLen, maxSymbolValue+1)
  418. } else {
  419. s.cTable = s.cTable[:s.symbolLen]
  420. for i := range s.cTable {
  421. s.cTable[i] = cTableEntry{}
  422. }
  423. }
  424. var startNode = int16(s.symbolLen)
  425. nonNullRank := s.symbolLen - 1
  426. nodeNb := startNode
  427. huffNode := s.nodes[1 : huffNodesLen+1]
  428. // This overlays the slice above, but allows "-1" index lookups.
  429. // Different from reference implementation.
  430. huffNode0 := s.nodes[0 : huffNodesLen+1]
  431. for huffNode[nonNullRank].count() == 0 {
  432. nonNullRank--
  433. }
  434. lowS := int16(nonNullRank)
  435. nodeRoot := nodeNb + lowS - 1
  436. lowN := nodeNb
  437. huffNode[nodeNb].setCount(huffNode[lowS].count() + huffNode[lowS-1].count())
  438. huffNode[lowS].setParent(nodeNb)
  439. huffNode[lowS-1].setParent(nodeNb)
  440. nodeNb++
  441. lowS -= 2
  442. for n := nodeNb; n <= nodeRoot; n++ {
  443. huffNode[n].setCount(1 << 30)
  444. }
  445. // fake entry, strong barrier
  446. huffNode0[0].setCount(1 << 31)
  447. // create parents
  448. for nodeNb <= nodeRoot {
  449. var n1, n2 int16
  450. if huffNode0[lowS+1].count() < huffNode0[lowN+1].count() {
  451. n1 = lowS
  452. lowS--
  453. } else {
  454. n1 = lowN
  455. lowN++
  456. }
  457. if huffNode0[lowS+1].count() < huffNode0[lowN+1].count() {
  458. n2 = lowS
  459. lowS--
  460. } else {
  461. n2 = lowN
  462. lowN++
  463. }
  464. huffNode[nodeNb].setCount(huffNode0[n1+1].count() + huffNode0[n2+1].count())
  465. huffNode0[n1+1].setParent(nodeNb)
  466. huffNode0[n2+1].setParent(nodeNb)
  467. nodeNb++
  468. }
  469. // distribute weights (unlimited tree height)
  470. huffNode[nodeRoot].setNbBits(0)
  471. for n := nodeRoot - 1; n >= startNode; n-- {
  472. huffNode[n].setNbBits(huffNode[huffNode[n].parent()].nbBits() + 1)
  473. }
  474. for n := uint16(0); n <= nonNullRank; n++ {
  475. huffNode[n].setNbBits(huffNode[huffNode[n].parent()].nbBits() + 1)
  476. }
  477. s.actualTableLog = s.setMaxHeight(int(nonNullRank))
  478. maxNbBits := s.actualTableLog
  479. // fill result into tree (val, nbBits)
  480. if maxNbBits > tableLogMax {
  481. return fmt.Errorf("internal error: maxNbBits (%d) > tableLogMax (%d)", maxNbBits, tableLogMax)
  482. }
  483. var nbPerRank [tableLogMax + 1]uint16
  484. var valPerRank [16]uint16
  485. for _, v := range huffNode[:nonNullRank+1] {
  486. nbPerRank[v.nbBits()]++
  487. }
  488. // determine stating value per rank
  489. {
  490. min := uint16(0)
  491. for n := maxNbBits; n > 0; n-- {
  492. // get starting value within each rank
  493. valPerRank[n] = min
  494. min += nbPerRank[n]
  495. min >>= 1
  496. }
  497. }
  498. // push nbBits per symbol, symbol order
  499. for _, v := range huffNode[:nonNullRank+1] {
  500. s.cTable[v.symbol()].nBits = v.nbBits()
  501. }
  502. // assign value within rank, symbol order
  503. t := s.cTable[:s.symbolLen]
  504. for n, val := range t {
  505. nbits := val.nBits & 15
  506. v := valPerRank[nbits]
  507. t[n].val = v
  508. valPerRank[nbits] = v + 1
  509. }
  510. return nil
  511. }
  512. // huffSort will sort symbols, decreasing order.
  513. func (s *Scratch) huffSort() {
  514. type rankPos struct {
  515. base uint32
  516. current uint32
  517. }
  518. // Clear nodes
  519. nodes := s.nodes[:huffNodesLen+1]
  520. s.nodes = nodes
  521. nodes = nodes[1 : huffNodesLen+1]
  522. // Sort into buckets based on length of symbol count.
  523. var rank [32]rankPos
  524. for _, v := range s.count[:s.symbolLen] {
  525. r := highBit32(v+1) & 31
  526. rank[r].base++
  527. }
  528. // maxBitLength is log2(BlockSizeMax) + 1
  529. const maxBitLength = 18 + 1
  530. for n := maxBitLength; n > 0; n-- {
  531. rank[n-1].base += rank[n].base
  532. }
  533. for n := range rank[:maxBitLength] {
  534. rank[n].current = rank[n].base
  535. }
  536. for n, c := range s.count[:s.symbolLen] {
  537. r := (highBit32(c+1) + 1) & 31
  538. pos := rank[r].current
  539. rank[r].current++
  540. prev := nodes[(pos-1)&huffNodesMask]
  541. for pos > rank[r].base && c > prev.count() {
  542. nodes[pos&huffNodesMask] = prev
  543. pos--
  544. prev = nodes[(pos-1)&huffNodesMask]
  545. }
  546. nodes[pos&huffNodesMask] = makeNodeElt(c, byte(n))
  547. }
  548. }
  549. func (s *Scratch) setMaxHeight(lastNonNull int) uint8 {
  550. maxNbBits := s.actualTableLog
  551. huffNode := s.nodes[1 : huffNodesLen+1]
  552. //huffNode = huffNode[: huffNodesLen]
  553. largestBits := huffNode[lastNonNull].nbBits()
  554. // early exit : no elt > maxNbBits
  555. if largestBits <= maxNbBits {
  556. return largestBits
  557. }
  558. totalCost := int(0)
  559. baseCost := int(1) << (largestBits - maxNbBits)
  560. n := uint32(lastNonNull)
  561. for huffNode[n].nbBits() > maxNbBits {
  562. totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits()))
  563. huffNode[n].setNbBits(maxNbBits)
  564. n--
  565. }
  566. // n stops at huffNode[n].nbBits <= maxNbBits
  567. for huffNode[n].nbBits() == maxNbBits {
  568. n--
  569. }
  570. // n end at index of smallest symbol using < maxNbBits
  571. // renorm totalCost
  572. totalCost >>= largestBits - maxNbBits /* note : totalCost is necessarily a multiple of baseCost */
  573. // repay normalized cost
  574. {
  575. const noSymbol = 0xF0F0F0F0
  576. var rankLast [tableLogMax + 2]uint32
  577. for i := range rankLast[:] {
  578. rankLast[i] = noSymbol
  579. }
  580. // Get pos of last (smallest) symbol per rank
  581. {
  582. currentNbBits := maxNbBits
  583. for pos := int(n); pos >= 0; pos-- {
  584. if huffNode[pos].nbBits() >= currentNbBits {
  585. continue
  586. }
  587. currentNbBits = huffNode[pos].nbBits() // < maxNbBits
  588. rankLast[maxNbBits-currentNbBits] = uint32(pos)
  589. }
  590. }
  591. for totalCost > 0 {
  592. nBitsToDecrease := uint8(highBit32(uint32(totalCost))) + 1
  593. for ; nBitsToDecrease > 1; nBitsToDecrease-- {
  594. highPos := rankLast[nBitsToDecrease]
  595. lowPos := rankLast[nBitsToDecrease-1]
  596. if highPos == noSymbol {
  597. continue
  598. }
  599. if lowPos == noSymbol {
  600. break
  601. }
  602. highTotal := huffNode[highPos].count()
  603. lowTotal := 2 * huffNode[lowPos].count()
  604. if highTotal <= lowTotal {
  605. break
  606. }
  607. }
  608. // only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !)
  609. // HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary
  610. // FIXME: try to remove
  611. for (nBitsToDecrease <= tableLogMax) && (rankLast[nBitsToDecrease] == noSymbol) {
  612. nBitsToDecrease++
  613. }
  614. totalCost -= 1 << (nBitsToDecrease - 1)
  615. if rankLast[nBitsToDecrease-1] == noSymbol {
  616. // this rank is no longer empty
  617. rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]
  618. }
  619. huffNode[rankLast[nBitsToDecrease]].setNbBits(1 +
  620. huffNode[rankLast[nBitsToDecrease]].nbBits())
  621. if rankLast[nBitsToDecrease] == 0 {
  622. /* special case, reached largest symbol */
  623. rankLast[nBitsToDecrease] = noSymbol
  624. } else {
  625. rankLast[nBitsToDecrease]--
  626. if huffNode[rankLast[nBitsToDecrease]].nbBits() != maxNbBits-nBitsToDecrease {
  627. rankLast[nBitsToDecrease] = noSymbol /* this rank is now empty */
  628. }
  629. }
  630. }
  631. for totalCost < 0 { /* Sometimes, cost correction overshoot */
  632. if rankLast[1] == noSymbol { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
  633. for huffNode[n].nbBits() == maxNbBits {
  634. n--
  635. }
  636. huffNode[n+1].setNbBits(huffNode[n+1].nbBits() - 1)
  637. rankLast[1] = n + 1
  638. totalCost++
  639. continue
  640. }
  641. huffNode[rankLast[1]+1].setNbBits(huffNode[rankLast[1]+1].nbBits() - 1)
  642. rankLast[1]++
  643. totalCost++
  644. }
  645. }
  646. return maxNbBits
  647. }
  648. // A nodeElt is the fields
  649. //
  650. // count uint32
  651. // parent uint16
  652. // symbol byte
  653. // nbBits uint8
  654. //
  655. // in some order, all squashed into an integer so that the compiler
  656. // always loads and stores entire nodeElts instead of separate fields.
  657. type nodeElt uint64
  658. func makeNodeElt(count uint32, symbol byte) nodeElt {
  659. return nodeElt(count) | nodeElt(symbol)<<48
  660. }
  661. func (e *nodeElt) count() uint32 { return uint32(*e) }
  662. func (e *nodeElt) parent() uint16 { return uint16(*e >> 32) }
  663. func (e *nodeElt) symbol() byte { return byte(*e >> 48) }
  664. func (e *nodeElt) nbBits() uint8 { return uint8(*e >> 56) }
  665. func (e *nodeElt) setCount(c uint32) { *e = (*e)&0xffffffff00000000 | nodeElt(c) }
  666. func (e *nodeElt) setParent(p int16) { *e = (*e)&0xffff0000ffffffff | nodeElt(uint16(p))<<32 }
  667. func (e *nodeElt) setNbBits(n uint8) { *e = (*e)&0x00ffffffffffffff | nodeElt(n)<<56 }