genericopenlibs/liboil/tsrc/examples/oil-random/src/oil-random.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <liboil/liboil.h>
       
    22 #include <liboil/liboildebug.h>
       
    23 #include <time.h>
       
    24 #include <stdio.h>
       
    25 #include <string.h>
       
    26 
       
    27 #include <liboil/globals.h>
       
    28 
       
    29 #define LOG_FILE "c:\\logs\\examples_oil-random_log.txt"
       
    30 #include "std_log_result.h"
       
    31 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    32 
       
    33 void create_xml(int result)
       
    34 {
       
    35     if(result)
       
    36         assert_failed = 1;
       
    37     
       
    38     testResultXml("examples_oil-random");
       
    39     close_log_file();
       
    40 }
       
    41 typedef struct _OilRandomState OilRandomState;
       
    42 
       
    43 struct _OilRandomState {
       
    44   int index;
       
    45   uint32_t mt[624];
       
    46   uint32_t bits[624];
       
    47 };
       
    48 
       
    49 OilRandomState state;
       
    50 
       
    51 static void _oil_random_init (void);
       
    52 uint32_t oil_random_get_int (OilRandomState *state);
       
    53 void oil_random_get_bits (OilRandomState *state, uint8_t *dest, int n);
       
    54 
       
    55 int main (int argc, char *argv[])
       
    56 {
       
    57   int i;
       
    58   uint32_t a[10];
       
    59 
       
    60   oil_init();
       
    61 
       
    62   _oil_random_init();
       
    63 
       
    64   for(i=0;i<10;i++){
       
    65   std_log(LOG_FILENAME_LINE,"%d\n", oil_random_get_int(&state));
       
    66   }
       
    67 
       
    68   oil_random_get_bits (&state, (void *)a, 10*4);
       
    69   for(i=0;i<10;i++){
       
    70   std_log(LOG_FILENAME_LINE,"%d\n", a[i]);
       
    71   }
       
    72     if(assert_failed)
       
    73           std_log(LOG_FILENAME_LINE,"Test Fail");
       
    74     else
       
    75           std_log(LOG_FILENAME_LINE,"Test Successful");
       
    76     create_xml(0);
       
    77 
       
    78 }
       
    79 
       
    80 
       
    81 void
       
    82 oil_random_state_seed (OilRandomState *state, uint32_t seed)
       
    83 {
       
    84   uint32_t *mt = state->mt;
       
    85   int i;
       
    86 
       
    87   mt[0] = seed;
       
    88   for(i=1;i<624;i++) {
       
    89     mt[i] = (1812433253UL * (mt[i-1] ^ (mt[i-1] >> 30)) + i);
       
    90   }
       
    91   oil_mt19937 (state->bits, state->mt);
       
    92   state->index = 0;
       
    93 }
       
    94 
       
    95 
       
    96 
       
    97 static void
       
    98 _oil_random_init (void)
       
    99 {
       
   100   int seed;
       
   101 
       
   102   seed = time(NULL);
       
   103   OIL_ERROR("seed is %d", seed);
       
   104 
       
   105   oil_random_state_seed (&state, seed);
       
   106 }
       
   107 
       
   108 uint32_t
       
   109 oil_random_get_int (OilRandomState *state)
       
   110 {
       
   111   if (state->index >= 624) {
       
   112     oil_mt19937 (state->bits, state->mt);
       
   113     state->index = 0;
       
   114   }
       
   115   return state->bits[state->index++];
       
   116 }
       
   117 
       
   118 void
       
   119 oil_random_get_bits (OilRandomState *state, uint8_t *dest, int n)
       
   120 {
       
   121   int i = state->index * 4;
       
   122   int m;
       
   123 
       
   124   while(n > 0) {
       
   125     if (i >= 624*4) {
       
   126       oil_mt19937 (state->bits, state->mt);
       
   127       i = 0;
       
   128     }
       
   129 
       
   130     m = n;
       
   131     if (m > 624*4 - i) m = 624*4 - i;
       
   132 
       
   133     memcpy (dest, ((uint8_t *)state->bits) + i, m);
       
   134     i += m;
       
   135     n -= m;
       
   136   }
       
   137   state->index = (i+3)/4;
       
   138 }
       
   139 
       
   140