refactor: move duplicated code into macro (k_mag)

This commit is contained in:
Gregor Weiss 2024-09-23 14:02:07 +02:00
parent 2ddd711267
commit 11f4a68fd4
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756

View file

@ -138,6 +138,37 @@
xi_v = SIG2XI[xi_v]; \ xi_v = SIG2XI[xi_v]; \
} }
#define calc_k_mag(stripe, bit_mask, k, l, k_mag) \
{ \
k_mag = CONTEXT_MAG; \
if(stripe[k].delta & bit_mask) \
{ \
k_mag += 2; \
} \
else \
{ \
if((bit_mask & (stripe[k].sigma >> 1)) || (bit_mask & (stripe[k].sigma << 1)) || \
(stripe[k].stripe_l->sigma & bit_mask) || (stripe[k].stripe_r->sigma & bit_mask) || \
(bit_mask & (stripe[k].stripe_l->sigma >> 1)) || (bit_mask & (stripe[k].stripe_l->sigma << 1)) || \
(bit_mask & (stripe[k].stripe_r->sigma >> 1)) || (bit_mask & (stripe[k].stripe_r->sigma << 1))) \
{ \
k_mag += 1; \
} \
else if((l == 3) && ((0x08 & (stripe[k].stripe_u->sigma << 3)) \
|| (0x08 & (stripe[k].stripe_u->stripe_l->sigma << 3)) \
|| (0x08 & (stripe[k].stripe_u->stripe_r->sigma << 3)))) \
{ \
k_mag += 1; \
} \
else if((l == 0) && ((0x01 & (stripe[k].stripe_d->sigma >> 3)) \
|| (0x01 & (stripe[k].stripe_d->stripe_l->sigma >> 3)) \
|| (0x01 & (stripe[k].stripe_d->stripe_r->sigma >> 3)))) \
{ \
k_mag += 1; \
} \
} \
}
/************************************************************************************************************\ /************************************************************************************************************\
|| ____ _ _ ___ ____ ____ _ _ ____ _ ____ ____ _ _ ____ ___ ____ _ _ ___ ____ || || ____ _ _ ___ ____ ____ _ _ ____ _ ____ ____ _ _ ____ ___ ____ _ _ ___ ____ ||
|| |___ \/ | |___ |__/ |\ | |__| | | | | |\ | [__ | |__| |\ | | [__ || || |___ \/ | |___ |__/ |\ | |__| | | | | |\ | [__ | |__| |\ | | [__ ||
@ -1192,34 +1223,7 @@ magnitude_refinement_enc_pass(bwc_coder *const coder, const int8 b)
{ {
if(sig & bit_mask) if(sig & bit_mask)
{ {
k_mag = CONTEXT_MAG; calc_k_mag(stripe, bit_mask, k, l, k_mag);
if(stripe[k].delta & bit_mask)
{
k_mag += 2;
}
else
{
if((bit_mask & (stripe[k].sigma >> 1)) || (bit_mask & (stripe[k].sigma << 1)) ||
(stripe[k].stripe_l->sigma & bit_mask) || (stripe[k].stripe_r->sigma & bit_mask) ||
(bit_mask & (stripe[k].stripe_l->sigma >> 1)) || (bit_mask & (stripe[k].stripe_l->sigma << 1)) ||
(bit_mask & (stripe[k].stripe_r->sigma >> 1)) || (bit_mask & (stripe[k].stripe_r->sigma << 1)))
{
k_mag += 1;
}
else if((l == 3) && ((0x08 & (stripe[k].stripe_u->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_l->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_r->sigma << 3))))
{
k_mag += 1;
}
else if((l == 0) && ((0x01 & (stripe[k].stripe_d->sigma >> 3))
|| (0x01 & (stripe[k].stripe_d->stripe_l->sigma >> 3))
|| (0x01 & (stripe[k].stripe_d->stripe_r->sigma >> 3))))
{
k_mag += 1;
}
}
mq_bit_encode(coder->bitcoder, (uchar)((bit >> l) & 0x01), k_mag); mq_bit_encode(coder->bitcoder, (uchar)((bit >> l) & 0x01), k_mag);
@ -1240,28 +1244,7 @@ magnitude_refinement_enc_pass(bwc_coder *const coder, const int8 b)
{ {
if(sig & bit_mask) if(sig & bit_mask)
{ {
k_mag = CONTEXT_MAG; calc_k_mag(stripe, bit_mask, k, l, k_mag);
if(stripe[k].delta & bit_mask)
{
k_mag += 2;
}
else
{
if((bit_mask & (stripe[k].sigma >> 1)) || (bit_mask & (stripe[k].sigma << 1)) ||
(stripe[k].stripe_l->sigma & bit_mask) || (stripe[k].stripe_r->sigma & bit_mask) ||
(bit_mask & (stripe[k].stripe_l->sigma >> 1)) || (bit_mask & (stripe[k].stripe_l->sigma << 1)) ||
(bit_mask & (stripe[k].stripe_r->sigma >> 1)) || (bit_mask & (stripe[k].stripe_r->sigma << 1)))
{
k_mag += 1;
}
else if((l == 3) && ((0x08 & (stripe[k].stripe_u->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_l->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_r->sigma << 3))))
{
k_mag += 1;
}
}
mq_bit_encode(coder->bitcoder, (uchar)((bit >> l) & 0x01), k_mag); mq_bit_encode(coder->bitcoder, (uchar)((bit >> l) & 0x01), k_mag);
@ -1641,34 +1624,7 @@ magnitude_refinement_dec_pass(bwc_coder *const coder, const int8 b)
{ {
if(sig & bit_mask) if(sig & bit_mask)
{ {
k_mag = CONTEXT_MAG; calc_k_mag(stripe, bit_mask, k, l, k_mag);
if(stripe[k].delta & bit_mask)
{
k_mag += 2;
}
else
{
if((bit_mask & (stripe[k].sigma >> 1)) || (bit_mask & (stripe[k].sigma << 1)) ||
(stripe[k].stripe_l->sigma & bit_mask) || (stripe[k].stripe_r->sigma & bit_mask) ||
(bit_mask & (stripe[k].stripe_l->sigma >> 1)) || (bit_mask & (stripe[k].stripe_l->sigma << 1)) ||
(bit_mask & (stripe[k].stripe_r->sigma >> 1)) || (bit_mask & (stripe[k].stripe_r->sigma << 1)))
{
k_mag += 1;
}
else if((l == 3) && ((0x08 & (stripe[k].stripe_u->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_l->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_r->sigma << 3))))
{
k_mag += 1;
}
else if((l == 0) && ((0x01 & (stripe[k].stripe_d->sigma >> 3))
|| (0x01 & (stripe[k].stripe_d->stripe_l->sigma >> 3))
|| (0x01 & (stripe[k].stripe_d->stripe_r->sigma >> 3))))
{
k_mag += 1;
}
}
stripe[k].bit[b] |= (mq_bit_decode(coder->bitcoder, k_mag) << l); stripe[k].bit[b] |= (mq_bit_decode(coder->bitcoder, k_mag) << l);
@ -1688,28 +1644,7 @@ magnitude_refinement_dec_pass(bwc_coder *const coder, const int8 b)
{ {
if(sig & bit_mask) if(sig & bit_mask)
{ {
k_mag = CONTEXT_MAG; calc_k_mag(stripe, bit_mask, k, l, k_mag);
if(stripe[k].delta & bit_mask)
{
k_mag += 2;
}
else
{
if((bit_mask & (stripe[k].sigma >> 1)) || (bit_mask & (stripe[k].sigma << 1)) ||
(stripe[k].stripe_l->sigma & bit_mask) || (stripe[k].stripe_r->sigma & bit_mask) ||
(bit_mask & (stripe[k].stripe_l->sigma >> 1)) || (bit_mask & (stripe[k].stripe_l->sigma << 1)) ||
(bit_mask & (stripe[k].stripe_r->sigma >> 1)) || (bit_mask & (stripe[k].stripe_r->sigma << 1)))
{
k_mag += 1;
}
else if((l == 3) && ((0x08 & (stripe[k].stripe_u->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_l->sigma << 3))
|| (0x08 & (stripe[k].stripe_u->stripe_r->sigma << 3))))
{
k_mag += 1;
}
}
stripe[k].bit[b] |= (mq_bit_decode(coder->bitcoder, k_mag) << l); stripe[k].bit[b] |= (mq_bit_decode(coder->bitcoder, k_mag) << l);