File: random.h

package info (click to toggle)
pd-cyclone 0.9.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,860 kB
  • sloc: ansic: 38,656; makefile: 318; tcl: 81
file content (86 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// random number generator from supercollider
// coded by matt barber

#include <stdint.h>
#include <time.h>

typedef struct _cyrandom_state{
    uint32_t s1;
    uint32_t s2;
    uint32_t s3;
}t_cyrandom_state;

int cyrandom_get_id(void);
void cyrandom_init(t_cyrandom_state* rstate, int seed);
int cyget_seed(t_symbol *s, int ac, t_atom *av, int n);
uint32_t cyrandom_trand(uint32_t* s1, uint32_t* s2, uint32_t* s3);
float cyrandom_frand(uint32_t* s1, uint32_t* s2, uint32_t* s3);

// These are for [pink~]

#if defined(__GNUC__)

// use gcc's builtins
static __inline__ int32_t CLZ(int32_t arg){
    if(arg)
        return(__builtin_clz(arg));
    else
        return(32);
}

#elif defined(_MSC_VER)

#include <intrin.h>
#pragma intrinsic(_BitScanReverse)

__forceinline static int32_t CLZ(int32_t arg){
    unsigned long idx;
    if(_BitScanReverse(&idx, (unsigned long)arg))
        return((int32_t)(31-idx));
    return(32);
}

#elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)

static __inline__ int32_t CLZ(int32_t arg){
    __asm__ volatile("cntlzw %0, %1" : "=r" (arg) : "r" (arg));
    return(arg);
}

#elif defined(__i386__) || defined(__x86_64__)
static __inline__ int32_t CLZ(int32_t arg){
    if(arg)
        __asm__ volatile("bsrl %0, %0\nxorl $31, %0\n" : "=r" (arg) : "0" (arg));
    else
        arg = 32;
    return(arg);
}

#else

static __inline__ int32_t CLZ(int32_t arg){
    if(!arg)
        return(32);
    int32_t n = 0;
    if(!(arg & 0xFFFF0000)){
        n += 16;
        arg <<= 16;
    }
    if(!(arg & 0xFF000000)){
        n += 8;
        arg <<= 8;
    }
    if(!(arg & 0xF0000000)){
        n += 4;
        arg <<= 4;
    }
    if(!(arg & 0xC0000000)){
        n += 2;
        arg <<= 2;
    }
    if(!(arg & 0x80000000))
        n++;
    return(n);
}

#endif