aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jumper/SkJumper_misc.h
blob: 54e957ad6e05c7c483182ca83b5eb5db12082a46 (plain)
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkJumper_misc_DEFINED
#define SkJumper_misc_DEFINED

#include "SkJumper.h"  // for memcpy()

// Miscellany used by SkJumper_stages.cpp and SkJumper_vectors.h.

// Every function in this file should be marked static and inline using SI.
#define SI static inline

template <typename T, typename P>
SI T unaligned_load(const P* p) {  // const void* would work too, but const P* helps ARMv7 codegen.
    T v;
    memcpy(&v, p, sizeof(v));
    return v;
}

template <typename Dst, typename Src>
SI Dst bit_cast(const Src& src) {
    static_assert(sizeof(Dst) == sizeof(Src), "");
    return unaligned_load<Dst>(&src);
}

template <typename Dst, typename Src>
SI Dst widen_cast(const Src& src) {
    static_assert(sizeof(Dst) > sizeof(Src), "");
    Dst dst;
    memcpy(&dst, &src, sizeof(Src));
    return dst;
}

// A couple functions for embedding constants directly into code,
// so that no .const or .literal4 section is created.
SI int C(int x) {
#if defined(JUMPER) && defined(__x86_64__)
    // Move x-the-compile-time-constant as a literal into x-the-register.
    asm("mov %1, %0" : "=r"(x) : "i"(x));
#endif
    return x;
}
SI float C(float f) {
    int x = C(unaligned_load<int>(&f));
    return unaligned_load<float>(&x);
}

// Syntax sugar to make C() easy to use for constant literals.
SI int   operator "" _i(unsigned long long int i) { return C(  (int)i); }
SI float operator "" _f(           long double f) { return C((float)f); }

#endif//SkJumper_misc_DEFINED