From patchwork Thu Dec 19 07:51:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Yi Lee X-Patchwork-Id: 3375441 Return-Path: X-Original-To: patchwork-linux-acpi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 65D9AC0D4A for ; Thu, 19 Dec 2013 07:56:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 51DDB20649 for ; Thu, 19 Dec 2013 07:56:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7782120647 for ; Thu, 19 Dec 2013 07:56:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753161Ab3LSHyv (ORCPT ); Thu, 19 Dec 2013 02:54:51 -0500 Received: from mail-pb0-f49.google.com ([209.85.160.49]:59192 "EHLO mail-pb0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753159Ab3LSHyt (ORCPT ); Thu, 19 Dec 2013 02:54:49 -0500 Received: by mail-pb0-f49.google.com with SMTP id jt11so802040pbb.36 for ; Wed, 18 Dec 2013 23:54:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1D9oRBg3FD+VC/Y4O/osDz+mmMIX1BJycapMHJfmaPs=; b=goOYwhyBKerPQGtBNFI7upIDC375v0O+9hgcz+YWv0pJH18I4Od/B+Tjtzl2eQMFJB WsvhuS7QV5beo6xIiJhGCfzsimBhhtM2u3pP6DeyZu/ggDAeo4SVtbLv1s1pgFt8xSdf E6oXjxa1pSGQ29RTeQhqwL2XanBMIm2j1MM5JGSn9h/Kw+lMnzY3EqbsElKCiVow4BDV OMVm1QZRrloVTPHRDfejKVUNj1gywV8jpyg3Bu36crJpjYhR0rfPcTg0gkXuQfHJ9T1k 7UUP0Gb3pxO6ATZDmmUX2EUbH0fk7noTVuhNYDVcyE4WWe26h4PyT2gMFgFf2lsnvdR8 qGDQ== X-Received: by 10.66.197.135 with SMTP id iu7mr38686531pac.149.1387439688500; Wed, 18 Dec 2013 23:54:48 -0800 (PST) Received: from localhost.localdomain ([130.57.30.250]) by mx.google.com with ESMTPSA id qp15sm5173154pbb.2.2013.12.18.23.54.40 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 18 Dec 2013 23:54:47 -0800 (PST) From: "Lee, Chun-Yi" To: "Rafael J. Wysocki" , Alessandro Zummo , "H. Peter Anvin" , Matt Fleming , Matthew Garrett Cc: Elliott@hp.com, samer.el-haj-mahmoud@hp.com, Oliver Neukum , werner@suse.com, trenn@suse.de, JBeulich@suse.com, linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com, x86@kernel.org, "linux-efi@vger.kernel.org" , linux-acpi@vger.kernel.org, "Lee, Chun-Yi" Subject: [RFC PATCH 10/14] rtc: improve and move week day computing function to rtc header Date: Thu, 19 Dec 2013 15:51:51 +0800 Message-Id: <1387439515-8926-11-git-send-email-jlee@suse.com> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: <1387439515-8926-1-git-send-email-jlee@suse.com> References: <1387439515-8926-1-git-send-email-jlee@suse.com> Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Due to rtc-acpid and efi time used the same logic for computing week day, so this patch moves code to rtc.h header file. Additionally using a leap year algorithm to replace the for-loop block in compute_wday for improve the performance. The first version of algorithm is from Oliver Neukum. --- drivers/rtc/rtc-acpitad.c | 13 +------------ include/linux/efi.h | 13 +------------ include/linux/rtc.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/drivers/rtc/rtc-acpitad.c b/drivers/rtc/rtc-acpitad.c index 065a033..bdf7ae1 100644 --- a/drivers/rtc/rtc-acpitad.c +++ b/drivers/rtc/rtc-acpitad.c @@ -32,23 +32,12 @@ compute_yday(struct acpi_time *acpit) static int compute_wday(struct acpi_time *acpit) { - int y; - int ndays = 0; - if (acpit->year < 1900) { pr_err("ACPI year < 1900, invalid date\n"); return -1; } - for (y = 1900; y < acpit->year; y++) - ndays += 365 + (is_leap_year(y) ? 1 : 0); - - ndays += compute_yday(acpit); - - /* - * 1=1/1/1900 was a Monday - */ - return (ndays + 1) % 7; + return rtc_wday(acpit->day, acpit->month - 1, acpit->year); } static void diff --git a/include/linux/efi.h b/include/linux/efi.h index 3859f3e..1c78ae7 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -177,23 +177,12 @@ compute_yday(efi_time_t *eft) static inline int compute_wday(efi_time_t *eft) { - int y; - int ndays = 0; - if (eft->year < 1998) { pr_err("EFI year < 1998, invalid date\n"); return -1; } - for (y = EFI_RTC_EPOCH; y < eft->year; y++) - ndays += 365 + (is_leap_year(y) ? 1 : 0); - - ndays += compute_yday(eft); - - /* - * 4=1/1/1998 was a Thursday - */ - return (ndays + 4) % 7; + return rtc_wday(eft->day, eft->month - 1, eft->year); } static inline void diff --git a/include/linux/rtc.h b/include/linux/rtc.h index e6380ec..511884f 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -196,6 +196,44 @@ static inline bool is_leap_year(unsigned int year) return (!(year % 4) && (year % 100)) || !(year % 400); } +#define SINCE1900 25 /* valid from 2000 */ + +static inline int rtc_wday(unsigned int day, unsigned int month, unsigned int year) +{ + int ndays, correction; + int base; + + if (year < 1900) { + pr_err("rtc: year < 1900, invalid date\n"); + return -1; + } + + if (year >= 2000) + base = year - 2000; + else + base = year - 1900; + + correction = 0; + if (base >= 0) { + correction += base / 4; + correction -= base / 100; + correction += base / 400; + if (year >= 2000) + correction += SINCE1900; + + /* later rtc_year_days will add the leap day of current year */ + correction -= ((is_leap_year(year)) ? 1 : 0); + } + + ndays = (year - 1900) * 365 + correction; + ndays += rtc_year_days(day, month, year); + + /* + * 1=1/1/1900 was a Monday + */ + return (ndays + 1) % 7; +} + #ifdef CONFIG_RTC_HCTOSYS_DEVICE extern int rtc_hctosys_ret; #else