v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
fpu.cc
Go to the documentation of this file.
1// Copyright 2025 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/base/fpu.h"
6
7#include <float.h>
8
9#include "include/v8config.h"
10
11namespace v8::base {
12
13#if defined(V8_HOST_ARCH_X64) || defined(V8_HOST_ARCH_IA32)
14#if defined(V8_CC_GNU)
15namespace {
16// Two bits on Intel CPUs, for FTZ (flush denormalized results to zero) and DAZ
17// (flush denormalized inputs to zero).
18constexpr int kFlushDenormToZeroBits = 0x8040;
19int GetCSR() {
20 int result;
21 asm volatile("stmxcsr %0" : "=m"(result));
22 return result;
23}
24
25void SetCSR(int a) {
26 int temp = a;
27 asm volatile("ldmxcsr %0" : : "m"(temp));
28}
29} // namespace
30
32 int csr = GetCSR();
33 return csr & kFlushDenormToZeroBits;
34}
35
36void FPU::SetFlushDenormals(bool value) {
37 int old_csr = GetCSR();
38 int new_csr = value ? old_csr | kFlushDenormToZeroBits
39 : old_csr & ~kFlushDenormToZeroBits;
40 SetCSR(new_csr);
41}
42#elif defined(V8_CC_MSVC)
44 unsigned int csr;
45 _controlfp_s(&csr, 0, 0);
46 return (csr & _MCW_DN) == _DN_FLUSH;
47}
48
49void FPU::SetFlushDenormals(bool value) {
50 unsigned int csr;
51 _controlfp_s(&csr, value ? _DN_FLUSH : _DN_SAVE, _MCW_DN);
52}
53#else
54#error "Unsupported compiler"
55#endif
56
57#elif defined(V8_HOST_ARCH_ARM64) || defined(V8_HOST_ARCH_ARM)
58
59namespace {
60// Bit 24 is the flush-to-zero mode control bit. Setting it to 1 flushes
61// denormals to 0.
62constexpr int kFlushDenormToZeroBit = (1 << 24);
63int GetStatusWord() {
64 int result;
65#if defined(V8_HOST_ARCH_ARM64)
66 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
67#else
68 asm volatile("vmrs %[result], FPSCR" : [result] "=r"(result));
69#endif
70 return result;
71}
72
73void SetStatusWord(int a) {
74#if defined(V8_HOST_ARCH_ARM64)
75 asm volatile("msr FPCR, %x[src]" : : [src] "r"(a));
76#else
77 asm volatile("vmsr FPSCR, %[src]" : : [src] "r"(a));
78#endif
79}
80} // namespace
81
83 int csr = GetStatusWord();
84 return csr & kFlushDenormToZeroBit;
85}
86
87void FPU::SetFlushDenormals(bool value) {
88 int old_csr = GetStatusWord();
89 int new_csr = value ? old_csr | kFlushDenormToZeroBit
90 : old_csr & ~kFlushDenormToZeroBit;
91 SetStatusWord(new_csr);
92}
93
94#else
95
96bool FPU::GetFlushDenormals() { return false; }
97void FPU::SetFlushDenormals(bool value) {}
98
99#endif
100
101} // namespace v8::base
static V8_BASE_EXPORT bool GetFlushDenormals()
Definition fpu.cc:96
static V8_BASE_EXPORT void SetFlushDenormals(bool)
Definition fpu.cc:97
std::optional< TNode< JSArray > > a
ZoneVector< RpoNumber > & result