Simplify some operations using CEIL_POWER_OF_TWO

This CL simplifies some operations by making use of the
CEIL_POWER_OF_TWO macro at some places based on the applicability.

Change-Id: I5848bf258e96bfc5d99948db5e78543daf188fa4
diff --git a/av1/common/av1_loopfilter.c b/av1/common/av1_loopfilter.c
index 52b2029..5dede53 100644
--- a/av1/common/av1_loopfilter.c
+++ b/av1/common/av1_loopfilter.c
@@ -1362,9 +1362,9 @@
   // Ensure that mi_cols/mi_rows are calculated based on frame dimension aligned
   // to MI_SIZE.
   const int plane_mi_cols =
-      (plane_ptr->dst.width + MI_SIZE - 1) >> MI_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(plane_ptr->dst.width, MI_SIZE_LOG2);
   const int plane_mi_rows =
-      (plane_ptr->dst.height + MI_SIZE - 1) >> MI_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(plane_ptr->dst.height, MI_SIZE_LOG2);
   const int y_range = AOMMIN((int)(plane_mi_rows - mi_row), MAX_MIB_SIZE);
   const int x_range = AOMMIN((int)(plane_mi_cols - mi_col), MAX_MIB_SIZE);
   const ptrdiff_t mode_step = 1;
@@ -1955,9 +1955,9 @@
   // Ensure that mi_cols/mi_rows are calculated based on frame dimension aligned
   // to MI_SIZE.
   const int plane_mi_cols =
-      (plane_ptr->dst.width + MI_SIZE - 1) >> MI_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(plane_ptr->dst.width, MI_SIZE_LOG2);
   const int plane_mi_rows =
-      (plane_ptr->dst.height + MI_SIZE - 1) >> MI_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(plane_ptr->dst.height, MI_SIZE_LOG2);
   const int y_range = AOMMIN((int)(plane_mi_rows - mi_row), MAX_MIB_SIZE);
   const int x_range = AOMMIN((int)(plane_mi_cols - mi_col), MAX_MIB_SIZE);
 
diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c
index 8c1780d..49522ea 100644
--- a/av1/common/thread_common.c
+++ b/av1/common/thread_common.c
@@ -338,8 +338,7 @@
     int dir, int lpf_opt_level, AV1LfSync *const lf_sync,
     AV1_DEBLOCKING_PARAMETERS *params_buf, TX_SIZE *tx_buf) {
   const int sb_cols =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2) >>
-      MAX_MIB_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, MAX_MIB_SIZE_LOG2);
   const int r = mi_row >> MAX_MIB_SIZE_LOG2;
   int mi_col, c;
 
@@ -425,8 +424,7 @@
   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
   // Number of superblock rows and cols
   const int sb_rows =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, MAX_MIB_SIZE_LOG2) >>
-      MAX_MIB_SIZE_LOG2;
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, MAX_MIB_SIZE_LOG2);
   int i;
 
   if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
