Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33935C4338F for ; Tue, 27 Jul 2021 03:52:13 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7E5E860F9E for ; Tue, 27 Jul 2021 03:52:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7E5E860F9E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2741983358; Tue, 27 Jul 2021 05:52:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="DvuyGmp/"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DBDA883417; Tue, 27 Jul 2021 05:51:59 +0200 (CEST) Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9A73B8334E for ; Tue, 27 Jul 2021 05:51:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qk1-x732.google.com with SMTP id b20so11137861qkj.3 for ; Mon, 26 Jul 2021 20:51:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uO0RYPNlCEXdVYSfiBQJr4Hf2D59ePxQEWV6RiRM58I=; b=DvuyGmp/R4F/+3E7Ol9xSN7syHoDRyFNAVj2/jUtDqzDx2bCpJKDOP7D2DfM5+MFi+ 5MJPlh/Ghq2uN9LWb1AFeYZYzhkDE3LvKrACdJrvMHkVxTJomXOCX8dRPsxkzDGpCdgY vsKiE59SN9M+Gfwy4zR8j+5U1v3nsgSWDI22qD2QLZpzA7sIOfl+ZW4Q9mKcAYftUpOT 1Tcop3qBxgJ24LX6o30oeW/PLLS/QnK1hzittWxxHg5+NxleDfdkVYHbIbIAaRG34uID XVR+vQOdwKoYv6tm3ykZF29eR2VkgSzX7QoVPNnagGC9t1tzv9iIPgCqu/m8bNry1NG0 pCvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uO0RYPNlCEXdVYSfiBQJr4Hf2D59ePxQEWV6RiRM58I=; b=Nqmq3HRR2WdBoKkIiRYb9uPAxpR8uGIvJ09JJM+ht/x6dJtpHWUNeL4L5sKcDGb2Yu uWYmRdZIAqAO47I4QyMgVfwZXonc42I3vvHAMmHxMByc1Nn+V2AGOZivoHWOGIREKfRP 3ELmjK+A9fW6M3A82FUNYlFIImFRhtZ5F15xHiatSvG4Yzhtif3W+aRMIDp3rR4Z3btF i9O0nZPGU54k1P9cuxJN0mo508zkP2TGxh6znPWOR1GpvIiS6MFDJnBoh6aGpq4np0oZ wg3SMge1wEBOyGdupuCPhRr9UaD6VoQcqIqL3vYJXX22Q9vP48ZO/1rsTF/6Mzf/OsVo /jxg== X-Gm-Message-State: AOAM533KPxh59WsURkLKghVZ++d9g/mrze8fJdH09EAp+2rP2NesNNeA sm9lLe4RERoqaeQz1iyvJfKdTtzGrd0= X-Google-Smtp-Source: ABdhPJxO4/siBKEr00+oiWe0Iev4Gp+DV0ScFGdW1FYtkMk2yOiy7qlu+TF66NypHzXGkgOBGZ8bDg== X-Received: by 2002:ae9:e109:: with SMTP id g9mr20811925qkm.95.1627357909219; Mon, 26 Jul 2021 20:51:49 -0700 (PDT) Received: from godwin.fios-router.home (pool-74-96-87-9.washdc.fios.verizon.net. [74.96.87.9]) by smtp.gmail.com with ESMTPSA id y67sm1096211qkd.58.2021.07.26.20.51.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 20:51:48 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Leo Liang Cc: Lukasz Majewski , Damien Le Moal , Sean Anderson Subject: [PATCH 2/4] k210: clk: Refactor out_of_spec tests Date: Mon, 26 Jul 2021 23:51:42 -0400 Message-Id: <20210727035144.298277-2-seanga2@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210727035144.298277-1-seanga2@gmail.com> References: <20210727035144.298277-1-seanga2@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean 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 --- 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 37bd624eca..d2cee2cf97 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.32.0