include contiki.h and use standard data types.
This commit is contained in:
parent
aaae9f4cf8
commit
e6630a6ccb
2 changed files with 13 additions and 12 deletions
|
@ -28,7 +28,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* @(#)$Id: rand.c,v 1.8 2007/08/10 08:01:17 oliverschmidt Exp $
|
* @(#)$Id: rand.c,v 1.9 2007/09/04 08:48:54 bg- Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -37,6 +37,7 @@
|
||||||
#undef RAND_MAX /* Broken header files! */
|
#undef RAND_MAX /* Broken header files! */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
#include "lib/assert.h"
|
#include "lib/assert.h"
|
||||||
|
|
||||||
#ifdef RAND_MAX
|
#ifdef RAND_MAX
|
||||||
|
@ -61,21 +62,21 @@ CTASSERT(RAND_MAX == 0x7fff);
|
||||||
* When possible, keep rand_state across reboots.
|
* When possible, keep rand_state across reboots.
|
||||||
*/
|
*/
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
s32_t rand_state __attribute__((section(".noinit")));
|
int32_t rand_state __attribute__((section(".noinit")));
|
||||||
#else
|
#else
|
||||||
s32_t rand_state = 1;
|
int32_t rand_state = 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int
|
int
|
||||||
rand(void)
|
rand(void)
|
||||||
{
|
{
|
||||||
u32_t hi, lo;
|
uint32_t hi, lo;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Perform two 16x16 bits multiplication with 32 bit results.
|
* Perform two 16x16 bits multiplication with 32 bit results.
|
||||||
*/
|
*/
|
||||||
lo = 16807ul * (u16_t)(rand_state);
|
lo = 16807ul * (uint16_t)(rand_state);
|
||||||
hi = 16807ul * (u16_t)(rand_state >> 16);
|
hi = 16807ul * (uint16_t)(rand_state >> 16);
|
||||||
|
|
||||||
lo += (hi & 0x7fff) << 16;
|
lo += (hi & 0x7fff) << 16;
|
||||||
|
|
||||||
|
@ -83,14 +84,14 @@ rand(void)
|
||||||
* lo += hi >> 15; But faster using 16 bit registers.
|
* lo += hi >> 15; But faster using 16 bit registers.
|
||||||
*/
|
*/
|
||||||
hi <<= 1;
|
hi <<= 1;
|
||||||
lo += (u16_t)(hi >> 16);
|
lo += (uint16_t)(hi >> 16);
|
||||||
|
|
||||||
if ((s32_t)lo <= 0) /* Deal with rand_state == 0. */
|
if ((int32_t)lo <= 0) /* Deal with rand_state == 0. */
|
||||||
lo -= 0x7fffffff;
|
lo -= 0x7fffffff;
|
||||||
|
|
||||||
rand_state = lo;
|
rand_state = lo;
|
||||||
|
|
||||||
if (sizeof(unsigned int) == sizeof(u16_t))
|
if (sizeof(int) == sizeof(int16_t))
|
||||||
return (lo ^ (lo >> 16)) & RAND_MAX; /* Not ANSI C! */
|
return (lo ^ (lo >> 16)) & RAND_MAX; /* Not ANSI C! */
|
||||||
else
|
else
|
||||||
return lo & RAND_MAX;
|
return lo & RAND_MAX;
|
||||||
|
|
|
@ -28,10 +28,10 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* @(#)$Id: rand.h,v 1.3 2007/08/10 08:02:01 oliverschmidt Exp $
|
* @(#)$Id: rand.h,v 1.4 2007/09/04 08:48:54 bg- Exp $
|
||||||
*/
|
*/
|
||||||
/* -*- C -*- */
|
/* -*- C -*- */
|
||||||
/* @(#)$Id: rand.h,v 1.3 2007/08/10 08:02:01 oliverschmidt Exp $ */
|
/* @(#)$Id: rand.h,v 1.4 2007/09/04 08:48:54 bg- Exp $ */
|
||||||
|
|
||||||
#ifndef RAND_H
|
#ifndef RAND_H
|
||||||
#define RAND_H
|
#define RAND_H
|
||||||
|
@ -50,7 +50,7 @@
|
||||||
* Comm. of the ACM, V. 31. No. 10, pp 1192-1201
|
* Comm. of the ACM, V. 31. No. 10, pp 1192-1201
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern s32_t rand_state;
|
extern int32_t rand_state;
|
||||||
|
|
||||||
int rand(void);
|
int rand(void);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue