Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2019-2023 Broadcom 3 : : * All rights reserved. 4 : : */ 5 : : 6 : : /* Random Number Functions */ 7 : : 8 : : #include <stdio.h> 9 : : #include <stdint.h> 10 : : #include "rand.h" 11 : : 12 : : #define TF_RAND_LFSR_INIT_VALUE 0xACE1u 13 : : 14 : : uint16_t lfsr = TF_RAND_LFSR_INIT_VALUE; 15 : : uint32_t bit; 16 : : 17 : : /** 18 : : * Generates a 16 bit pseudo random number 19 : : * 20 : : * Returns: 21 : : * uint16_t number 22 : : */ 23 : 0 : uint16_t rand16(void) 24 : : { 25 : 0 : bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5)) & 1; 26 : 0 : return lfsr = (lfsr >> 1) | (bit << 15); 27 : : } 28 : : 29 : : /** 30 : : * Generates a 32 bit pseudo random number 31 : : * 32 : : * Returns: 33 : : * uint32_t number 34 : : */ 35 : 0 : uint32_t rand32(void) 36 : : { 37 : 0 : return (rand16() << 16) | rand16(); 38 : : } 39 : : 40 : : /** 41 : : * Resets the seed used by the pseudo random number generator 42 : : */ 43 : 0 : void rand_init(void) 44 : : { 45 : 0 : lfsr = TF_RAND_LFSR_INIT_VALUE; 46 : 0 : bit = 0; 47 : 0 : }