mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 16:47:28 +01:00
std/brotli: speed up the encoder at qualities 10 and 11, byte-exactly
- encode_float.wuffs: package-private fast paths for the f32/f64 add, sub, mul, compare and int conversion calls in the encoder's hot loops. Calling std/softfloat is a cross-package public call (a receiver check, never inlined). Each fast path returns exactly the softfloat result and falls back to it for the uncommon cases. script/manual-test-brotli-float.c cross-checks them against hardware float and double arithmetic. - Zopfli update_nodes: compute a length's cost only when its copy code changes, hoist the distance cache values and the continuation byte, and use a table for small int to f32 conversions. - FindBlocks: memoize (block_switch_bitcost + insert_cost) for capped costs and skip the subtraction for costs at or above a per-byte threshold (S(c) = c - min_cost is monotone, so such costs would be capped anyway). - BrotliPopulationCost: per-call memo of each count's term and depth, and skip the zero terms of the entropy sums. - std/softfloat: a 64-bit f32_add_mags. Output is unchanged (mimic tests on gcc and clang, parity sweeps). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UZ978BikvkegL2JUkqoB1e
This commit is contained in:
16 files changed
+2650
-400
No files matched your search
+11
-6
@@ -880,6 +880,10 @@ round trips through the decoder. Its large-window tests cover qualities 3 to
|
||||
9 with lgwin 18 to 30 (and the H35, H55 and H65 hashers) on small files and
|
||||
on a 46 MiB input with repeats at distances of about 20 and 40 MiB, decoded
|
||||
by both this package's decoder and libbrotlidec.
|
||||
`script/manual-test-brotli-float.c` checks the encoder's package-private
|
||||
float fast paths (`encode_float.wuffs`, which spare the hot loops the
|
||||
cross-package `std/softfloat` calls) against the C compiler's hardware float
|
||||
and double arithmetic, for operands biased towards their edge cases.
|
||||
`script/manual-test-brotli-encoder-parity.c` runs a larger grid over every
|
||||
file in `test/data/brotli`, some larger files in `test/data` (up to about
|
||||
400 KiB) and some synthetic inputs (up to 1 MiB). Its `-q=LO-HI` and
|
||||
@@ -1127,12 +1131,13 @@ metablock) for as long as it needs the results.
|
||||
- `workbuf_len` is a worst case (or, at qualities 10 and 11, a practical
|
||||
bound). A future option could take the input length (like the C code's
|
||||
size hint) to give a tighter bound.
|
||||
- Speed. Compressing the 3 MiB `test/data/harvesters.bmp` (with
|
||||
`example/brcat` on x86-64, lgwin 22), the Wuffs encoder takes about 1.2
|
||||
times libbrotlienc's time at quality 5, 1.7 times at quality 9 and 6 times
|
||||
at quality 11, mostly because of the soft float arithmetic in the Zopfli
|
||||
cost model, the block splitter and histogram clustering. Decoding takes
|
||||
about 1.2 times libbrotlidec's time.
|
||||
- Speed. Compressing the 3 MiB `test/data/harvesters.bmp` in one call (on
|
||||
x86-64, lgwin 22), the Wuffs encoder takes about 1.2 times libbrotlienc's
|
||||
time at quality 5, 1.7 times at quality 9, 4 times at quality 10 and 3.5
|
||||
times at quality 11 (on text, 2 to 3 times at qualities 10 and 11), mostly
|
||||
because of the soft float arithmetic in the Zopfli cost model, the block
|
||||
splitter and histogram clustering. Decoding takes about 1.2 times
|
||||
libbrotlidec's time.
|
||||
- Serialized (shared) dictionaries, which only an experimental
|
||||
(`BROTLI_EXPERIMENTAL`) libbrotlienc build supports.
|
||||
- Changing parameters mid-stream.
|
||||
|
||||
@@ -54,8 +54,18 @@ pri struct bit_cost?(
|
||||
// (but log2_cache need not be), so the cache starts empty.
|
||||
log2_valid : array[1024] base.u64,
|
||||
|
||||
// population_cost's per-call memo of its per-symbol values, for
|
||||
// symbol counts less than 256: pc_term[c] and pc_depth[c] are valid
|
||||
// during the call numbered pc_call if pc_valid[c] == pc_call. Call
|
||||
// numbers start at 1, so the zeroed pc_valid starts empty.
|
||||
pc_call : base.u32,
|
||||
pc_valid : array[256] base.u32,
|
||||
|
||||
sf : softfloat.arith,
|
||||
) + (
|
||||
pc_term : array[256] base.u64,
|
||||
pc_depth : array[256] base.u8,
|
||||
|
||||
// log2_cache[v] is FastLog2(v), for 256 <= v < 65536, filled lazily
|
||||
// (see fast_log2_memo). It is only a speed optimization (glibc's log2
|
||||
// is expensive to emulate): the values are computed by the same
|
||||
@@ -166,17 +176,22 @@ pri func bit_cost.bits_entropy!(population: roslice base.u8, size: base.u64) bas
|
||||
q = q[4 ..]
|
||||
}
|
||||
sum ~mod+= p
|
||||
if p <= 1 {
|
||||
// FastLog2(p) is +0, so (p * FastLog2(p)) is +0 and subtracting
|
||||
// it leaves retval (which is never a NaN) unchanged.
|
||||
continue
|
||||
}
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
retval = this.f64_sub(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: p), b: lp))
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
retval = this.f64_add(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.f64_lt(a: retval, b: this.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
@@ -184,6 +199,7 @@ pri func bit_cost.bits_entropy!(population: roslice base.u8, size: base.u64) bas
|
||||
// Mirrors BrotliPopulationCostLiteral, BrotliPopulationCostCommand and
|
||||
// BrotliPopulationCostDistance (FN(BrotliPopulationCost) in bit_cost_inc.h).
|
||||
// histogram is a histogram of data_size symbols (see the layout above).
|
||||
|
||||
pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
var total : base.u64
|
||||
var count : base.u32
|
||||
@@ -209,6 +225,7 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
var sum : base.u64
|
||||
var retval : base.u64
|
||||
var p : base.u64
|
||||
var term : base.u64
|
||||
|
||||
total = this.ld64(s: args.histogram, i: (args.data_size as base.u64) * 4)
|
||||
if total == 0 {
|
||||
@@ -234,7 +251,7 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
}
|
||||
if count == 2 {
|
||||
// kTwoSymbolHistogramCost + (double)total_count_.
|
||||
return this.sf.f64_add(a: 0x4034_0000_0000_0000, b: this.sf.f64_from_u64(a: total))
|
||||
return this.f64_add(a: 0x4034_0000_0000_0000, b: this.f64_from_u64(a: total))
|
||||
}
|
||||
if count == 3 {
|
||||
h0 = this.ld32(s: args.histogram, i: (s[0] as base.u64) * 4)
|
||||
@@ -243,11 +260,11 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
histomax = h0.max(no_less_than: h1.max(no_less_than: h2))
|
||||
// kThreeSymbolHistogramCost + 2 * (histo0 + histo1 + histo2) -
|
||||
// histomax, where the integer expression is uint32_t arithmetic.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
return this.f64_sub(
|
||||
a: this.f64_add(
|
||||
a: 0x403C_0000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* ((h0 ~mod+ h1) ~mod+ h2))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
b: this.f64_from_u32(a: 2 ~mod* ((h0 ~mod+ h1) ~mod+ h2))),
|
||||
b: this.f64_from_u32(a: histomax))
|
||||
}
|
||||
if count == 4 {
|
||||
i = 0
|
||||
@@ -273,28 +290,48 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
histomax = h23.max(no_less_than: histo[0])
|
||||
// kFourSymbolHistogramCost + 3 * h23 + 2 * (histo[0] + histo[1]) -
|
||||
// histomax, evaluated left to right.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: this.sf.f64_add(
|
||||
return this.f64_sub(
|
||||
a: this.f64_add(
|
||||
a: this.f64_add(
|
||||
a: 0x4042_8000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 3 ~mod* h23)),
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* (histo[0] ~mod+ histo[1]))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
b: this.f64_from_u32(a: 3 ~mod* h23)),
|
||||
b: this.f64_from_u32(a: 2 ~mod* (histo[0] ~mod+ histo[1]))),
|
||||
b: this.f64_from_u32(a: histomax))
|
||||
}
|
||||
|
||||
// Compute the entropy of the histogram and simultaneously build a
|
||||
// simplified histogram of the code length codes.
|
||||
max_depth = 1
|
||||
log2total = this.fast_log2_memo!(v: total)
|
||||
this.pc_call ~mod+= 1
|
||||
if this.pc_call == 0 {
|
||||
this.pc_valid[.. 256].bulk_memset!(byte_value: 0)
|
||||
this.pc_call = 1
|
||||
}
|
||||
i = 0
|
||||
while i < args.data_size {
|
||||
c = this.ld32(s: args.histogram, i: (i as base.u64) * 4)
|
||||
if c > 0 {
|
||||
lp = this.fast_log2_memo!(v: c as base.u64)
|
||||
log2p = this.sf.f64_sub(a: log2total, b: lp)
|
||||
depth = this.sf.f64_to_u64_trunc(a: this.sf.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: c), b: log2p))
|
||||
// term is (c * log2p) and depth is (size_t)(log2p + 0.5) (capped
|
||||
// at 15), which only depend on c (and the fixed total).
|
||||
if (c < 256) and (this.pc_valid[c & 255] == this.pc_call) {
|
||||
term = this.pc_term[c & 255]
|
||||
depth = this.pc_depth[c & 255] as base.u64
|
||||
} else {
|
||||
lp = this.fast_log2_memo!(v: c as base.u64)
|
||||
log2p = this.f64_sub(a: log2total, b: lp)
|
||||
depth = this.f64_to_u64_trunc(a: this.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
term = this.f64_mul(a: this.f64_from_u32(a: c), b: log2p)
|
||||
if depth > 15 {
|
||||
depth = 15
|
||||
}
|
||||
if c < 256 {
|
||||
this.pc_valid[c & 255] = this.pc_call
|
||||
this.pc_term[c & 255] = term
|
||||
this.pc_depth[c & 255] = (depth & 15) as base.u8
|
||||
}
|
||||
}
|
||||
bits = this.f64_add(a: bits, b: term)
|
||||
if depth > 15 {
|
||||
depth = 15
|
||||
}
|
||||
@@ -326,14 +363,14 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
while reps > 0 {
|
||||
depth_histo[17] ~mod+= 1
|
||||
// Add the 3 extra bits for the 17 code length code.
|
||||
bits = this.sf.f64_add(a: bits, b: 0x4008_0000_0000_0000)
|
||||
bits = this.f64_add(a: bits, b: 0x4008_0000_0000_0000)
|
||||
reps >>= 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the estimated encoding cost of the code length code histogram.
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_from_u64(a: 18 + (2 * max_depth)))
|
||||
bits = this.f64_add(a: bits, b: this.f64_from_u64(a: 18 + (2 * max_depth)))
|
||||
|
||||
// Add the entropy of the code length code histogram. This is
|
||||
// BrotliBitsEntropy(depth_histo, BROTLI_CODE_LENGTH_CODES), inlined
|
||||
@@ -342,18 +379,22 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
while i < 18 {
|
||||
p = depth_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
i ~mod+= 1
|
||||
if p <= 1 {
|
||||
// As in bits_entropy, this would leave retval unchanged.
|
||||
continue
|
||||
}
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.f64_sub(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: p), b: lp))
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
retval = this.f64_add(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.f64_lt(a: retval, b: this.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.f64_from_u64(a: sum)
|
||||
}
|
||||
return this.sf.f64_add(a: bits, b: retval)
|
||||
return this.f64_add(a: bits, b: retval)
|
||||
}
|
||||
@@ -175,16 +175,16 @@ pri func encoder.bits_entropy_of_histogram() base.u64 {
|
||||
while i < 256 {
|
||||
p = this.histogram[i] as base.u64
|
||||
sum ~mod+= p
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
retval = this.bc.f64_sub(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
i += 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
retval = this.bc.f64_add(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.bc.f64_lt(a: retval, b: this.bc.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.bc.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
|
||||
@@ -295,6 +295,13 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
var c : base.u64
|
||||
var cur_id : base.u8
|
||||
var b : base.u8
|
||||
var nh : base.u64[..= 256]
|
||||
var icn : base.u64
|
||||
var best_k : base.u64
|
||||
var use_capped : base.bool
|
||||
var skip_t : base.u64
|
||||
var t : base.u64
|
||||
var n : base.u32
|
||||
|
||||
alphabet_size = args.ds as base.u64
|
||||
bitmap_len = (args.num_histograms ~mod+ 7) >> 3
|
||||
@@ -330,7 +337,7 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
k = (i ~mod* args.num_histograms) ~mod+ j
|
||||
if k < 35200 {
|
||||
c = this.bit_cost!(count: this.ld32(wb: args.wb, i: (args.histograms ~mod+ (j ~mod* hs)) ~mod+ (i ~mod* 4)) as base.u64)
|
||||
this.insert_cost[k] = this.sf.f64_sub(a: this.insert_cost[j & 255], b: c)
|
||||
this.insert_cost[k] = this.bc.f64_sub(a: this.insert_cost[j & 255], b: c)
|
||||
}
|
||||
j ~mod+= 1
|
||||
}
|
||||
@@ -344,48 +351,121 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
// last position, we need to switch here.
|
||||
this.cost[.. 256].bulk_memset!(byte_value: 0)
|
||||
this.zero!(wb: args.wb, i: args.switch_signal, n: args.length ~mod* bitmap_len)
|
||||
nh = args.num_histograms.min(no_more_than: 256)
|
||||
byte_ix = 0
|
||||
while byte_ix < args.length {
|
||||
ix = byte_ix ~mod* bitmap_len
|
||||
insert_cost_ix = this.sym(wb: args.wb, base: args.data, i: byte_ix, width: args.width) ~mod* args.num_histograms
|
||||
insert_cost_ix = this.sym(wb: args.wb, base: args.data, i: byte_ix, width: args.width) ~mod* nh
|
||||
min_cost = F64_1E99
|
||||
block_switch_cost = args.block_switch_bitcost
|
||||
// insert_cost_ix is (symbol * nh) for a symbol less than
|
||||
// alphabet_size, so (insert_cost_ix + nh) is at most 35200.
|
||||
// Otherwise (which never happens), like the C code, the symbol's
|
||||
// insert costs would be out of bounds (and here, they are zero).
|
||||
icn = 0
|
||||
if insert_cost_ix <= 35200 {
|
||||
icn = nh.min(no_more_than: 35200 - insert_cost_ix)
|
||||
}
|
||||
// From byte 2000 on, block_switch_cost is block_switch_bitcost, so
|
||||
// the many cost[k] values that the previous byte capped at it all add
|
||||
// the same (block_switch_bitcost + insert_cost[i]) values. Memoize
|
||||
// those sums (the same f64_add, on the same operands) in
|
||||
// capped_plus, if there are enough remaining bytes to pay for it.
|
||||
if (byte_ix == 2000) and ((args.length ~mod- 2000) > alphabet_size) and (nh >= 4) {
|
||||
t = alphabet_size ~mod* nh
|
||||
t = t.min(no_more_than: 35200)
|
||||
i = 0
|
||||
while i < t {
|
||||
this.capped_plus[i.min(no_more_than: 35199)] = this.bc.f64_add(
|
||||
a: args.block_switch_bitcost, b: this.insert_cost[i.min(no_more_than: 35199)])
|
||||
i ~mod+= 1
|
||||
}
|
||||
use_capped = true
|
||||
}
|
||||
best_k = 0xFFFF_FFFF_FFFF_FFFF
|
||||
k = 0
|
||||
while k < args.num_histograms {
|
||||
while k < nh {
|
||||
// We are coding the symbol with entropy code k.
|
||||
c = 0
|
||||
if (insert_cost_ix ~mod+ k) < 35200 {
|
||||
c = this.insert_cost[(insert_cost_ix ~mod+ k) % 35200]
|
||||
if k < icn {
|
||||
j = insert_cost_ix ~mod+ k
|
||||
j = j.min(no_more_than: 35199)
|
||||
if use_capped and (this.cost[k & 255] == args.block_switch_bitcost) {
|
||||
c = this.capped_plus[j]
|
||||
} else {
|
||||
c = this.bc.f64_add(a: this.cost[k & 255], b: this.insert_cost[j])
|
||||
}
|
||||
} else {
|
||||
c = this.bc.f64_add(a: this.cost[k & 255], b: 0)
|
||||
}
|
||||
c = this.sf.f64_add(a: this.cost[k & 255], b: c)
|
||||
this.cost[k & 255] = c
|
||||
if this.sf.f64_lt(a: c, b: min_cost) <> 0 {
|
||||
if this.bc.f64_lt(a: c, b: min_cost) <> 0 {
|
||||
min_cost = c
|
||||
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: (k & 0xFF) as base.u8)
|
||||
best_k = k
|
||||
}
|
||||
k ~mod+= 1
|
||||
}
|
||||
if best_k <= 0xFF {
|
||||
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: (best_k & 0xFF) as base.u8)
|
||||
}
|
||||
// More blocks for the beginning.
|
||||
if byte_ix < 2000 {
|
||||
// block_switch_cost *= 0.77 + multiplier * (double)byte_ix, with
|
||||
// multiplier = 0.07 / 2000.
|
||||
block_switch_cost = this.sf.f64_mul(
|
||||
block_switch_cost = this.bc.f64_mul(
|
||||
a: block_switch_cost,
|
||||
b: this.sf.f64_add(
|
||||
b: this.bc.f64_add(
|
||||
a: 0x3FE8_A3D7_0A3D_70A4,
|
||||
b: this.sf.f64_mul(a: 0x3F02_599E_D7C6_FBD3, b: this.sf.f64_from_u64(a: byte_ix))))
|
||||
b: this.bc.f64_mul(a: 0x3F02_599E_D7C6_FBD3, b: this.bc.f64_from_u64(a: byte_ix))))
|
||||
}
|
||||
// S(c) = (c - min_cost), rounded, is monotone non-decreasing in c, so
|
||||
// the c values whose S(c) is at least block_switch_cost (the cost[k]
|
||||
// values that the loop below caps) are those at least some threshold
|
||||
// T. Find T by stepping (by one ULP) from (block_switch_cost +
|
||||
// min_cost) and, if that takes only a few steps, skip the
|
||||
// subtraction for every c >= T. skip_t holds T, or 0xFFFF_FFFF_FFFF_FFFF
|
||||
// (which no non-negative, non-NaN c is at least) to skip nothing.
|
||||
skip_t = 0xFFFF_FFFF_FFFF_FFFF
|
||||
if (nh >= 8) and (min_cost < 0x7FF0_0000_0000_0000) and
|
||||
((block_switch_cost ~mod- 1) < 0x7FEF_FFFF_FFFF_FFFF) {
|
||||
// min_cost is non-negative and finite, and block_switch_cost is
|
||||
// positive and finite.
|
||||
t = this.bc.f64_add(a: block_switch_cost, b: min_cost)
|
||||
n = 0
|
||||
while (n < 3) and (t < 0x7FF0_0000_0000_0000) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t, b: min_cost), b: block_switch_cost) == 0) {
|
||||
t ~mod+= 1
|
||||
n += 1
|
||||
}
|
||||
if (t < 0x7FF0_0000_0000_0000) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t, b: min_cost), b: block_switch_cost) <> 0) {
|
||||
// S(t) >= block_switch_cost. Step down while S(t - 1) is too.
|
||||
n = 0
|
||||
while (n < 3) and (t > 0) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t ~mod- 1, b: min_cost), b: block_switch_cost) <> 0) {
|
||||
t ~mod-= 1
|
||||
n += 1
|
||||
}
|
||||
if (t == 0) or
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t ~mod- 1, b: min_cost), b: block_switch_cost) == 0) {
|
||||
skip_t = t
|
||||
}
|
||||
}
|
||||
}
|
||||
k = 0
|
||||
while k < args.num_histograms {
|
||||
c = this.sf.f64_sub(a: this.cost[k & 255], b: min_cost)
|
||||
while k < nh {
|
||||
c = this.cost[k & 255]
|
||||
if (c <= 0x7FF0_0000_0000_0000) and (c >= skip_t) {
|
||||
// S(c) >= S(T) >= block_switch_cost.
|
||||
c = block_switch_cost
|
||||
} else {
|
||||
c = this.bc.f64_sub(a: c, b: min_cost)
|
||||
}
|
||||
this.cost[k & 255] = c
|
||||
if this.sf.f64_ge(a: c, b: block_switch_cost) <> 0 {
|
||||
if this.bc.f64_ge(a: c, b: block_switch_cost) <> 0 {
|
||||
this.cost[k & 255] = block_switch_cost
|
||||
j = ix ~mod+ (k >> 3)
|
||||
if true {
|
||||
b = this.ld8(wb: args.wb, i: args.switch_signal ~mod+ j)
|
||||
this.st8!(wb: args.wb, i: args.switch_signal ~mod+ j, v: b | ((1 as base.u8) << (k & 7)))
|
||||
}
|
||||
b = this.ld8(wb: args.wb, i: args.switch_signal ~mod+ j)
|
||||
this.st8!(wb: args.wb, i: args.switch_signal ~mod+ j, v: b | ((1 as base.u8) << (k & 7)))
|
||||
}
|
||||
k ~mod+= 1
|
||||
}
|
||||
@@ -648,7 +728,7 @@ pri func metablock_builder.cluster_blocks!(wb: slice base.u8, cat: base.u32[..=
|
||||
s = this.ld32(wb: args.wb, i: clusters ~mod+ (j ~mod* 4))
|
||||
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds, histogram: tmp,
|
||||
candidate: all_histograms ~mod+ ((s as base.u64) ~mod* hs), tmp: tmp ~mod+ hs)
|
||||
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
if this.bc.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
best_bits = cur_bits
|
||||
best_out = s
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ pri func metablock_builder.histogram_pair_is_less(
|
||||
p1_cost_diff: base.u64, p1_idx1: base.u32, p1_idx2: base.u32,
|
||||
p2_cost_diff: base.u64, p2_idx1: base.u32, p2_idx2: base.u32) base.bool {
|
||||
if this.sf.f64_ne(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0 {
|
||||
return this.sf.f64_gt(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0
|
||||
return this.bc.f64_gt(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0
|
||||
}
|
||||
return (args.p1_idx2 ~mod- args.p1_idx1) > (args.p2_idx2 ~mod- args.p2_idx1)
|
||||
}
|
||||
@@ -36,11 +36,11 @@ pri func metablock_builder.cluster_cost_diff(size_a: base.u64, size_b: base.u64)
|
||||
var size_c : base.u64
|
||||
|
||||
size_c = args.size_a ~mod+ args.size_b
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_a), b: this.bc.fast_log2(v: args.size_a)),
|
||||
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_b), b: this.bc.fast_log2(v: args.size_b))),
|
||||
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: size_c), b: this.bc.fast_log2(v: size_c)))
|
||||
return this.bc.f64_sub(
|
||||
a: this.bc.f64_add(
|
||||
a: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.size_a), b: this.bc.fast_log2(v: args.size_a)),
|
||||
b: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.size_b), b: this.bc.fast_log2(v: args.size_b))),
|
||||
b: this.bc.f64_mul(a: this.bc.f64_from_u64(a: size_c), b: this.bc.fast_log2(v: size_c)))
|
||||
}
|
||||
|
||||
// pair_copy is the struct assignment pairs[dst] = pairs[src], where dst and
|
||||
@@ -88,11 +88,11 @@ pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: bas
|
||||
}
|
||||
h1 = args.out ~mod+ ((idx1 as base.u64) ~mod* hs)
|
||||
h2 = args.out ~mod+ ((idx2 as base.u64) ~mod* hs)
|
||||
cost_diff = this.sf.f64_mul(a: 0x3FE0_0000_0000_0000, b: this.cluster_cost_diff(
|
||||
cost_diff = this.bc.f64_mul(a: 0x3FE0_0000_0000_0000, b: this.cluster_cost_diff(
|
||||
size_a: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx1 as base.u64) ~mod* 4)) as base.u64,
|
||||
size_b: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx2 as base.u64) ~mod* 4)) as base.u64))
|
||||
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds))
|
||||
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds))
|
||||
cost_diff = this.bc.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds))
|
||||
cost_diff = this.bc.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds))
|
||||
|
||||
if this.h_total(wb: args.wb, h: h1, ds: args.ds) == 0 {
|
||||
cost_combo = this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds)
|
||||
@@ -109,13 +109,13 @@ pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: bas
|
||||
this.h_copy!(wb: args.wb, h: args.tmp, v: h1, ds: args.ds)
|
||||
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: h2, ds: args.ds)
|
||||
c = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: args.ds)
|
||||
if this.sf.f64_lt(a: c, b: this.sf.f64_sub(a: threshold, b: cost_diff)) <> 0 {
|
||||
if this.bc.f64_lt(a: c, b: this.bc.f64_sub(a: threshold, b: cost_diff)) <> 0 {
|
||||
cost_combo = c
|
||||
is_good_pair = true
|
||||
}
|
||||
}
|
||||
if is_good_pair {
|
||||
cost_diff = this.sf.f64_add(a: cost_diff, b: cost_combo)
|
||||
cost_diff = this.bc.f64_add(a: cost_diff, b: cost_combo)
|
||||
if (this.num_pairs > 0) and this.histogram_pair_is_less(
|
||||
p1_cost_diff: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16),
|
||||
p1_idx1: this.ld32(wb: args.wb, i: args.pairs),
|
||||
@@ -182,7 +182,7 @@ pri func metablock_builder.histogram_combine!(wb: slice base.u8, ds: base.u32[..
|
||||
}
|
||||
|
||||
while num_clusters > min_cluster_size {
|
||||
if this.sf.f64_ge(a: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16), b: cost_diff_threshold) <> 0 {
|
||||
if this.bc.f64_ge(a: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16), b: cost_diff_threshold) <> 0 {
|
||||
cost_diff_threshold = F64_1E99
|
||||
min_cluster_size = args.max_clusters
|
||||
continue
|
||||
@@ -279,7 +279,7 @@ pri func metablock_builder.histogram_bit_cost_distance!(wb: slice base.u8, ds: b
|
||||
this.h_copy!(wb: args.wb, h: args.tmp, v: args.histogram, ds: args.ds)
|
||||
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: args.candidate, ds: args.ds)
|
||||
c = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: args.ds)
|
||||
return this.sf.f64_sub(a: c, b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
|
||||
return this.bc.f64_sub(a: c, b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
|
||||
}
|
||||
|
||||
// Mirrors FN(BrotliHistogramRemap) in cluster_inc.h.
|
||||
@@ -310,7 +310,7 @@ pri func metablock_builder.histogram_remap!(wb: slice base.u8, ds: base.u32[..=
|
||||
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds,
|
||||
histogram: args.in_histos ~mod+ (i ~mod* hs),
|
||||
candidate: args.out ~mod+ ((c as base.u64) ~mod* hs), tmp: args.tmp)
|
||||
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
if this.bc.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
best_bits = cur_bits
|
||||
best_out = c
|
||||
}
|
||||
|
||||
@@ -553,12 +553,12 @@ pri func encoder.estimate_entropy(off: base.u32, size: base.u32) base.u64 {
|
||||
while i < args.size {
|
||||
p = this.ctx_histo[(args.off ~mod+ i) & 511]
|
||||
total ~mod+= p as base.u64
|
||||
result = this.sf.f64_add(a: result, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
result = this.bc.f64_add(a: result, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
return this.sf.f64_sub(a: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
return this.bc.f64_sub(a: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
}
|
||||
|
||||
// Mirrors ChooseContextMap in encode.c. The bigram_histo is
|
||||
@@ -585,30 +585,30 @@ pri func encoder.choose_context_map!() base.u32 {
|
||||
i ~mod+= 1
|
||||
}
|
||||
e1 = this.estimate_entropy(off: 16, size: 3)
|
||||
e2 = this.sf.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e2 = this.bc.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e3 = 0
|
||||
i = 0
|
||||
while i < 3 {
|
||||
e3 = this.sf.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
e3 = this.bc.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
total = (this.ctx_histo[16] ~mod+ this.ctx_histo[17]) ~mod+ this.ctx_histo[18]
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e3 = this.sf.f64_mul(a: e3, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
e3 = this.bc.f64_mul(a: e3, b: e0)
|
||||
|
||||
if this.quality < 7 {
|
||||
// 3 context models is a bit slower, don't use it at lower qualities.
|
||||
e3 = this.sf.f64_mul(a: e1, b: F64_10)
|
||||
e3 = this.bc.f64_mul(a: e1, b: F64_10)
|
||||
}
|
||||
// If expected savings by symbol are less than 0.2 bits, skip the context
|
||||
// modeling -- in exchange for faster decoding speed.
|
||||
if (this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
return 1
|
||||
} else if this.sf.f64_lt(a: this.sf.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
} else if this.bc.f64_lt(a: this.bc.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
return 2
|
||||
}
|
||||
return 3
|
||||
@@ -668,16 +668,16 @@ pri func encoder.should_use_complex_static_context_map!(wb: roslice base.u8, sta
|
||||
e2 = 0
|
||||
i = 0
|
||||
while i < 13 {
|
||||
e2 = this.sf.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
e2 = this.bc.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
i ~mod+= 1
|
||||
}
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
// The triggering heuristics below were tuned by compressing the
|
||||
// individual files of the silesia corpus.
|
||||
if (this.sf.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
return 0
|
||||
}
|
||||
return 13
|
||||
@@ -748,16 +748,16 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
return false
|
||||
}
|
||||
if args.num_commands < ((args.bytes >> 8) + 2) {
|
||||
if this.sf.f64_gt(
|
||||
a: this.sf.f64_from_u64(a: args.num_literals),
|
||||
b: this.sf.f64_mul(a: F64_0_99, b: this.sf.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
if this.bc.f64_gt(
|
||||
a: this.bc.f64_from_u64(a: args.num_literals),
|
||||
b: this.bc.f64_mul(a: F64_0_99, b: this.bc.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.ctx_histo[i] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
bit_cost_threshold = this.sf.f64_mul(
|
||||
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
bit_cost_threshold = this.bc.f64_mul(
|
||||
a: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
b: F64_INV_13)
|
||||
t = (args.bytes ~mod+ 12) / 13
|
||||
pos = (args.last_flush_pos & 0xFFFF_FFFF) as base.u32
|
||||
@@ -767,7 +767,7 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
pos ~mod+= 13
|
||||
i ~mod+= 1
|
||||
}
|
||||
if this.sf.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
if this.bc.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -787,16 +787,16 @@ pri func encoder.ctx_histo_bits_entropy() base.u64 {
|
||||
while i < 256 {
|
||||
p = this.ctx_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
retval = this.bc.f64_sub(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
retval = this.bc.f64_add(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.bc.f64_lt(a: retval, b: this.bc.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.bc.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
|
||||
@@ -0,0 +1,742 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// This file holds package-private fast paths for the std/softfloat
|
||||
// operations that the encoder's hot loops use (the Zopfli cost model, the
|
||||
// block splitter and histogram clustering).
|
||||
//
|
||||
// Each softfloat.arith method is another package's public function: every
|
||||
// call checks its receiver and, being an exported function, is not inlined
|
||||
// by the C compiler. These bit_cost methods instead handle the common cases
|
||||
// (finite, normal and, for addition and subtraction, mostly non-negative
|
||||
// operands with normal results) directly, so that they can be inlined, and
|
||||
// otherwise call the softfloat.arith method of the same name. Either way,
|
||||
// they return exactly the same bits as that method (the IEEE 754 result,
|
||||
// rounding to nearest, ties to even), so they are only a speed optimization.
|
||||
//
|
||||
// Operands are IEEE 754 binary32 (base.u32) or binary64 (base.u64) bit
|
||||
// patterns. The fast paths' conditions on positive normal values use that
|
||||
// such bit patterns are in [0x0080_0000, 0x7F80_0000) (or, for f64, in
|
||||
// [0x0010_0000_0000_0000, 0x7FF0_0000_0000_0000)) and that, there, the bit
|
||||
// patterns sort like the values.
|
||||
|
||||
// -------- Helpers.
|
||||
|
||||
// clz64 returns the number of leading zero bits of a, which must be non-zero
|
||||
// (if a is zero, it returns 63).
|
||||
pri func bit_cost.clz64(a: base.u64) base.u32[..= 63] {
|
||||
var x : base.u64
|
||||
var n : base.u32[..= 63]
|
||||
|
||||
x = args.a
|
||||
if (x >> 32) == 0 {
|
||||
x = x ~mod<< 32
|
||||
n = 32
|
||||
}
|
||||
if (x >> 48) == 0 {
|
||||
x = x ~mod<< 16
|
||||
n = (n + 16) & 63
|
||||
}
|
||||
if (x >> 56) == 0 {
|
||||
x = x ~mod<< 8
|
||||
n = (n + 8) & 63
|
||||
}
|
||||
if (x >> 60) == 0 {
|
||||
x = x ~mod<< 4
|
||||
n = (n + 4) & 63
|
||||
}
|
||||
if (x >> 62) == 0 {
|
||||
x = x ~mod<< 2
|
||||
n = (n + 2) & 63
|
||||
}
|
||||
if (x >> 63) == 0 {
|
||||
n = (n + 1) & 63
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// -------- f32.
|
||||
|
||||
// f32_add returns (a + b), like softfloat.arith.f32_add. Its fast path is
|
||||
// for the most common case (both are positive and normal) and the others
|
||||
// take f32_add_any.
|
||||
pri func bit_cost.f32_add(a: base.u32, b: base.u32) base.u32 {
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
var z : base.u32
|
||||
|
||||
if ((args.a ~mod- 0x80_0000) < 0x7F00_0000) and ((args.b ~mod- 0x80_0000) < 0x7F00_0000) {
|
||||
// Both are positive and normal. Order them so that x >= y.
|
||||
x = args.a
|
||||
y = args.b
|
||||
if y > x {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 25 {
|
||||
// y is less than half an ULP of x, so the sum rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 24 is exact, and the sum is less than (1 << 64).
|
||||
s = ((((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod+
|
||||
(((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
// Round to nearest, ties to even: add just under half an ULP, plus
|
||||
// one if the truncated result is odd. The rounded significand is in
|
||||
// [1 << 23 ..= 1 << 24] and adding it (whose bit 23 is the implicit
|
||||
// bit) to the shifted exponent carries any rounding overflow into the
|
||||
// exponent field.
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
if z >= 0x7F80_0000 {
|
||||
return 0x7F80_0000
|
||||
}
|
||||
return z
|
||||
}
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
} else if (args.a == 0) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) and (args.b <> 0x8000_0000) {
|
||||
// (+0 + b) is b, for any b other than a NaN or -0.
|
||||
return args.b
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and (args.a <> 0x8000_0000) {
|
||||
return args.a
|
||||
}
|
||||
return this.f32_add_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_sub returns (a - b), like softfloat.arith.f32_sub. Its fast path is
|
||||
// for the most common case (both are positive and normal, a > b and there is
|
||||
// little cancellation) and the others take f32_sub_any.
|
||||
pri func bit_cost.f32_sub(a: base.u32, b: base.u32) base.u32 {
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
|
||||
if ((args.b ~mod- 0x80_0000) < 0x7F00_0000) and (args.a > args.b) and (args.a < 0x7F80_0000) {
|
||||
// Both are positive and normal, and a > b.
|
||||
ex = (args.a >> 23) & 0xFF
|
||||
d = ex ~mod- ((args.b >> 23) & 0xFF)
|
||||
if d >= 26 {
|
||||
// b is less than half of the spacing of the f32 values just
|
||||
// below a, so the difference rounds to a.
|
||||
return args.a
|
||||
} else if d >= 2 {
|
||||
// The significands, with their implicit bit at bit 62. Shifting
|
||||
// b's right by at most 25 is exact. As (d >= 2), the difference
|
||||
// is at least (1 << 61), so it needs at most a 1 bit shift to
|
||||
// normalize (which is exact too).
|
||||
s = ((((args.a & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod-
|
||||
(((((args.b & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
s ~mod<<= 1
|
||||
ex = (ex ~mod- 1) & 0xFF
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The result is
|
||||
// normal, as ex is at least 2 after the shift (a's biased
|
||||
// exponent is at least (d + 1)).
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
}
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
// (a - +0) is a, for any non-NaN a.
|
||||
return args.a
|
||||
} else if (args.a == args.b) and (args.a < 0x7F80_0000) {
|
||||
// (a - a) is +0, for finite non-negative a.
|
||||
return 0
|
||||
}
|
||||
return this.f32_sub_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_add_any returns (a + b), like softfloat.arith.f32_add. It is the
|
||||
// general (but slower) fast path, for operands of any sign.
|
||||
pri func bit_cost.f32_add_any(a: base.u32, b: base.u32) base.u32 {
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var mx : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u32[..= 63]
|
||||
var z : base.u32
|
||||
|
||||
if ((((args.a >> 23) & 0xFF) ~mod- 1) < 0xFE) and ((((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE) {
|
||||
// Both are normal. Order them so that |x| >= |y|. The result has x's
|
||||
// sign (or is +0).
|
||||
x = args.a
|
||||
y = args.b
|
||||
if (y & 0x7FFF_FFFF) > (x & 0x7FFF_FFFF) {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 26 {
|
||||
// |y| is less than half an ULP of |x| (and less than half of the
|
||||
// spacing of the f32 values just below |x|), so the result
|
||||
// rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 25 is exact, so the sum or difference is exact
|
||||
// (and less than (1 << 64)).
|
||||
mx = (((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39
|
||||
my = ((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31)
|
||||
if ((x ^ y) >> 31) == 0 {
|
||||
s = mx ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
// Round to nearest, ties to even: add just under half an ULP,
|
||||
// plus one if the truncated result is odd. The rounded
|
||||
// significand is in [1 << 23 ..= 1 << 24] and adding it
|
||||
// (whose bit 23 is the implicit bit) to the shifted exponent
|
||||
// carries any rounding overflow into the exponent field.
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
if z >= 0x7F80_0000 {
|
||||
z = 0x7F80_0000
|
||||
}
|
||||
return (x & 0x8000_0000) | z
|
||||
}
|
||||
} else {
|
||||
s = mx ~mod- my
|
||||
if s == 0 {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if s < 0x4000_0000_0000_0000 {
|
||||
lz = (this.clz64(a: s) ~mod- 1) & 63
|
||||
if lz >= ex {
|
||||
// The result is subnormal.
|
||||
return this.sf.f32_add(a: args.a, b: args.b)
|
||||
}
|
||||
ex = (ex ~mod- lz) & 0xFF
|
||||
s = s ~mod<< lz
|
||||
}
|
||||
}
|
||||
// s is in [1 << 62, 1 << 63) and ex in [1 ..= 254].
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return (x & 0x8000_0000) | (((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32))
|
||||
}
|
||||
return this.sf.f32_add(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_sub_any returns (a - b), like softfloat.arith.f32_sub.
|
||||
pri func bit_cost.f32_sub_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if (((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE {
|
||||
// b is normal (not a NaN), so (a - b) is exactly (a + -b), including
|
||||
// the sign of a zero result.
|
||||
return this.f32_add_any(a: args.a, b: args.b ^ 0x8000_0000)
|
||||
}
|
||||
return this.sf.f32_sub(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_key maps a non-NaN f32 to a u32 that sorts in the same order as the
|
||||
// f32 values (except that -0 sorts just below +0).
|
||||
pri func bit_cost.f32_key(a: base.u32) base.u32 {
|
||||
if (args.a >> 31) <> 0 {
|
||||
return 0xFFFF_FFFF ^ args.a
|
||||
}
|
||||
return args.a | 0x8000_0000
|
||||
}
|
||||
|
||||
// f32_lt returns (a < b), like softfloat.arith.f32_lt.
|
||||
pri func bit_cost.f32_lt(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a < args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_lt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_lt_any returns (a < b), like softfloat.arith.f32_lt.
|
||||
pri func bit_cost.f32_lt_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
// Neither is a NaN.
|
||||
if (this.f32_key(a: args.a) < this.f32_key(a: args.b)) and
|
||||
(((args.a | args.b) & 0x7FFF_FFFF) <> 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f32_lt(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_le returns (a <= b), like softfloat.arith.f32_le.
|
||||
pri func bit_cost.f32_le(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a <= args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_le_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_le_any returns (a <= b), like softfloat.arith.f32_le.
|
||||
pri func bit_cost.f32_le_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
if (this.f32_key(a: args.a) <= this.f32_key(a: args.b)) or
|
||||
(((args.a | args.b) & 0x7FFF_FFFF) == 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f32_le(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_gt returns (a > b), like softfloat.arith.f32_gt.
|
||||
pri func bit_cost.f32_gt(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a > args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_gt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_gt_any returns (a > b), like softfloat.arith.f32_gt.
|
||||
pri func bit_cost.f32_gt_any(a: base.u32, b: base.u32) base.u32 {
|
||||
return this.f32_lt_any(a: args.b, b: args.a)
|
||||
}
|
||||
|
||||
// -------- f64.
|
||||
|
||||
// f64_add returns (a + b), like softfloat.arith.f64_add. Its fast path is
|
||||
// for the most common case (both are positive and normal) and the others
|
||||
// take f64_add_any.
|
||||
pri func bit_cost.f64_add(a: base.u64, b: base.u64) base.u64 {
|
||||
var x : base.u64
|
||||
var y : base.u64
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
|
||||
if ((args.a ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) and
|
||||
((args.b ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) {
|
||||
// Both are positive and normal. Order them so that x >= y.
|
||||
x = args.a
|
||||
y = args.b
|
||||
if y > x {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 52) & 0x7FF
|
||||
d = ex ~mod- ((y >> 52) & 0x7FF)
|
||||
if d >= 54 {
|
||||
// y is less than half an ULP of x, so the sum rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62 and 10 extra
|
||||
// bits of precision at the bottom. Align y's with x's, keeping a
|
||||
// sticky bit.
|
||||
my = ((y & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
s = (((x & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
ex = (ex ~mod+ 1) & 0x7FF
|
||||
s = (s >> 1) | (s & 1)
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The rounded
|
||||
// significand is in [1 << 52 ..= 1 << 53].
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
s = ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
if s >= 0x7FF0_0000_0000_0000 {
|
||||
return 0x7FF0_0000_0000_0000
|
||||
}
|
||||
return s
|
||||
} else if (args.a == 0) and ((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
(args.b <> 0x8000_0000_0000_0000) {
|
||||
// (+0 + b) is b, for any b other than a NaN or -0.
|
||||
return args.b
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
(args.a <> 0x8000_0000_0000_0000) {
|
||||
return args.a
|
||||
}
|
||||
return this.f64_add_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_sub returns (a - b), like softfloat.arith.f64_sub. Its fast path is
|
||||
// for the most common case (both are positive and normal, a > b and there is
|
||||
// little cancellation) and the others take f64_sub_any.
|
||||
pri func bit_cost.f64_sub(a: base.u64, b: base.u64) base.u64 {
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u64
|
||||
|
||||
if ((args.b ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) and
|
||||
(args.a > args.b) and (args.a < 0x7FF0_0000_0000_0000) {
|
||||
// Both are positive and normal, and a > b.
|
||||
ex = (args.a >> 52) & 0x7FF
|
||||
d = ex ~mod- ((args.b >> 52) & 0x7FF)
|
||||
if d >= 55 {
|
||||
// b is less than half of the spacing of the f64 values just
|
||||
// below a, so the difference rounds to a.
|
||||
return args.a
|
||||
} else if d >= 2 {
|
||||
// The significands, with their implicit bit at bit 62 and 10
|
||||
// extra bits of precision at the bottom. Align b's with a's,
|
||||
// keeping a sticky bit. As (d >= 2), the difference is at least
|
||||
// (1 << 61), so it needs at most a 1 bit shift to normalize.
|
||||
my = ((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
s = (((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod- my
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
s ~mod<<= 1
|
||||
ex = (ex ~mod- 1) & 0x7FF
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The result is
|
||||
// normal, as ex is at least 2 after the shift.
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
return ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
}
|
||||
// d is 0 or 1, so shifting b's significand right by d (with 10
|
||||
// extra bits) is exact, and so is the difference (which is non-zero,
|
||||
// as a > b) and normalizing it.
|
||||
s = (((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod-
|
||||
((((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) >> (d & 1))
|
||||
lz = (this.clz64(a: s) as base.u64) ~mod- 1
|
||||
if lz < ex {
|
||||
ex = (ex ~mod- lz) & 0x7FF
|
||||
s = s ~mod<< (lz & 63)
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
return ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
}
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
// (a - +0) is a, for any non-NaN a.
|
||||
return args.a
|
||||
} else if (args.a == args.b) and (args.a < 0x7FF0_0000_0000_0000) {
|
||||
// (a - a) is +0, for finite non-negative a.
|
||||
return 0
|
||||
}
|
||||
return this.f64_sub_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_add_any returns (a + b), like softfloat.arith.f64_add. It is the
|
||||
// general (but slower) fast path, for operands of any sign.
|
||||
pri func bit_cost.f64_add_any(a: base.u64, b: base.u64) base.u64 {
|
||||
var x : base.u64
|
||||
var y : base.u64
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var mx : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u64[..= 63]
|
||||
|
||||
if ((((args.a >> 52) & 0x7FF) ~mod- 1) < 0x7FE) and ((((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE) {
|
||||
// Both are normal. Order them so that |x| >= |y|. The result has x's
|
||||
// sign (or is +0).
|
||||
x = args.a
|
||||
y = args.b
|
||||
if (y & 0x7FFF_FFFF_FFFF_FFFF) > (x & 0x7FFF_FFFF_FFFF_FFFF) {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 52) & 0x7FF
|
||||
d = ex ~mod- ((y >> 52) & 0x7FF)
|
||||
if d >= 55 {
|
||||
// |y| is less than half an ULP of |x| (and less than half of the
|
||||
// spacing of the f64 values just below |x|), so the result
|
||||
// rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62 and 10 extra
|
||||
// bits of precision at the bottom. Align my with mx, keeping a
|
||||
// sticky bit (which only happens if (d > 10)).
|
||||
mx = ((x & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
my = ((y & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
if ((x ^ y) >> 63) == 0 {
|
||||
s = mx ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
ex = (ex ~mod+ 1) & 0x7FF
|
||||
s = (s >> 1) | (s & 1)
|
||||
}
|
||||
} else {
|
||||
s = mx ~mod- my
|
||||
if s == 0 {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if s < 0x4000_0000_0000_0000 {
|
||||
// If (d >= 2) then s >= (1 << 61) and the normalization shift
|
||||
// is 1. Otherwise, no bits were shifted out of my.
|
||||
lz = ((this.clz64(a: s) as base.u64) ~mod- 1) & 63
|
||||
if lz >= ex {
|
||||
// The result is subnormal.
|
||||
return this.sf.f64_add(a: args.a, b: args.b)
|
||||
}
|
||||
ex = (ex ~mod- lz) & 0x7FF
|
||||
s = s ~mod<< lz
|
||||
}
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). s is in
|
||||
// [1 << 62, 1 << 63) and ex in [1 ..= 0x7FF]. The rounded
|
||||
// significand is in [1 << 52 ..= 1 << 53].
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
s = ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
if s >= 0x7FF0_0000_0000_0000 {
|
||||
s = 0x7FF0_0000_0000_0000
|
||||
}
|
||||
return (x & 0x8000_0000_0000_0000) | s
|
||||
}
|
||||
return this.sf.f64_add(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_sub_any returns (a - b), like softfloat.arith.f64_sub.
|
||||
pri func bit_cost.f64_sub_any(a: base.u64, b: base.u64) base.u64 {
|
||||
if (((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE {
|
||||
// b is normal (not a NaN), so (a - b) is exactly (a + -b), including
|
||||
// the sign of a zero result.
|
||||
return this.f64_add_any(a: args.a, b: args.b ^ 0x8000_0000_0000_0000)
|
||||
}
|
||||
return this.sf.f64_sub(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_key maps a non-NaN f64 to a u64 that sorts in the same order as the
|
||||
// f64 values (except that -0 sorts just below +0).
|
||||
pri func bit_cost.f64_key(a: base.u64) base.u64 {
|
||||
if (args.a >> 63) <> 0 {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF ^ args.a
|
||||
}
|
||||
return args.a | 0x8000_0000_0000_0000
|
||||
}
|
||||
|
||||
// f64_lt returns (a < b), like softfloat.arith.f64_lt.
|
||||
pri func bit_cost.f64_lt(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a < args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_lt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_lt_any returns (a < b), like softfloat.arith.f64_lt.
|
||||
pri func bit_cost.f64_lt_any(a: base.u64, b: base.u64) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
// Neither is a NaN.
|
||||
if (this.f64_key(a: args.a) < this.f64_key(a: args.b)) and
|
||||
(((args.a | args.b) & 0x7FFF_FFFF_FFFF_FFFF) <> 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f64_lt(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_gt returns (a > b), like softfloat.arith.f64_gt.
|
||||
pri func bit_cost.f64_gt(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a > args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_gt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_gt_any returns (a > b), like softfloat.arith.f64_gt.
|
||||
pri func bit_cost.f64_gt_any(a: base.u64, b: base.u64) base.u32 {
|
||||
return this.f64_lt_any(a: args.b, b: args.a)
|
||||
}
|
||||
|
||||
// f64_ge returns (a >= b), like softfloat.arith.f64_ge.
|
||||
pri func bit_cost.f64_ge(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a >= args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_ge_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_ge_any returns (a >= b), like softfloat.arith.f64_ge.
|
||||
pri func bit_cost.f64_ge_any(a: base.u64, b: base.u64) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
if (this.f64_key(a: args.a) >= this.f64_key(a: args.b)) or
|
||||
(((args.a | args.b) & 0x7FFF_FFFF_FFFF_FFFF) == 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f64_ge(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_mul returns (a * b), like softfloat.arith.f64_mul. Its fast path is
|
||||
// for normal operands with a normal (and finite) result.
|
||||
pri func bit_cost.f64_mul(a: base.u64, b: base.u64) base.u64 {
|
||||
var ea : base.u64
|
||||
var eb : base.u64
|
||||
var e : base.u64
|
||||
var ma : base.u64
|
||||
var mb : base.u64
|
||||
var a0 : base.u64[..= 0xFFFF_FFFF]
|
||||
var a1 : base.u64[..= 0xFFFF_FFFF]
|
||||
var b0 : base.u64[..= 0xFFFF_FFFF]
|
||||
var b1 : base.u64[..= 0xFFFF_FFFF]
|
||||
var p00 : base.u64
|
||||
var p01 : base.u64
|
||||
var p10 : base.u64
|
||||
var mid : base.u64
|
||||
var s : base.u64
|
||||
var rb : base.u64
|
||||
|
||||
ea = (args.a >> 52) & 0x7FF
|
||||
eb = (args.b >> 52) & 0x7FF
|
||||
if ((ea ~mod- 1) < 0x7FE) and ((eb ~mod- 1) < 0x7FE) {
|
||||
// Both are normal. e is one less than the result's biased exponent
|
||||
// (before normalization), as a two's complement integer. The
|
||||
// significands have their implicit bit at bit 62 and bit 63, so that
|
||||
// the high 64 bits of their 128-bit product have the implicit bit at
|
||||
// bit 61 or 62.
|
||||
e = (ea ~mod+ eb) ~mod- 0x3FF
|
||||
ma = ((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
mb = ((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 11
|
||||
a0 = ma & 0xFFFF_FFFF
|
||||
a1 = ma >> 32
|
||||
b0 = mb & 0xFFFF_FFFF
|
||||
b1 = mb >> 32
|
||||
p00 = a0 * b0
|
||||
p01 = a0 * b1
|
||||
p10 = a1 * b0
|
||||
mid = (p00 >> 32) + (p01 & 0xFFFF_FFFF) + (p10 & 0xFFFF_FFFF)
|
||||
s = (((a1 * b1) ~mod+ (p01 >> 32)) ~mod+ (p10 >> 32)) ~mod+ (mid >> 32)
|
||||
// The low 64 bits of the product, (ma * mb), give the sticky bit.
|
||||
if (ma ~mod* mb) <> 0 {
|
||||
s |= 1
|
||||
}
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
e ~mod-= 1
|
||||
s ~mod<<= 1
|
||||
}
|
||||
if e < 0x7FD {
|
||||
// The result is normal and finite (if e is 0x7FC and the
|
||||
// significand rounds up to (1 << 53), the exponent field is 0x7FE).
|
||||
// Round to nearest, ties to even.
|
||||
rb = s & 0x3FF
|
||||
s = (s ~mod+ 0x200) >> 10
|
||||
if rb == 0x200 {
|
||||
s &= 0xFFFF_FFFF_FFFF_FFFE
|
||||
}
|
||||
return ((args.a ^ args.b) & 0x8000_0000_0000_0000) | ((e ~mod<< 52) ~mod+ s)
|
||||
}
|
||||
}
|
||||
return this.sf.f64_mul(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_from_u64 returns the f64 nearest to a (C's (double)a), like
|
||||
// softfloat.arith.f64_from_u64. Its fast path is for a < (1 << 53), whose
|
||||
// conversion is exact.
|
||||
pri func bit_cost.f64_from_u64(a: base.u64) base.u64 {
|
||||
var lz : base.u32[..= 63]
|
||||
|
||||
if args.a == 0 {
|
||||
return 0
|
||||
} else if args.a < 0x0020_0000_0000_0000 {
|
||||
// Shift a's top bit to bit 52 (the implicit bit, which is then added
|
||||
// to the exponent field).
|
||||
lz = this.clz64(a: args.a)
|
||||
if lz >= 11 {
|
||||
return (((0x43D ~mod- (lz as base.u64)) & 0x7FF) ~mod<< 52) ~mod+ (args.a ~mod<< (lz - 11))
|
||||
}
|
||||
}
|
||||
return this.sf.f64_from_u64(a: args.a)
|
||||
}
|
||||
|
||||
// f64_from_u32 returns the f64 equal to a (C's (double)a), like
|
||||
// softfloat.arith.f64_from_u32.
|
||||
pri func bit_cost.f64_from_u32(a: base.u32) base.u64 {
|
||||
return this.f64_from_u64(a: args.a as base.u64)
|
||||
}
|
||||
|
||||
// f64_to_u64_trunc returns a, truncated towards zero, like
|
||||
// softfloat.arith.f64_to_u64_trunc. Its fast path is for 0 <= a < 2**63.
|
||||
pri func bit_cost.f64_to_u64_trunc(a: base.u64) base.u64 {
|
||||
var exp : base.u64
|
||||
|
||||
exp = args.a >> 52
|
||||
if exp < 0x3FF {
|
||||
// a is in [+0 .. 1), so the result is 0.
|
||||
return 0
|
||||
} else if exp <= 0x433 {
|
||||
return ((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) >> ((0x433 - exp) & 63)
|
||||
}
|
||||
return this.sf.f64_to_u64_trunc(a: args.a)
|
||||
}
|
||||
|
||||
// f32_from_u32 returns the f32 nearest to a (C's (float)a), like
|
||||
// softfloat.arith.f32_from_u32. Its fast path is for a < (1 << 24), whose
|
||||
// conversion is exact.
|
||||
pri func bit_cost.f32_from_u32(a: base.u32) base.u32 {
|
||||
var lz : base.u32[..= 63]
|
||||
|
||||
if args.a == 0 {
|
||||
return 0
|
||||
} else if args.a < 0x100_0000 {
|
||||
// Shift a's top bit to bit 23 (the implicit bit, which is then added
|
||||
// to the exponent field).
|
||||
lz = this.clz64(a: args.a as base.u64)
|
||||
if lz >= 40 {
|
||||
return ((189 ~mod- lz) ~mod<< 23) ~mod+ (args.a ~mod<< (lz - 40))
|
||||
}
|
||||
}
|
||||
return this.sf.f32_from_u32(a: args.a)
|
||||
}
|
||||
|
||||
// F32_OF_SMALL_INT[i] is the f32 equal to i (C's (float)i).
|
||||
pri const F32_OF_SMALL_INT : roarray[32] base.u32 = [
|
||||
0x0000_0000, 0x3F80_0000, 0x4000_0000, 0x4040_0000, 0x4080_0000, 0x40A0_0000,
|
||||
0x40C0_0000, 0x40E0_0000, 0x4100_0000, 0x4110_0000, 0x4120_0000, 0x4130_0000,
|
||||
0x4140_0000, 0x4150_0000, 0x4160_0000, 0x4170_0000, 0x4180_0000, 0x4188_0000,
|
||||
0x4190_0000, 0x4198_0000, 0x41A0_0000, 0x41A8_0000, 0x41B0_0000, 0x41B8_0000,
|
||||
0x41C0_0000, 0x41C8_0000, 0x41D0_0000, 0x41D8_0000, 0x41E0_0000, 0x41E8_0000,
|
||||
0x41F0_0000, 0x41F8_0000,
|
||||
]
|
||||
@@ -332,25 +332,25 @@ pri func encoder.should_merge_block!(input: roslice base.u8, from: base.u64, len
|
||||
|
||||
total = (args.len ~mod+ 42) / 43
|
||||
// r = (FastLog2(total) + 0.5) * (double)total + 200.
|
||||
r = this.sf.f64_add(
|
||||
a: this.sf.f64_mul(
|
||||
a: this.sf.f64_add(a: this.fast_log2(v: total), b: 0x3FE0_0000_0000_0000),
|
||||
b: this.sf.f64_from_u64(a: total)),
|
||||
r = this.bc.f64_add(
|
||||
a: this.bc.f64_mul(
|
||||
a: this.bc.f64_add(a: this.fast_log2(v: total), b: 0x3FE0_0000_0000_0000),
|
||||
b: this.bc.f64_from_u64(a: total)),
|
||||
b: 0x4069_0000_0000_0000)
|
||||
j = 0
|
||||
while j < 256 {
|
||||
// r -= (double)histo[i] * (depths[i] + FastLog2(histo[i])).
|
||||
h = this.histogram[j] as base.u64
|
||||
r = this.sf.f64_sub(
|
||||
r = this.bc.f64_sub(
|
||||
a: r,
|
||||
b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: h),
|
||||
b: this.sf.f64_add(
|
||||
a: this.sf.f64_from_u32(a: this.lit_depth[j] as base.u32),
|
||||
b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: h),
|
||||
b: this.bc.f64_add(
|
||||
a: this.bc.f64_from_u32(a: this.lit_depth[j] as base.u32),
|
||||
b: this.fast_log2(v: h))))
|
||||
j ~mod+= 1
|
||||
}
|
||||
return this.sf.f64_ge(a: r, b: 0) <> 0
|
||||
return this.bc.f64_ge(a: r, b: 0) <> 0
|
||||
}
|
||||
|
||||
// Mirrors ShouldUseUncompressedMode in compress_fragment.c.
|
||||
|
||||
@@ -472,18 +472,18 @@ pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u6
|
||||
var max_total_bit_cost : base.u64
|
||||
var i : base.u64
|
||||
|
||||
corpus_size = this.sf.f64_from_u64(a: args.input_size)
|
||||
corpus_size = this.bc.f64_from_u64(a: args.input_size)
|
||||
// MIN_RATIO is 0.98 (0x3FEF_5C28_F5C2_8F5C).
|
||||
if this.sf.f64_lt(
|
||||
a: this.sf.f64_from_u64(a: args.num_literals),
|
||||
b: this.sf.f64_mul(a: 0x3FEF_5C28_F5C2_8F5C, b: corpus_size)) <> 0 {
|
||||
if this.bc.f64_lt(
|
||||
a: this.bc.f64_from_u64(a: args.num_literals),
|
||||
b: this.bc.f64_mul(a: 0x3FEF_5C28_F5C2_8F5C, b: corpus_size)) <> 0 {
|
||||
return true
|
||||
}
|
||||
// max_total_bit_cost = corpus_size * 8 * MIN_RATIO / SAMPLE_RATE, where
|
||||
// SAMPLE_RATE is 43 and 8.0 is 0x4020_0000_0000_0000.
|
||||
max_total_bit_cost = this.sf.f64_div(
|
||||
a: this.sf.f64_mul(
|
||||
a: this.sf.f64_mul(a: corpus_size, b: 0x4020_0000_0000_0000),
|
||||
a: this.bc.f64_mul(
|
||||
a: this.bc.f64_mul(a: corpus_size, b: 0x4020_0000_0000_0000),
|
||||
b: 0x3FEF_5C28_F5C2_8F5C),
|
||||
b: 0x4045_8000_0000_0000)
|
||||
this.histogram[.. 256].bulk_memset!(byte_value: 0)
|
||||
@@ -494,7 +494,7 @@ pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u6
|
||||
}
|
||||
i ~mod+= 43
|
||||
}
|
||||
return this.sf.f64_lt(a: this.bits_entropy_of_histogram(), b: max_total_bit_cost) <> 0
|
||||
return this.bc.f64_lt(a: this.bits_entropy_of_histogram(), b: max_total_bit_cost) <> 0
|
||||
}
|
||||
|
||||
// Mirrors EmitUncompressedMetaBlock in compress_fragment_two_pass.c (which,
|
||||
|
||||
@@ -93,9 +93,9 @@ pri func encoder.is_mostly_utf8!(wb: roslice base.u8, pos: base.u64, length: bas
|
||||
size_utf8 ~mod+= bytes_read
|
||||
}
|
||||
}
|
||||
return this.sf.f64_gt(
|
||||
a: this.sf.f64_from_u64(a: size_utf8),
|
||||
b: this.sf.f64_mul(a: LC_F64_UTF8_FRAC, b: this.sf.f64_from_u64(a: args.length))) <> 0
|
||||
return this.bc.f64_gt(
|
||||
a: this.bc.f64_from_u64(a: size_utf8),
|
||||
b: this.bc.f64_mul(a: LC_F64_UTF8_FRAC, b: this.bc.f64_from_u64(a: args.length))) <> 0
|
||||
}
|
||||
|
||||
// Mirrors UTF8Position in literal_cost.c.
|
||||
@@ -154,17 +154,17 @@ pri func encoder.lc_finish_cost!(wb: slice base.u8, cost_off: base.u64, i: base.
|
||||
var lit_cost : base.u64
|
||||
|
||||
lit_cost = args.lit_cost
|
||||
if this.sf.f64_lt(a: lit_cost, b: LC_F64_1) <> 0 {
|
||||
lit_cost = this.sf.f64_mul(a: lit_cost, b: LC_F64_0_5)
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_5)
|
||||
if this.bc.f64_lt(a: lit_cost, b: LC_F64_1) <> 0 {
|
||||
lit_cost = this.bc.f64_mul(a: lit_cost, b: LC_F64_0_5)
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_5)
|
||||
}
|
||||
// Make the first bytes more expensive -- seems to help, not sure why.
|
||||
// Perhaps because the entropy source is changing its properties rapidly
|
||||
// in the beginning of the file, perhaps because the beginning of the data
|
||||
// is a statistical "anomaly".
|
||||
if args.utf8 and (args.i < 2000) {
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: this.sf.f64_add(a: LC_F64_0_35,
|
||||
b: this.sf.f64_mul(a: LC_F64_MULT, b: this.sf.f64_from_u64(a: args.i))))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: this.bc.f64_add(a: LC_F64_0_35,
|
||||
b: this.bc.f64_mul(a: LC_F64_MULT, b: this.bc.f64_from_u64(a: args.i))))
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: args.cost_off ~mod+ (args.i ~mod* 4), v: this.sf.f64_to_f32(a: lit_cost))
|
||||
}
|
||||
@@ -278,8 +278,8 @@ pri func encoder.estimate_bit_costs_for_literals_utf8!(wb: slice base.u8, pos: b
|
||||
iw = iwu2
|
||||
}
|
||||
lit_cost = this.bc.fast_log2_memo!(v: iw)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_02905)
|
||||
lit_cost = this.bc.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_02905)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: true)
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -339,8 +339,8 @@ pri func encoder.estimate_bit_costs_for_literals!(wb: slice base.u8, pos: base.u
|
||||
histo = 1
|
||||
}
|
||||
lit_cost = this.bc.fast_log2_memo!(v: in_window)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_029)
|
||||
lit_cost = this.bc.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_029)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: false)
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
@@ -103,6 +103,10 @@ pri struct metablock_builder?(
|
||||
cost : array[256] base.u64,
|
||||
insert_cost : array[35200] base.u64,
|
||||
|
||||
// FindBlocks' memoized (block_switch_bitcost + insert_cost[i]) sums
|
||||
// (see find_blocks).
|
||||
capped_plus : array[35200] base.u64,
|
||||
|
||||
// RemapBlockIds' new_id array.
|
||||
new_id : array[256] base.u16,
|
||||
|
||||
@@ -481,12 +485,12 @@ pri func metablock_builder.compute_distance_cost!(wb: slice base.u8, cmds: rosli
|
||||
postfix_bits: args.new_npostfix) & 0xFFFF) as base.u32
|
||||
}
|
||||
this.h_add!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE, val: (dist_prefix & 0x3FF) as base.u64)
|
||||
extra_bits = this.sf.f64_add(a: extra_bits, b: this.sf.f64_from_u32(a: dist_prefix >> 10))
|
||||
extra_bits = this.bc.f64_add(a: extra_bits, b: this.bc.f64_from_u32(a: dist_prefix >> 10))
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
this.dist_cost = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE)
|
||||
this.dist_cost = this.sf.f64_add(a: this.dist_cost, b: extra_bits)
|
||||
this.dist_cost = this.bc.f64_add(a: this.dist_cost, b: extra_bits)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -624,7 +628,7 @@ pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: rosl
|
||||
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
|
||||
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
|
||||
new_npostfix: npostfix, new_ndirect: ndirect, new_max_distance: this.dist_max_distance, tmp: tmp)
|
||||
if (not success) or (this.sf.f64_gt(a: this.dist_cost, b: best_dist_cost) <> 0) {
|
||||
if (not success) or (this.bc.f64_gt(a: this.dist_cost, b: best_dist_cost) <> 0) {
|
||||
break
|
||||
}
|
||||
best_dist_cost = this.dist_cost
|
||||
@@ -645,7 +649,7 @@ pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: rosl
|
||||
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
|
||||
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
|
||||
new_npostfix: orig_npostfix, new_ndirect: orig_ndirect, new_max_distance: orig_md, tmp: tmp)
|
||||
if this.sf.f64_lt(a: this.dist_cost, b: best_dist_cost) <> 0 {
|
||||
if this.bc.f64_lt(a: this.dist_cost, b: best_dist_cost) <> 0 {
|
||||
best_npostfix = orig_npostfix
|
||||
best_ndirect = orig_ndirect
|
||||
best_asm = orig_asm
|
||||
|
||||
@@ -201,20 +201,20 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
this.gbs_add_histogram!(wb: args.wb, dst: tmp, src: this.gbs_histo_off(c: args.c, ix: last1 ~mod+ i), n: data_size)
|
||||
}
|
||||
this.gbs_comb_entropy[jx & 31] = this.gbs_entropy!(wb: args.wb, off: tmp, size: alphabet_size)
|
||||
d = this.sf.f64_sub(
|
||||
a: this.sf.f64_sub(a: this.gbs_comb_entropy[jx & 31], b: e),
|
||||
d = this.bc.f64_sub(
|
||||
a: this.bc.f64_sub(a: this.gbs_comb_entropy[jx & 31], b: e),
|
||||
b: this.gbs_last_entropy[((le_base as base.u64) ~mod+ jx) & 127])
|
||||
if j == 0 {
|
||||
if nc == 1 {
|
||||
diff0 = d
|
||||
} else {
|
||||
diff0 = this.sf.f64_add(a: diff0, b: d)
|
||||
diff0 = this.bc.f64_add(a: diff0, b: d)
|
||||
}
|
||||
} else {
|
||||
if nc == 1 {
|
||||
diff1 = d
|
||||
} else {
|
||||
diff1 = this.sf.f64_add(a: diff1, b: d)
|
||||
diff1 = this.bc.f64_add(a: diff1, b: d)
|
||||
}
|
||||
}
|
||||
j ~mod+= 1
|
||||
@@ -223,8 +223,8 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
}
|
||||
|
||||
if (this.gbs_num_types[args.c] < this.gbs_max_block_types[args.c]) and
|
||||
(this.sf.f64_gt(a: diff0, b: threshold) <> 0) and
|
||||
(this.sf.f64_gt(a: diff1, b: threshold) <> 0) {
|
||||
(this.bc.f64_gt(a: diff0, b: threshold) <> 0) and
|
||||
(this.bc.f64_gt(a: diff1, b: threshold) <> 0) {
|
||||
// Create new block.
|
||||
this.poke_u32!(s: args.wb, i: lengths_off ~mod+ (num_blocks ~mod* 4),
|
||||
v: (this.gbs_block_size[args.c] & 0xFFFF_FFFF) as base.u32)
|
||||
@@ -250,7 +250,7 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
this.gbs_merge_last_count[args.c] = 0
|
||||
this.gbs_target_block_size[args.c] = this.gbs_min_block_size[args.c] as base.u64
|
||||
|
||||
} else if this.sf.f64_lt(a: diff1, b: this.sf.f64_sub(a: diff0, b: GBS_F64_20)) <> 0 {
|
||||
} else if this.bc.f64_lt(a: diff1, b: this.bc.f64_sub(a: diff0, b: GBS_F64_20)) <> 0 {
|
||||
// Combine this block with second last block.
|
||||
this.poke_u32!(s: args.wb, i: lengths_off ~mod+ (num_blocks ~mod* 4),
|
||||
v: (this.gbs_block_size[args.c] & 0xFFFF_FFFF) as base.u32)
|
||||
|
||||
@@ -232,7 +232,7 @@ pri func encoder.z_set_cost_from_histogram!(which: base.u32, histogram_size: bas
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
missing_symbol_cost = this.sf.f32_add(a: this.sf.f64_to_f32(a: this.bc.fast_log2(v: missing_symbol_sum)), b: Z_F32_2)
|
||||
missing_symbol_cost = this.bc.f32_add(a: this.sf.f64_to_f32(a: this.bc.fast_log2(v: missing_symbol_sum)), b: Z_F32_2)
|
||||
i = 0
|
||||
while i < args.histogram_size {
|
||||
h = this.z_hist(which: args.which, i: i)
|
||||
@@ -240,9 +240,9 @@ pri func encoder.z_set_cost_from_histogram!(which: base.u32, histogram_size: bas
|
||||
this.z_set_cost!(which: args.which, i: i, v: missing_symbol_cost)
|
||||
} else {
|
||||
// Shannon bits for this symbol.
|
||||
cost = this.sf.f32_sub(a: log2sum, b: this.sf.f64_to_f32(a: this.bc.fast_log2(v: h as base.u64)))
|
||||
cost = this.bc.f32_sub(a: log2sum, b: this.sf.f64_to_f32(a: this.bc.fast_log2(v: h as base.u64)))
|
||||
// Cannot be coded with less than 1 bit.
|
||||
if this.sf.f32_lt(a: cost, b: Z_F32_1) <> 0 {
|
||||
if this.bc.f32_lt(a: cost, b: Z_F32_1) <> 0 {
|
||||
cost = Z_F32_1
|
||||
}
|
||||
this.z_set_cost!(which: args.which, i: i, v: cost)
|
||||
@@ -322,10 +322,10 @@ pri func encoder.zopfli_cost_model_set_from_commands!(wb: slice base.u8, positio
|
||||
i = 0
|
||||
while i < this.z_num_bytes {
|
||||
b = this.rb_byte(wb: args.wb, pos: args.position ~mod+ i)
|
||||
literal_carry = this.sf.f32_add(a: literal_carry, b: this.z_cost_literal[b])
|
||||
lc_i1 = this.sf.f32_add(a: lc_i, b: literal_carry)
|
||||
literal_carry = this.bc.f32_add(a: literal_carry, b: this.z_cost_literal[b])
|
||||
lc_i1 = this.bc.f32_add(a: lc_i, b: literal_carry)
|
||||
this.poke_u32!(s: args.wb, i: this.z_lit_off ~mod+ ((i ~mod+ 1) ~mod* 4), v: lc_i1)
|
||||
literal_carry = this.sf.f32_sub(a: literal_carry, b: this.sf.f32_sub(a: lc_i1, b: lc_i))
|
||||
literal_carry = this.bc.f32_sub(a: literal_carry, b: this.bc.f32_sub(a: lc_i1, b: lc_i))
|
||||
lc_i = lc_i1
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -346,10 +346,10 @@ pri func encoder.zopfli_cost_model_set_from_literal_costs!(wb: slice base.u8, po
|
||||
lc_i = 0
|
||||
i = 0
|
||||
while i < this.z_num_bytes {
|
||||
literal_carry = this.sf.f32_add(a: literal_carry, b: this.lit_cost_at(wb: args.wb, i: i ~mod+ 1))
|
||||
lc_i1 = this.sf.f32_add(a: lc_i, b: literal_carry)
|
||||
literal_carry = this.bc.f32_add(a: literal_carry, b: this.lit_cost_at(wb: args.wb, i: i ~mod+ 1))
|
||||
lc_i1 = this.bc.f32_add(a: lc_i, b: literal_carry)
|
||||
this.poke_u32!(s: args.wb, i: this.z_lit_off ~mod+ ((i ~mod+ 1) ~mod* 4), v: lc_i1)
|
||||
literal_carry = this.sf.f32_sub(a: literal_carry, b: this.sf.f32_sub(a: lc_i1, b: lc_i))
|
||||
literal_carry = this.bc.f32_sub(a: literal_carry, b: this.bc.f32_sub(a: lc_i1, b: lc_i))
|
||||
lc_i = lc_i1
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -368,7 +368,7 @@ pri func encoder.zopfli_cost_model_set_from_literal_costs!(wb: slice base.u8, po
|
||||
|
||||
// Mirrors ZopfliCostModelGetLiteralCosts in backward_references_hq.c.
|
||||
pri func encoder.z_literal_costs(wb: roslice base.u8, from: base.u64, to: base.u64) base.u32 {
|
||||
return this.sf.f32_sub(a: this.lit_cost_at(wb: args.wb, i: args.to), b: this.lit_cost_at(wb: args.wb, i: args.from))
|
||||
return this.bc.f32_sub(a: this.lit_cost_at(wb: args.wb, i: args.to), b: this.lit_cost_at(wb: args.wb, i: args.from))
|
||||
}
|
||||
|
||||
// Mirrors UpdateZopfliNode in backward_references_hq.c.
|
||||
@@ -420,7 +420,7 @@ pri func encoder.start_pos_queue_push!() {
|
||||
while i < len {
|
||||
a = (offset & 7) as base.u32
|
||||
b = ((offset ~mod+ 1) & 7) as base.u32
|
||||
if this.sf.f32_gt(a: this.zq_costdiff[a], b: this.zq_costdiff[b]) <> 0 {
|
||||
if this.bc.f32_gt(a: this.zq_costdiff[a], b: this.zq_costdiff[b]) <> 0 {
|
||||
this.start_pos_queue_swap!(a: a, b: b)
|
||||
}
|
||||
offset ~mod+= 1
|
||||
@@ -473,7 +473,7 @@ pri func encoder.compute_minimum_copy_length(wb: roslice base.u8, start_cost: ba
|
||||
next_len_bucket = 4
|
||||
next_len_offset = 10
|
||||
while ((args.pos ~mod+ len) <= args.num_bytes) and
|
||||
(this.sf.f32_le(a: this.zn_u(wb: args.wb, i: args.pos ~mod+ len), b: min_cost) <> 0) {
|
||||
(this.bc.f32_le(a: this.zn_u(wb: args.wb, i: args.pos ~mod+ len), b: min_cost) <> 0) {
|
||||
// We already reached (pos + len) with no more cost than the minimum
|
||||
// possible cost of reaching anything from this pos, so there is no
|
||||
// point in looking for lengths <= len.
|
||||
@@ -481,7 +481,7 @@ pri func encoder.compute_minimum_copy_length(wb: roslice base.u8, start_cost: ba
|
||||
if len == next_len_offset {
|
||||
// We reached the next copy length code bucket, so we add one
|
||||
// more extra bit to the minimum cost.
|
||||
min_cost = this.sf.f32_add(a: min_cost, b: Z_F32_1)
|
||||
min_cost = this.bc.f32_add(a: min_cost, b: Z_F32_1)
|
||||
next_len_offset ~mod+= next_len_bucket
|
||||
next_len_bucket ~mod*= 2
|
||||
}
|
||||
@@ -553,10 +553,10 @@ pri func encoder.evaluate_node!(wb: slice base.u8, block_start: base.u64, pos: b
|
||||
this.zn_set_u!(wb: args.wb, i: args.pos, v: this.compute_distance_shortcut(
|
||||
wb: args.wb, block_start: args.block_start, pos: args.pos, max_backward_limit: args.max_backward_limit))
|
||||
lc = this.z_literal_costs(wb: args.wb, from: 0, to: args.pos)
|
||||
if this.sf.f32_le(a: node_cost, b: lc) <> 0 {
|
||||
if this.bc.f32_le(a: node_cost, b: lc) <> 0 {
|
||||
this.zp_pos = args.pos
|
||||
this.zp_cost = node_cost
|
||||
this.zp_costdiff = this.sf.f32_sub(a: node_cost, b: lc)
|
||||
this.zp_costdiff = this.bc.f32_sub(a: node_cost, b: lc)
|
||||
this.compute_distance_cache!(wb: args.wb, pos: args.pos)
|
||||
this.start_pos_queue_push!()
|
||||
}
|
||||
@@ -586,7 +586,6 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
var base_cost : base.u32
|
||||
var best_len : base.u64
|
||||
var j : base.u64
|
||||
var idx : base.u32[..= 3]
|
||||
var backward : base.u64
|
||||
var bw32 : base.u32
|
||||
var prev_ix : base.u64
|
||||
@@ -607,6 +606,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
var max_match_len : base.u64
|
||||
var len_code : base.u64
|
||||
var gap : base.u64
|
||||
var dcs : array[4] base.u32
|
||||
var prev_copycode : base.u32
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
gap = this.dict_total_size
|
||||
@@ -626,8 +627,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
pos: args.pos, max_backward_limit: args.max_backward_limit)
|
||||
|
||||
q = this.start_pos_queue_at(k: 0)
|
||||
min_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: this.zq_cost[q], b: this.z_min_cost_cmd),
|
||||
min_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: this.zq_cost[q], b: this.z_min_cost_cmd),
|
||||
b: this.z_literal_costs(wb: args.wb, from: this.zq_pos[q], to: args.pos))
|
||||
min_len = this.compute_minimum_copy_length(wb: args.wb, start_cost: min_cost, num_bytes: args.num_bytes, pos: args.pos)
|
||||
|
||||
@@ -639,27 +640,32 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
start = this.zq_pos[q]
|
||||
inscode = this.get_insert_length_code(insertlen: args.pos ~mod- start)
|
||||
start_costdiff = this.zq_costdiff[q]
|
||||
base_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: start_costdiff, b: this.sf.f32_from_u32(a: INS_EXTRA[inscode.min(no_more_than: 23)])),
|
||||
base_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: start_costdiff, b: F32_OF_SMALL_INT[INS_EXTRA[inscode.min(no_more_than: 23)]]),
|
||||
b: this.z_literal_costs(wb: args.wb, from: 0, to: args.pos))
|
||||
|
||||
// Look for last distance matches using the distance cache from this
|
||||
// starting position.
|
||||
best_len = min_len ~mod- 1
|
||||
dcs[0] = this.zq_dc[((q as base.u32) << 2) | 0]
|
||||
dcs[1] = this.zq_dc[((q as base.u32) << 2) | 1]
|
||||
dcs[2] = this.zq_dc[((q as base.u32) << 2) | 2]
|
||||
dcs[3] = this.zq_dc[((q as base.u32) << 2) | 3]
|
||||
// continuation and the break condition only depend on best_len, so
|
||||
// they are computed again only when best_len changes.
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
j = 0
|
||||
while (j < 16) and (best_len < max_len) {
|
||||
idx = Z_DISTANCE_CACHE_INDEX[j]
|
||||
bw32 = this.zq_dc[((q as base.u32) << 2) | idx] ~mod+ Z_DISTANCE_CACHE_OFFSET[j]
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
bw32 = dcs[Z_DISTANCE_CACHE_INDEX[j]] ~mod+ Z_DISTANCE_CACHE_OFFSET[j]
|
||||
backward = bw32 as base.u64
|
||||
if bw32 >= 0x8000_0000 {
|
||||
backward |= 0xFFFF_FFFF_0000_0000
|
||||
}
|
||||
prev_ix = cur_ix ~mod- backward
|
||||
len = 0
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
if backward > (dictionary_start ~mod+ gap) {
|
||||
// Word dictionary -> ignore.
|
||||
j ~mod+= 1
|
||||
@@ -691,20 +697,26 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
j ~mod+= 1
|
||||
continue
|
||||
}
|
||||
dist_cost = this.sf.f32_add(a: base_cost, b: this.z_cost_dist[j])
|
||||
dist_cost = this.bc.f32_add(a: base_cost, b: this.z_cost_dist[j])
|
||||
l = best_len ~mod+ 1
|
||||
// The cost only depends on copycode (inscode and j are fixed), so
|
||||
// it is computed again only when copycode changes.
|
||||
prev_copycode = 0xFFFF_FFFF
|
||||
while l <= len {
|
||||
copycode = this.get_copy_length_code(copylen: l)
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: j == 0)
|
||||
if cmdcode < 128 {
|
||||
cost = base_cost
|
||||
} else {
|
||||
cost = dist_cost
|
||||
if copycode <> prev_copycode {
|
||||
prev_copycode = copycode
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: j == 0)
|
||||
if cmdcode < 128 {
|
||||
cost = base_cost
|
||||
} else {
|
||||
cost = dist_cost
|
||||
}
|
||||
cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: cost, b: F32_OF_SMALL_INT[COPY_EXTRA[copycode.min(no_more_than: 23)]]),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
}
|
||||
cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: cost, b: this.sf.f32_from_u32(a: COPY_EXTRA[copycode.min(no_more_than: 23)])),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
if this.sf.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ l)) <> 0 {
|
||||
if this.bc.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ l)) <> 0 {
|
||||
this.update_zopfli_node!(wb: args.wb, pos: args.pos, start_pos: start, len: l,
|
||||
len_code: l, dist: backward, short_code: j ~mod+ 1, cost: cost)
|
||||
result = result.max(no_less_than: l)
|
||||
@@ -712,6 +724,7 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
best_len = l
|
||||
l ~mod+= 1
|
||||
}
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
j ~mod+= 1
|
||||
}
|
||||
|
||||
@@ -739,8 +752,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
postfix_bits: this.distance_postfix_bits)
|
||||
dist_symbol = (pe & 0xFFFF) as base.u32
|
||||
distnumextra = dist_symbol >> 10
|
||||
dist_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: base_cost, b: this.sf.f32_from_u32(a: distnumextra)),
|
||||
dist_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: base_cost, b: this.bc.f32_from_u32(a: distnumextra)),
|
||||
b: this.z_cost_dist[this.min32_543(a: dist_symbol & 0x3FF)])
|
||||
|
||||
// Try all copy lengths up until the maximum copy length
|
||||
@@ -751,17 +764,22 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
if (len < max_match_len) and (is_dictionary_match or (max_match_len > max_zopfli_len)) {
|
||||
len = max_match_len
|
||||
}
|
||||
// As above, the cost only depends on copycode.
|
||||
prev_copycode = 0xFFFF_FFFF
|
||||
while len <= max_match_len {
|
||||
len_code = len
|
||||
if is_dictionary_match {
|
||||
len_code = this.bm_length_code(wb: args.wb, off: moff)
|
||||
}
|
||||
copycode = this.get_copy_length_code(copylen: len_code)
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: false)
|
||||
cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: dist_cost, b: this.sf.f32_from_u32(a: COPY_EXTRA[copycode.min(no_more_than: 23)])),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
if this.sf.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ len)) <> 0 {
|
||||
if copycode <> prev_copycode {
|
||||
prev_copycode = copycode
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: false)
|
||||
cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: dist_cost, b: F32_OF_SMALL_INT[COPY_EXTRA[copycode.min(no_more_than: 23)]]),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
}
|
||||
if this.bc.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ len)) <> 0 {
|
||||
this.update_zopfli_node!(wb: args.wb, pos: args.pos, start_pos: start, len: len,
|
||||
len_code: len_code, dist: dist, short_code: 0, cost: cost)
|
||||
result = result.max(no_less_than: len)
|
||||
|
||||
@@ -172,50 +172,41 @@ pub func arith.f32_mul(a: base.u32, b: base.u32) base.u32 {
|
||||
|
||||
// f32_add_mags returns (sign(a) * (|a| + |b|)), for normal a and b.
|
||||
pri func arith.f32_add_mags(a: base.u32, b: base.u32) base.u32 {
|
||||
var ea : base.u32[..= 0x100]
|
||||
var eb : base.u32[..= 0xFF]
|
||||
var ma : base.u32[..= 0x3FFF_FFC0]
|
||||
var mb : base.u32[..= 0x3FFF_FFC0]
|
||||
var d : base.u32[..= 0xFF]
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
var z : base.u32
|
||||
var rb : base.u32
|
||||
|
||||
ea = (args.a >> 23) & 0xFF
|
||||
eb = (args.b >> 23) & 0xFF
|
||||
// The significands, with their implicit bit at bit 29 and 6 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = ((args.a & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
mb = ((args.b & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
if ea < eb {
|
||||
d = (eb ~mod- ea) & 0xFF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
// Order the magnitudes so that |x| >= |y|.
|
||||
x = args.a & F32_ABS
|
||||
y = args.b & F32_ABS
|
||||
if y > x {
|
||||
x = args.b & F32_ABS
|
||||
y = args.a & F32_ABS
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 25 {
|
||||
// |y| is less than half an ULP of |x|, so the sum rounds to x.
|
||||
return (args.a & F32_SIGN) | x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 24 is exact, and the sum is less than (1 << 64).
|
||||
s = ((((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod+
|
||||
(((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
// Round to nearest, ties to even: add just under half an ULP, plus one
|
||||
// if the truncated result is odd. The result is in [1 << 23 ..= 1 << 24]
|
||||
// and adding it (whose bit 23 is the implicit bit) to the shifted
|
||||
// exponent carries any rounding overflow into the exponent field.
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0xFF
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
z = ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
}
|
||||
if d >= 31 {
|
||||
mb = 1
|
||||
} else if d > 0 {
|
||||
if (mb ~mod<< (32 - d)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
}
|
||||
z = (ma & 0x3FFF_FFFF) + (mb & 0x3FFF_FFFF)
|
||||
if z >= 0x4000_0000 {
|
||||
ea += 1
|
||||
z = (z >> 1) | (z & 1)
|
||||
}
|
||||
rb = z & 0x3F
|
||||
z = (z ~mod+ 0x20) >> 6
|
||||
if rb == 0x20 {
|
||||
z &= 0xFFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 255] and z is in [1 << 23 ..= 1 << 24].
|
||||
z = ((ea ~mod- 1) ~mod<< 23) ~mod+ z
|
||||
if z >= F32_INF {
|
||||
z = F32_INF
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user