From patchwork Fri May 11 22:50:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Walle X-Patchwork-Id: 158623 X-Patchwork-Delegate: prafulla@marvell.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 9D325B700B for ; Sat, 12 May 2012 08:51:32 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 426042812E; Sat, 12 May 2012 00:51:26 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4lM2AsuLvRa0; Sat, 12 May 2012 00:51:26 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0C4D828140; Sat, 12 May 2012 00:51:16 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 890CC28132 for ; Sat, 12 May 2012 00:51:12 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XUzr5I3hOwHO for ; Sat, 12 May 2012 00:51:05 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.serverraum.org (mail.serverraum.org [78.47.150.89]) by theia.denx.de (Postfix) with ESMTP id 412012812E for ; Sat, 12 May 2012 00:51:02 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.serverraum.org (Postfix) with ESMTP id B81163EFFB; Sat, 12 May 2012 00:51:02 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.serverraum.org Received: from mail.serverraum.org ([127.0.0.1]) by localhost (web.serverraum.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sxuVCPlBvmlf; Sat, 12 May 2012 00:51:02 +0200 (CEST) Received: from thanatos.fritz.box (95-89-251-205-dynip.superkabel.de [95.89.251.205]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.serverraum.org (Postfix) with ESMTPSA id 620943EFF8; Sat, 12 May 2012 00:51:02 +0200 (CEST) From: Michael Walle To: u-boot@lists.denx.de Date: Sat, 12 May 2012 00:50:45 +0200 Message-Id: <1336776649-9316-2-git-send-email-michael@walle.cc> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1336776649-9316-1-git-send-email-michael@walle.cc> References: <1336776649-9316-1-git-send-email-michael@walle.cc> Subject: [U-Boot] [PATCH v5 1/5] lib: add rand() function X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de It's a PRNG using the simple and fast xorshift method. Signed-off-by: Michael Walle Cc: Wolfgang Denk --- include/common.h | 6 ++++++ lib/Makefile | 1 + lib/rand.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 0 deletions(-) create mode 100644 lib/rand.c diff --git a/include/common.h b/include/common.h index 4b5841e..a9cb62c 100644 --- a/include/common.h +++ b/include/common.h @@ -733,6 +733,12 @@ char * strmhz(char *buf, unsigned long hz); /* lib/crc32.c */ #include +/* lib/rand.c */ +#ifdef CONFIG_RANDOM_MACADDR +void srand(unsigned int seed); +unsigned int rand(void); +#endif + /* common/console.c */ int console_init_f(void); /* Before relocation; uses the serial stuff */ int console_init_r(void); /* After relocation; uses the console stuff */ diff --git a/lib/Makefile b/lib/Makefile index a0fec60..74579f9 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -65,6 +65,7 @@ COBJS-y += string.o COBJS-y += time.o COBJS-$(CONFIG_BOOTP_PXE) += uuid.o COBJS-y += vsprintf.o +COBJS-$(CONFIG_RANDOM_MACADDR) += rand.o COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/lib/rand.c b/lib/rand.c new file mode 100644 index 0000000..6cb76ac --- /dev/null +++ b/lib/rand.c @@ -0,0 +1,43 @@ +/* + * Simple xorshift PRNG + * see http://www.jstatsoft.org/v08/i14/paper + * + * Copyright (c) 2012 Michael Walle + * Michael Walle + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include + +static unsigned int y = 1U; + +void srand(unsigned int seed) +{ + y = seed; +} + +unsigned int rand(void) +{ + y ^= (y << 13); + y ^= (y >> 17); + y ^= (y << 5); + + return y; +}