code: def calc_piece_size(total_size, max_pieces, min_piece_size, max_piece_size):
      
      ps = 1 << max(0, math.ceil(math.log(total_size / max_pieces, 2)))
      if ps < min_piece_size:
          ps = min_piece_size
      if ps > max_piece_size:
          ps = max_piece_size
      return ps
docs: Calculate piece size