File: cvtsh_ss_bugfix.h

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (75 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download
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
#pragma once

// Apple clang was fixed in 8.1
#if defined(__apple_build_version__) && \
    ((__clang_major__ < 8) ||           \
     ((__clang_major__ == 8) && (__clang_minor__ < 1)))
#define CAFFE2_INTERNAL_APPLE_NEED_FIX 1
#endif

// Regular clang was fixed in 3.9
#if defined(__clang__) && (__clang_major__ < 4) && (__clang_minor__ < 9)
#define CAFFE2_INTERNAL_CLANG_NEED_FIX 1
#endif

#if defined(CAFFE2_INTERNAL_APPLE_NEED_FIX) || \
    defined(CAFFE2_INTERNAL_CLANG_NEED_FIX)

#include <c10/util/Half.h>
#include <emmintrin.h>

// This version of clang has a bug that _cvtsh_ss is not defined, see
// https://reviews.llvm.org/D16177
static __inline float
    __attribute__((__always_inline__, __nodebug__, __target__("f16c")))
    _cvtsh_ss(unsigned short a) {
  __v8hi v = {(short)a, 0, 0, 0, 0, 0, 0, 0};
  __v4sf r = __builtin_ia32_vcvtph2ps(v);
  return r[0];
}

static __inline unsigned short
    __attribute__((__always_inline__, __nodebug__, __target__("f16c")))
    _cvtss_sh(float a, int imm8) {
  unsigned short ret;
  *reinterpret_cast<at::Half*>(&ret) = a;
  return ret;
}

#endif // __APPLE_NEED_FIX || __CLANG_NEED_FIX

#undef __APPLE_NEED_FIX
#undef __CLANG_NEED_FIX

#if defined(_MSC_VER) && !defined(__clang__)

#include <c10/util/Half.h>
#include <cstdint>

// It seems that microsoft msvc does not have a _cvtsh_ss implementation so
// we will add a dummy version to it.

static inline float _cvtsh_ss(unsigned short x) {
  union {
    std::uint32_t intval;
    float floatval;
  } t1;
  std::uint32_t t2, t3;
  t1.intval = x & 0x7fff; // Non-sign bits
  t2 = x & 0x8000; // Sign bit
  t3 = x & 0x7c00; // Exponent
  t1.intval <<= 13; // Align mantissa on MSB
  t2 <<= 16; // Shift sign bit into position
  t1.intval += 0x38000000; // Adjust bias
  t1.intval = (t3 == 0 ? 0 : t1.intval); // Denormals-as-zero
  t1.intval |= t2; // Re-insert sign bit
  return t1.floatval;
}

static inline unsigned short _cvtss_sh(float x, int imm8) {
  unsigned short ret;
  *reinterpret_cast<at::Half*>(&ret) = x;
  return ret;
}

#endif // _MSC_VER