"next_docs/en/user_guide/data.rst" did not exist on "73afb7d6e17d5c82c4d17e20ecab970a59e3229e"
numba.h 1.39 KB
Newer Older
chenxl's avatar
chenxl committed
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
// Adapted from
// https://github.com/Mozilla-Ocho/llamafile/blob/0.8.8/llamafile/numba.h
// Copyrigth 2024 Mozilla Foundation.
// Copyright(c) 2024 by KVCache.AI, All Rights Reserved.

#pragma once

inline int rand32(void) {
    static unsigned long long lcg = 1;
    lcg *= 6364136223846793005;
    lcg += 1442695040888963407;
    return lcg >> 32;
}

inline int popcount(unsigned x) {
    x = x - ((x >> 1) & 0x55555555);
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
    x = (x + (x >> 4)) & 0x0F0F0F0F;
    x = (x + (x >> 16));
    return (x + (x >> 8)) & 0x0000003F;
}

inline int hamming(int x, int y) {
    return popcount(x ^ y);
}

inline float float01(unsigned x) {  // (0,1)
    return 1.f / 8388608 * ((x >> 9) + .5f);
}

inline float numba(void) {  // (-10,10)
    return float01(rand32()) * 2.f - 1.f;
}

template <typename T>
void randomize(T* A, int n) {
    for (int i = 0; i < n; ++i)
        A[i] = numba();
}

template <typename T>
void randomize(int m, int n, T* A, int lda) {
    for (int j = 0; j < n; ++j)
        for (int i = 0; i < m; ++i)
            A[lda * j + i] = numba();
}

template <typename T, typename U>
void broadcast(T* A, int n, U x) {
    for (int i = 0; i < n; ++i)
        A[i] = x;
}

template <typename T, typename U>
void broadcast(int m, int n, T* A, int lda, U x) {
    for (int j = 0; j < n; ++j)
        for (int i = 0; i < m; ++i)
            A[lda * j + i] = x;
}