File: x86-fp-write.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (69 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (23)
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
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <cstdio>

struct alignas(16) float80_raw {
  uint8_t data[10];
};

int main() {
  float80_raw st[8];
  uint16_t env[14];
  union alignas(16) {
    uint16_t i16[256];
    uint32_t i32[128];
    uint64_t i64[64];
  } fxsave;

  asm volatile(
    "finit\n\t"
    "int3\n\t"
#if defined(__x86_64__)
    "fxsave64 %2\n\t"
#else
    "fxsave %2\n\t"
#endif
    "fnstenv %1\n\t"
    "fnclex\n\t"
    "fstpt 0x00(%0)\n\t"
    "fstpt 0x10(%0)\n\t"
    "fstpt 0x20(%0)\n\t"
    "fstpt 0x30(%0)\n\t"
    "fstpt 0x40(%0)\n\t"
    "fstpt 0x50(%0)\n\t"
    "fstpt 0x60(%0)\n\t"
    "fstpt 0x70(%0)\n\t"
    :
    : "a"(st), "m"(env), "m"(fxsave)
    : "st"
  );

  assert(env[0] == fxsave.i16[0]);
  assert(env[2] == fxsave.i16[1]);

  printf("fctrl = 0x%04" PRIx16 "\n", env[0]);
  printf("fstat = 0x%04" PRIx16 "\n", env[2]);
  printf("ftag = 0x%04" PRIx16 "\n", env[4]);
  printf("fop = 0x%04" PRIx16 "\n", fxsave.i16[3]);
#if defined(__x86_64__)
  printf("fip = 0x%016" PRIx64 "\n", fxsave.i64[1]);
  printf("fdp = 0x%016" PRIx64 "\n", fxsave.i64[2]);
#else
  printf("fip = 0x%08" PRIx32 "\n", fxsave.i32[2]);
  printf("fcs = 0x%04" PRIx16 "\n", fxsave.i16[6]);
  printf("fdp = 0x%08" PRIx32 "\n", fxsave.i32[4]);
  printf("fds = 0x%04" PRIx16 "\n", fxsave.i16[10]);
#endif
  printf("mxcsr = 0x%08" PRIx32 "\n", fxsave.i32[6]);
  printf("mxcsr_mask = 0x%08" PRIx32 "\n", fxsave.i32[7]);

  for (int i = 0; i < 8; ++i) {
    printf("st%d = { ", i);
    for (int j = 0; j < sizeof(st->data); ++j)
      printf("0x%02" PRIx8 " ", st[i].data[j]);
    printf("}\n");
  }

  return 0;
}