u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de, Leo Liang <ycliang@andestech.com>
Cc: Bin Meng <bmeng.cn@gmail.com>,
	Damien Le Moal <Damien.LeMoal@wdc.com>,
	Lukasz Majewski <lukma@denx.de>,
	Sean Anderson <seanga2@gmail.com>
Subject: [PATCH v2 2/4] k210: clk: Refactor out_of_spec tests
Date: Sat, 11 Sep 2021 13:20:01 -0400	[thread overview]
Message-ID: <20210911172003.899301-2-seanga2@gmail.com> (raw)
In-Reply-To: <20210911172003.899301-1-seanga2@gmail.com>

Everything here sits in a while (true) loop. However, this introduces a
couple of layers of indentation. We can simplify the code by introducing a
single goto instead of using continue/break. This will also make adding
loops in the next patch easier.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

(no changes since v1)

 drivers/clk/clk_kendryte.c | 105 ++++++++++++++++++-------------------
 1 file changed, 52 insertions(+), 53 deletions(-)

diff --git a/drivers/clk/clk_kendryte.c b/drivers/clk/clk_kendryte.c
index 2caa21aec9..69691c4a04 100644
--- a/drivers/clk/clk_kendryte.c
+++ b/drivers/clk/clk_kendryte.c
@@ -709,6 +709,10 @@ TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in,
 		 * Whether we swapped r and od while enforcing frequency limits
 		 */
 		bool swapped = false;
+		/*
+		 * Whether the intermediate frequencies are out-of-spec
+		 */
+		bool out_of_spec;
 		u64 last_od = od;
 		u64 last_r = r;
 
@@ -767,76 +771,71 @@ TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in,
 		 * aren't in spec, try swapping r and od. If everything is
 		 * in-spec, calculate the relative error.
 		 */
-		while (true) {
+again:
+		out_of_spec = false;
+		if (r > max_r) {
+			out_of_spec = true;
+		} else {
 			/*
-			 * Whether the intermediate frequencies are out-of-spec
+			 * There is no way to only divide once; we need
+			 * to examine the frequency with and without the
+			 * effect of od.
 			 */
-			bool out_of_spec = false;
+			u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
 
-			if (r > max_r) {
+			if (vco > 1750000000 || vco < 340000000)
 				out_of_spec = true;
-			} else {
-				/*
-				 * There is no way to only divide once; we need
-				 * to examine the frequency with and without the
-				 * effect of od.
-				 */
-				u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r);
+		}
 
-				if (vco > 1750000000 || vco < 340000000)
-					out_of_spec = true;
+		if (out_of_spec) {
+			u64 new_r, new_od;
+
+			if (!swapped) {
+				u64 tmp = r;
+
+				r = od;
+				od = tmp;
+				swapped = true;
+				goto again;
 			}
 
-			if (out_of_spec) {
-				if (!swapped) {
-					u64 tmp = r;
-
-					r = od;
-					od = tmp;
-					swapped = true;
-					continue;
-				} else {
-					/*
-					 * Try looking ahead to see if there are
-					 * additional factors for the same
-					 * product.
-					 */
-					if (i + 1 < ARRAY_SIZE(factors)) {
-						u64 new_r, new_od;
-
-						i++;
-						new_r = UNPACK_R(factors[i]);
-						new_od = UNPACK_OD(factors[i]);
-						if (r * od == new_r * new_od) {
-							r = new_r;
-							od = new_od;
-							swapped = false;
-							continue;
-						}
-						i--;
-					}
-					break;
+			/*
+			 * Try looking ahead to see if there are additional
+			 * factors for the same product.
+			 */
+			if (i + 1 < ARRAY_SIZE(factors)) {
+				i++;
+				new_r = UNPACK_R(factors[i]);
+				new_od = UNPACK_OD(factors[i]);
+				if (r * od == new_r * new_od) {
+					r = new_r;
+					od = new_od;
+					swapped = false;
+					goto again;
 				}
+				i--;
 			}
 
-			error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio, r * od);
-			/* The lower 16 bits are spurious */
-			error = abs((error - BIT(32))) >> 16;
+			/* We ran out of things to try */
+			continue;
+		}
 
-			if (error < best_error) {
-				best->r = r;
-				best->f = f;
-				best->od = od;
-				best_error = error;
-			}
-			break;
+		error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio, r * od);
+		/* The lower 16 bits are spurious */
+		error = abs((error - BIT(32))) >> 16;
+
+		if (error < best_error) {
+			best->r = r;
+			best->f = f;
+			best->od = od;
+			best_error = error;
 		}
 	} while (f < 64 && i + 1 < ARRAY_SIZE(factors) && error != 0);
 
+	log_debug("best error %lld\n", best_error);
 	if (best_error == S64_MAX)
 		return -EINVAL;
 
-	log_debug("best error %lld\n", best_error);
 	return 0;
 }
 
-- 
2.33.0


  reply	other threads:[~2021-09-11 17:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11 17:20 [PATCH v2 1/4] clk: k210: Fix checking if ulongs are less than 0 Sean Anderson
2021-09-11 17:20 ` Sean Anderson [this message]
2021-09-14  8:45   ` [PATCH v2 2/4] k210: clk: Refactor out_of_spec tests Leo Liang
2021-09-11 17:20 ` [PATCH v2 3/4] test: dm: k210: Reduce duplication in test cases Sean Anderson
2021-09-14  8:48   ` Leo Liang
2021-09-11 17:20 ` [PATCH v2 4/4] clk: k210: Try harder to get the best config Sean Anderson
2021-09-30  4:08   ` Simon Glass
2021-09-14  8:39 ` [PATCH v2 1/4] clk: k210: Fix checking if ulongs are less than 0 Leo Liang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210911172003.899301-2-seanga2@gmail.com \
    --to=seanga2@gmail.com \
    --cc=Damien.LeMoal@wdc.com \
    --cc=bmeng.cn@gmail.com \
    --cc=lukma@denx.de \
    --cc=u-boot@lists.denx.de \
    --cc=ycliang@andestech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).