diff --git a/av1/common/tile_common.c b/av1/common/tile_common.c
index 33226c9..6ecead8 100644
--- a/av1/common/tile_common.c
+++ b/av1/common/tile_common.c
@@ -30,12 +30,10 @@
 void av1_get_tile_limits(AV1_COMMON *const cm) {
   const SequenceHeader *const seq_params = cm->seq_params;
   CommonTileParams *const tiles = &cm->tiles;
-  const int mi_cols =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
-  const int mi_rows =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
-  const int sb_cols = mi_cols >> seq_params->mib_size_log2;
-  const int sb_rows = mi_rows >> seq_params->mib_size_log2;
+  const int sb_cols =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
+  const int sb_rows =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
 
   const int sb_size_log2 = seq_params->mib_size_log2 + MI_SIZE_LOG2;
   tiles->max_width_sb = MAX_TILE_WIDTH >> sb_size_log2;
@@ -51,10 +49,8 @@
 void av1_calculate_tile_cols(const SequenceHeader *const seq_params,
                              int cm_mi_rows, int cm_mi_cols,
                              CommonTileParams *const tiles) {
-  int mi_cols = ALIGN_POWER_OF_TWO(cm_mi_cols, seq_params->mib_size_log2);
-  int mi_rows = ALIGN_POWER_OF_TWO(cm_mi_rows, seq_params->mib_size_log2);
-  int sb_cols = mi_cols >> seq_params->mib_size_log2;
-  int sb_rows = mi_rows >> seq_params->mib_size_log2;
+  int sb_cols = CEIL_POWER_OF_TWO(cm_mi_cols, seq_params->mib_size_log2);
+  int sb_rows = CEIL_POWER_OF_TWO(cm_mi_rows, seq_params->mib_size_log2);
   int i;
 
   // This will be overridden if there is at least two columns of tiles
@@ -63,8 +59,7 @@
 
   if (tiles->uniform_spacing) {
     int start_sb;
-    int size_sb = ALIGN_POWER_OF_TWO(sb_cols, tiles->log2_cols);
-    size_sb >>= tiles->log2_cols;
+    int size_sb = CEIL_POWER_OF_TWO(sb_cols, tiles->log2_cols);
     assert(size_sb > 0);
     for (i = 0, start_sb = 0; start_sb < sb_cols; i++) {
       tiles->col_start_sb[i] = start_sb;
@@ -105,13 +100,11 @@
 
 void av1_calculate_tile_rows(const SequenceHeader *const seq_params,
                              int cm_mi_rows, CommonTileParams *const tiles) {
-  int mi_rows = ALIGN_POWER_OF_TWO(cm_mi_rows, seq_params->mib_size_log2);
-  int sb_rows = mi_rows >> seq_params->mib_size_log2;
+  int sb_rows = CEIL_POWER_OF_TWO(cm_mi_rows, seq_params->mib_size_log2);
   int start_sb, size_sb, i;
 
   if (tiles->uniform_spacing) {
-    size_sb = ALIGN_POWER_OF_TWO(sb_rows, tiles->log2_rows);
-    size_sb >>= tiles->log2_rows;
+    size_sb = CEIL_POWER_OF_TWO(sb_rows, tiles->log2_rows);
     assert(size_sb > 0);
     for (i = 0, start_sb = 0; start_sb < sb_rows; i++) {
       tiles->row_start_sb[i] = start_sb;
@@ -152,19 +145,13 @@
 }
 
 int av1_get_sb_rows_in_tile(AV1_COMMON *cm, const TileInfo *tile) {
-  int mi_rows_aligned_to_sb = ALIGN_POWER_OF_TWO(
-      tile->mi_row_end - tile->mi_row_start, cm->seq_params->mib_size_log2);
-  int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params->mib_size_log2;
-
-  return sb_rows;
+  return CEIL_POWER_OF_TWO(tile->mi_row_end - tile->mi_row_start,
+                           cm->seq_params->mib_size_log2);
 }
 
 int av1_get_sb_cols_in_tile(AV1_COMMON *cm, const TileInfo *tile) {
-  int mi_cols_aligned_to_sb = ALIGN_POWER_OF_TWO(
-      tile->mi_col_end - tile->mi_col_start, cm->seq_params->mib_size_log2);
-  int sb_cols = mi_cols_aligned_to_sb >> cm->seq_params->mib_size_log2;
-
-  return sb_cols;
+  return CEIL_POWER_OF_TWO(tile->mi_col_end - tile->mi_col_start,
+                           cm->seq_params->mib_size_log2);
 }
 
 AV1PixelRect av1_get_tile_rect(const TileInfo *tile_info, const AV1_COMMON *cm,
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index fc988d7..5665082 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1891,10 +1891,8 @@
                        width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
 #endif
   if (cm->width != width || cm->height != height) {
-    const int new_mi_rows =
-        ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
-    const int new_mi_cols =
-        ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
+    const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
+    const int new_mi_cols = CEIL_POWER_OF_TWO(width, MI_SIZE_LOG2);
 
     // Allocations in av1_alloc_context_buffers() depend on individual
     // dimensions as well as the overall size.
@@ -2075,12 +2073,10 @@
     AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
   const SequenceHeader *const seq_params = cm->seq_params;
   CommonTileParams *const tiles = &cm->tiles;
-  int width_mi =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
-  int height_mi =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
-  int width_sb = width_mi >> seq_params->mib_size_log2;
-  int height_sb = height_mi >> seq_params->mib_size_log2;
+  int width_sb =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
+  int height_sb =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
 
   av1_get_tile_limits(cm);
   tiles->uniform_spacing = aom_rb_read_bit(rb);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index f13c9d7..67d7e7a 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2163,12 +2163,10 @@
 
 static AOM_INLINE void write_tile_info_max_tile(
     const AV1_COMMON *const cm, struct aom_write_bit_buffer *wb) {
-  int width_mi =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2);
-  int height_mi =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2);
-  int width_sb = width_mi >> cm->seq_params->mib_size_log2;
-  int height_sb = height_mi >> cm->seq_params->mib_size_log2;
+  int width_sb =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2);
+  int height_sb =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2);
   int size_sb, i;
   const CommonTileParams *const tiles = &cm->tiles;
 
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 9571060..1cc2ad9 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -257,9 +257,8 @@
     tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
     tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
   } else {
-    int mi_cols =
-        ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
-    int sb_cols = mi_cols >> seq_params->mib_size_log2;
+    int sb_cols =
+        CEIL_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
     int size_sb, j = 0;
     tiles->uniform_spacing = 0;
     for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
@@ -279,9 +278,8 @@
     tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
     tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
   } else {
-    int mi_rows =
-        ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
-    int sb_rows = mi_rows >> seq_params->mib_size_log2;
+    int sb_rows =
+        CEIL_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
     int size_sb, j = 0;
     for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
       tiles->row_start_sb[i] = start_sb;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 7eb63ba..b298c3d 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -629,8 +629,7 @@
     AV1LfSync *lf_sync = &mt_info->lf_row_sync;
     // Number of superblock rows
     const int sb_rows =
-        ALIGN_POWER_OF_TWO(cm->height >> MI_SIZE_LOG2, MAX_MIB_SIZE_LOG2) >>
-        MAX_MIB_SIZE_LOG2;
+        CEIL_POWER_OF_TWO(cm->height >> MI_SIZE_LOG2, MAX_MIB_SIZE_LOG2);
     PrimaryMultiThreadInfo *const p_mt_info = &cpi->ppi->p_mt_info;
     int num_lf_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LPF);
 
@@ -926,10 +925,8 @@
 // Computes the max number of enc workers possible for each resolution.
 static AOM_INLINE int compute_max_num_enc_workers(
     CommonModeInfoParams *const mi_params, int mib_size_log2) {
-  int num_sb_rows =
-      ALIGN_POWER_OF_TWO(mi_params->mi_rows, mib_size_log2) >> mib_size_log2;
-  int num_sb_cols =
-      ALIGN_POWER_OF_TWO(mi_params->mi_cols, mib_size_log2) >> mib_size_log2;
+  int num_sb_rows = CEIL_POWER_OF_TWO(mi_params->mi_rows, mib_size_log2);
+  int num_sb_cols = CEIL_POWER_OF_TWO(mi_params->mi_cols, mib_size_log2);
 
   return AOMMIN((num_sb_cols + 1) >> 1, num_sb_rows);
 }
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index ffdf418..ff8ed96 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -1026,10 +1026,8 @@
 int av1_get_unit_rows_in_tile(const TileInfo *tile,
                               const BLOCK_SIZE fp_block_size) {
   const int unit_height_log2 = mi_size_high_log2[fp_block_size];
-  const int unit_height = 1 << unit_height_log2;
   const int mi_rows = tile->mi_row_end - tile->mi_row_start;
-  // Calculate (int)ceil((double)mi_rows / unit_height).
-  const int unit_rows = (mi_rows + unit_height - 1) >> unit_height_log2;
+  const int unit_rows = CEIL_POWER_OF_TWO(mi_rows, unit_height_log2);
 
   return unit_rows;
 }
@@ -1037,10 +1035,8 @@
 int av1_get_unit_cols_in_tile(const TileInfo *tile,
                               const BLOCK_SIZE fp_block_size) {
   const int unit_width_log2 = mi_size_wide_log2[fp_block_size];
-  const int unit_width = 1 << unit_width_log2;
   const int mi_cols = tile->mi_col_end - tile->mi_col_start;
-  // Calculate (int)ceil((double)mi_cols / unit_width).
-  const int unit_cols = (mi_cols + unit_width - 1) >> unit_width_log2;
+  const int unit_cols = CEIL_POWER_OF_TWO(mi_cols, unit_width_log2);
 
   return unit_cols;
 }
diff --git a/av1/encoder/tokenize.h b/av1/encoder/tokenize.h
index 6451b9d..f675c48 100644
--- a/av1/encoder/tokenize.h
+++ b/av1/encoder/tokenize.h
@@ -109,8 +109,8 @@
   const int shift = sb_size_log2 - 4;
   const int sb_size = 1 << sb_size_log2;
   const int sb_size_square = sb_size * sb_size;
-  const int sb_rows = ALIGN_POWER_OF_TWO(mb_rows, shift) >> shift;
-  const int sb_cols = ALIGN_POWER_OF_TWO(mb_cols, shift) >> shift;
+  const int sb_rows = CEIL_POWER_OF_TWO(mb_rows, shift);
+  const int sb_cols = CEIL_POWER_OF_TWO(mb_cols, shift);
 
   // One palette token for each pixel. There can be palettes on two planes.
   const int sb_palette_toks = AOMMIN(2, num_planes) * sb_size_square;
@@ -121,9 +121,8 @@
 // Allocate memory for token related info.
 static AOM_INLINE void alloc_token_info(AV1_COMMON *cm, TokenInfo *token_info,
                                         unsigned int tokens_required) {
-  int mi_rows_aligned_to_sb =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2);
-  int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params->mib_size_log2;
+  int sb_rows =
+      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, cm->seq_params->mib_size_log2);
   token_info->tokens_allocated = tokens_required;
 
   CHECK_MEM_ERROR(cm, token_info->tile_tok[0][0],