os/ossrv/genericopenlibs/liboil/src/liboilrandom.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/liboilrandom.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,262 @@
     1.4 +/*
     1.5 + * LIBOIL - Library of Optimized Inner Loops
     1.6 + * Copyright (c) 2005 David A. Schleef <ds@schleef.org>
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#include <liboil/liboilrandom.h>
    1.36 +#include "liboil/liboilcolorspace.h"
    1.37 +#include <stdlib.h>
    1.38 +
    1.39 +
    1.40 +/**
    1.41 + * SECTION:liboilrandom
    1.42 + * @title: Random Number Generation
    1.43 + * @short_description: Random number generation
    1.44 + */
    1.45 +
    1.46 +static void
    1.47 +_oil_random_bits (void *dest, int n)
    1.48 +{
    1.49 +  int i;
    1.50 +  uint8_t *d = dest;
    1.51 +  for(i=0;i<n;i++){
    1.52 +    d[i] = (rand()>>16);
    1.53 +  }
    1.54 +}
    1.55 +
    1.56 +/**
    1.57 + * oil_random_s32:
    1.58 + * @dest:
    1.59 + * @n:
    1.60 + *
    1.61 + * Writes random values in the range [-(1<<31), (1<<31)-1] to the
    1.62 + * destination array.
    1.63 + */
    1.64 +#ifdef __SYMBIAN32__
    1.65 +EXPORT_C
    1.66 +#endif
    1.67 +void
    1.68 +oil_random_s32(oil_type_s32 *dest, int n)
    1.69 +{
    1.70 +  _oil_random_bits (dest, n*4);
    1.71 +}
    1.72 +
    1.73 +/**
    1.74 + * oil_random_s64:
    1.75 + *
    1.76 + * Writes random values in the range [-(1<<63), (1<<63)-1] to the
    1.77 + * destination array.
    1.78 + */
    1.79 +#ifdef __SYMBIAN32__
    1.80 +EXPORT_C
    1.81 +#endif
    1.82 +void
    1.83 +oil_random_s64 (oil_type_s64 *dest, int n)
    1.84 +{
    1.85 +  _oil_random_bits (dest, n*8);
    1.86 +}
    1.87 +
    1.88 +/**
    1.89 + * oil_random_s16:
    1.90 + *
    1.91 + * Writes random values in the range [-(1<<15), (1<<15)-1] to the
    1.92 + * destination array.
    1.93 + */
    1.94 +#ifdef __SYMBIAN32__
    1.95 +EXPORT_C
    1.96 +#endif
    1.97 +void
    1.98 +oil_random_s16 (oil_type_s16 *dest, int n)
    1.99 +{
   1.100 +  _oil_random_bits (dest, n*2);
   1.101 +}
   1.102 +
   1.103 +/**
   1.104 + * oil_random_s8:
   1.105 + *
   1.106 + * Writes random values in the range [-(1<<7), (1<<7)-1] to the
   1.107 + * destination array.
   1.108 + */
   1.109 +#ifdef __SYMBIAN32__
   1.110 +EXPORT_C
   1.111 +#endif
   1.112 +void
   1.113 +oil_random_s8 (oil_type_s8 *dest, int n)
   1.114 +{
   1.115 +  _oil_random_bits (dest, n);
   1.116 +}
   1.117 +
   1.118 +/**
   1.119 + * oil_random_u32:
   1.120 + *
   1.121 + * Writes random values in the range [0, (1<<32)-1] to the
   1.122 + * destination array.
   1.123 + */
   1.124 +#ifdef __SYMBIAN32__
   1.125 +EXPORT_C
   1.126 +#endif
   1.127 +void
   1.128 +oil_random_u32 (oil_type_u32 *dest, int n)
   1.129 +{
   1.130 +  _oil_random_bits (dest, n*4);
   1.131 +}
   1.132 +
   1.133 +/**
   1.134 + * oil_random_u64:
   1.135 + *
   1.136 + * Writes random values in the range [0, (1<<64)-1] to the
   1.137 + * destination array.
   1.138 + */
   1.139 +#ifdef __SYMBIAN32__
   1.140 +EXPORT_C
   1.141 +#endif
   1.142 +void
   1.143 +oil_random_u64 (oil_type_u64 *dest, int n)
   1.144 +{
   1.145 +  _oil_random_bits (dest, n*8);
   1.146 +}
   1.147 +
   1.148 +/**
   1.149 + * oil_random_u16:
   1.150 + *
   1.151 + * Writes random values in the range [0, (1<<16)-1] to the
   1.152 + * destination array.
   1.153 + */
   1.154 +#ifdef __SYMBIAN32__
   1.155 +EXPORT_C
   1.156 +#endif
   1.157 +void
   1.158 +oil_random_u16 (oil_type_u16 *dest, int n)
   1.159 +{
   1.160 +  _oil_random_bits (dest, n*2);
   1.161 +}
   1.162 +
   1.163 +/**
   1.164 + * oil_random_u8:
   1.165 + *
   1.166 + * Writes random values in the range [0, (1<<8)-1] to the
   1.167 + * destination array.
   1.168 + */
   1.169 +#ifdef __SYMBIAN32__
   1.170 +EXPORT_C
   1.171 +#endif
   1.172 +void
   1.173 +oil_random_u8 (oil_type_u8 *dest, int n)
   1.174 +{
   1.175 +  _oil_random_bits (dest, n);
   1.176 +}
   1.177 +
   1.178 +/**
   1.179 + * oil_random_f64:
   1.180 + *
   1.181 + * Writes random double-precision floating point values in the
   1.182 + * range [0, 1.0) to the destination array.
   1.183 + */
   1.184 +#ifdef __SYMBIAN32__
   1.185 +EXPORT_C
   1.186 +#endif
   1.187 +void
   1.188 +oil_random_f64 (oil_type_f64 *dest, int n)
   1.189 +{
   1.190 +  int i;
   1.191 +  for(i=0;i<n;i++){
   1.192 +    dest[i] = (((rand()/(RAND_MAX+1.0))+rand())/(RAND_MAX+1.0));
   1.193 +  }
   1.194 +}
   1.195 +
   1.196 +/**
   1.197 + * oil_random_f32:
   1.198 + *
   1.199 + * Writes random single-precision floating point values in the
   1.200 + * range [0, 1.0) to the destination array.
   1.201 + */
   1.202 +#ifdef __SYMBIAN32__
   1.203 +EXPORT_C
   1.204 +#endif
   1.205 +void
   1.206 +oil_random_f32 (oil_type_f32 *dest, int n)
   1.207 +{
   1.208 +  int i;
   1.209 +  for(i=0;i<n;i++){
   1.210 +    dest[i] = (rand()/(RAND_MAX+1.0));
   1.211 +  }
   1.212 +}
   1.213 +
   1.214 +/**
   1.215 + * oil_random_alpha:
   1.216 + *
   1.217 + * Writes random values in the range [0, 255] to the destination
   1.218 + * array suitable for alpha values.  This is similar to oil_random_u8(),
   1.219 + * except the values 0 and 255 are strongly favored.
   1.220 + */
   1.221 +#ifdef __SYMBIAN32__
   1.222 +EXPORT_C
   1.223 +#endif
   1.224 +void
   1.225 +oil_random_alpha(uint8_t *dest, int n)
   1.226 +{
   1.227 +  int i;
   1.228 +  int x;
   1.229 +  for(i=0;i<n;i++){
   1.230 +    x = ((rand()>>8) & 0x1ff) - 0x80;
   1.231 +    if (x<0) x = 0;
   1.232 +    if (x>255) x = 255;
   1.233 +    dest[i] = x;
   1.234 +  }
   1.235 +}
   1.236 +
   1.237 +/**
   1.238 + * oil_random_argb:
   1.239 + * @dest: destination array.
   1.240 + * @n: number of values to write.
   1.241 + *
   1.242 + * Creates valid random RGBA values and places them in the destination
   1.243 + * array.
   1.244 + */
   1.245 +#ifdef __SYMBIAN32__
   1.246 +EXPORT_C
   1.247 +#endif
   1.248 +void
   1.249 +oil_random_argb(uint32_t *dest, int n)
   1.250 +{
   1.251 +  int i;
   1.252 +  int x;
   1.253 +  for(i=0;i<n;i++){
   1.254 +    x = ((rand()>>8) & 0x1ff) - 0x80;
   1.255 +    if (x<0) x = 0;
   1.256 +    if (x>255) x = 255;
   1.257 +    dest[i] = oil_argb_noclamp(x,
   1.258 +        oil_muldiv_255(x,oil_rand_u8()),
   1.259 +        oil_muldiv_255(x,oil_rand_u8()),
   1.260 +        oil_muldiv_255(x,oil_rand_u8()));
   1.261 +  }
   1.262 +
   1.263 +}
   1.264 +
   1.265 +