| 100 | // ---------------------------------------------------------------------------------------- |
| 101 | |
| 102 | void entropy_encoder_kernel_1:: |
| 103 | encode ( |
| 104 | uint32 low_count, |
| 105 | uint32 high_count, |
| 106 | uint32 total |
| 107 | ) |
| 108 | { |
| 109 | // note that we must add one because of the convention that |
| 110 | // high == the real upper range minus 1 |
| 111 | uint32 r = (high-low+1)/total; |
| 112 | |
| 113 | // note that we must subtract 1 to preserve the convention that |
| 114 | // high == the real upper range - 1 |
| 115 | high = low + r*high_count-1; |
| 116 | low = low + r*low_count; |
| 117 | |
| 118 | |
| 119 | while (true) |
| 120 | { |
| 121 | |
| 122 | // if the highest order bit in high and low is the same |
| 123 | if ( low >= 0x80000000 || high < 0x80000000) |
| 124 | { |
| 125 | // if buf is full then write it out |
| 126 | if (buf_used == 8) |
| 127 | { |
| 128 | if (streambuf->sputn(reinterpret_cast<char*>(&buf),1)==0) |
| 129 | { |
| 130 | throw std::ios_base::failure("error occurred in the entropy_encoder object"); |
| 131 | } |
| 132 | buf = 0; |
| 133 | buf_used = 0; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | // write the high order bit from low into buf |
| 138 | buf <<= 1; |
| 139 | ++buf_used; |
| 140 | if (low&0x80000000) |
| 141 | buf |= 0x1; |
| 142 | |
| 143 | // roll off the bit we just wrote to buf |
| 144 | low <<= 1; |
| 145 | high <<= 1; |
| 146 | high |= 1; // note that it is ok to add one to high here because |
| 147 | // of the convention that high == real upper range - 1. |
| 148 | // so that means that if we want to shift the upper range |
| 149 | // left by one then we must shift a one into high also |
| 150 | // since real upper range == high + 0.999999999... |
| 151 | |
| 152 | // make sure low is never zero |
| 153 | if (low == 0) |
| 154 | low = 1; |
| 155 | } |
| 156 | // if the distance between high and low is small and there aren't |
| 157 | // any bits we can roll off then round low up or high down. |
| 158 | else if (high-low < 0x10000) |
| 159 | { |
nothing calls this directly
no outgoing calls
no test coverage detected