217 if (scp ==
nullptr) {
220 auto *ctx =
static_cast<ucontext_t *
>(scp);
221#if defined(__linux__) && defined(__x86_64__)
222 regs.
ip =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_RIP]);
223 regs.
ax =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_RAX]);
224 regs.
dx =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_RDX]);
225#elif defined(__linux__) && defined(__i386__)
226 regs.
ip =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_EIP]);
227 regs.
ax =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_EAX]);
228 regs.
dx =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext.gregs[REG_EDX]);
229#elif defined(__APPLE__) && defined(__x86_64__)
230 if (ctx->uc_mcontext !=
nullptr) {
231 regs.
ip =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__rip);
232 regs.
ax =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__rax);
233 regs.
dx =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__rdx);
235#elif defined(__APPLE__) && defined(__i386__)
236 if (ctx->uc_mcontext !=
nullptr) {
237 regs.
ip =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__eip);
238 regs.
ax =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__eax);
239 regs.
dx =
reinterpret_cast<uintptr_t *
>(&ctx->uc_mcontext->__ss.__edx);
242 static_cast<void>(ctx);
336 static volatile sig_atomic_t reported_div = 0;
337 static volatile sig_atomic_t reported_inv = 0;
338 static volatile sig_atomic_t reported_ovf = 0;
339 static volatile sig_atomic_t reported_unk = 0;
340 static volatile sig_atomic_t reported_int = 0;
342 static constexpr char prefix[] =
"FPE (continuing, masking further): ";
343 static constexpr char msg_div[] =
"division by zero\n";
344 static constexpr char msg_inv[] =
"invalid operation\n";
345 static constexpr char msg_ovf[] =
"overflow\n";
346 static constexpr char msg_unk[] =
"unknown\n";
351 volatile sig_atomic_t *reported = &reported_unk;
352 const char *msg = msg_unk;
353 size_t msg_len =
sizeof(msg_unk) - 1;
354 bool classified =
false;
356#if defined(__APPLE__) && defined(__aarch64__)
358 unsigned trap_mask = 0;
359 if (!apple_arm64_fp_trap(scp, &trap_mask)) {
360 struct sigaction dfl;
361 dfl.sa_handler = SIG_DFL;
362 sigemptyset(&dfl.sa_mask);
364 sigaction(SIGILL, &dfl,
nullptr);
369 mxcsr_mask_bits = trap_mask;
371 reported = &reported_div;
373 msg_len =
sizeof(msg_div) - 1;
375 reported = &reported_inv;
377 msg_len =
sizeof(msg_inv) - 1;
379 reported = &reported_ovf;
381 msg_len =
sizeof(msg_ovf) - 1;
388 (sip->si_code == FPE_INTDIV || sip->si_code == FPE_INTOVF)) {
389 if (reported_int == 0) {
391 static constexpr char iprefix[] =
"FPE (continuing, skipping divide): ";
392 static constexpr char msg_idiv[] =
"integer divide\n";
393 static constexpr char msg_iovf[] =
"integer overflow\n";
394 const char *imsg = sip->si_code == FPE_INTDIV ? msg_idiv : msg_iovf;
395 size_t imsg_len =
sizeof(msg_iovf) - 1;
396 if (sip->si_code == FPE_INTDIV) {
397 imsg_len =
sizeof(msg_idiv) - 1;
399 write(STDERR_FILENO, iprefix,
sizeof(iprefix) - 1);
400 write(STDERR_FILENO, imsg, imsg_len);
404 static constexpr char stuck[] =
405 "FPE integer divide: could not skip faulting instruction\n";
406 write(STDERR_FILENO, stuck,
sizeof(stuck) - 1);
413 switch (sip->si_code) {
415 reported = &reported_div;
417 msg_len =
sizeof(msg_div) - 1;
421 reported = &reported_inv;
423 msg_len =
sizeof(msg_inv) - 1;
427 reported = &reported_ovf;
429 msg_len =
sizeof(msg_ovf) - 1;
437 if (*reported == 0) {
439 write(STDERR_FILENO, prefix,
sizeof(prefix) - 1);
440 write(STDERR_FILENO, msg, msg_len);
445#if defined(__linux__) && (defined(__x86_64__) || defined(__i386__))
446 ucontext_t *ctx =
static_cast<ucontext_t *
>(scp);
447 if (ctx->uc_mcontext.fpregs) {
451 ctx->uc_mcontext.fpregs->swd &= ~0x3Fu;
452 ctx->uc_mcontext.fpregs->mxcsr &= ~0x3Fu;
453 ctx->uc_mcontext.fpregs->mxcsr |= mxcsr_mask_bits;
457 ctx->uc_mcontext.fpregs->cwd |= (1u << 2);
460 ctx->uc_mcontext.fpregs->cwd |= (1u << 0);
463 ctx->uc_mcontext.fpregs->cwd |= (1u << 3);
466#elif defined(__linux__) && defined(__aarch64__)
470 constexpr uint32_t kFpsimdMagic = 0x46508001u;
471 constexpr unsigned kFpcrIoe = 1u << 8;
472 constexpr unsigned kFpcrDze = 1u << 9;
473 constexpr unsigned kFpcrOfe = 1u << 10;
474 unsigned fpcr_clear = kFpcrIoe | kFpcrDze | kFpcrOfe;
476 fpcr_clear = kFpcrDze;
478 fpcr_clear = kFpcrIoe;
480 fpcr_clear = kFpcrOfe;
482 auto *ctx =
static_cast<ucontext_t *
>(scp);
484 reinterpret_cast<unsigned char *
>(ctx->uc_mcontext.__reserved);
485 unsigned char *end = p +
sizeof(ctx->uc_mcontext.__reserved);
495 while (p +
sizeof(A64Head) <= end) {
496 auto *h =
reinterpret_cast<A64Head *
>(p);
497 if (h->magic == 0 || h->size <
sizeof(A64Head)) {
500 if (h->magic == kFpsimdMagic && h->size >=
sizeof(Fpsimd) &&
501 p + h->size <= end) {
502 auto *f =
reinterpret_cast<Fpsimd *
>(p);
504 f->fpcr &= ~fpcr_clear;
512#elif defined(__APPLE__) && defined(__x86_64__)
516 auto *ctx =
static_cast<ucontext_t *
>(scp);
517 if (ctx->uc_mcontext) {
518 auto &fs = ctx->uc_mcontext->__fs;
519 fs.__fpu_mxcsr &= ~0x3Fu;
520 fs.__fpu_mxcsr |= mxcsr_mask_bits;
522 fs.__fpu_fcw.__zdiv = 1;
525 fs.__fpu_fcw.__invalid = 1;
528 fs.__fpu_fcw.__ovrfl = 1;
530 fs.__fpu_fsw.__invalid = 0;
531 fs.__fpu_fsw.__denorm = 0;
532 fs.__fpu_fsw.__zdiv = 0;
533 fs.__fpu_fsw.__ovrfl = 0;
534 fs.__fpu_fsw.__undfl = 0;
535 fs.__fpu_fsw.__precis = 0;
537#elif defined(__APPLE__) && defined(__aarch64__)
538 constexpr unsigned kFpcrIoe = 1u << 8;
539 constexpr unsigned kFpcrDze = 1u << 9;
540 constexpr unsigned kFpcrOfe = 1u << 10;
541 unsigned fpcr_clear = kFpcrIoe | kFpcrDze | kFpcrOfe;
543 fpcr_clear = kFpcrDze;
545 fpcr_clear = kFpcrIoe;
547 fpcr_clear = kFpcrOfe;
549 auto *ctx =
static_cast<ucontext_t *
>(scp);
550 if (ctx->uc_mcontext) {
551 ctx->uc_mcontext->__ns.__fpsr &= ~0x1Fu;
552 ctx->uc_mcontext->__ns.__fpcr &= ~fpcr_clear;
555 static_cast<void>(sig);
562 SetUnhandledExceptionFilter(windowsFPEHandler);
564 _controlfp_s(
nullptr, 0, _MCW_EM);
565 _controlfp_s(
nullptr, ~(_EM_ZERODIVIDE | _EM_INVALID | _EM_OVERFLOW),
567#elif defined(__unix__)
569 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
570#elif defined(__APPLE__) && defined(__aarch64__)
574 env.__fpcr |= ((1u << 8) | (1u << 9) | (1u << 10));
576#elif defined(__APPLE__) && defined(__x86_64__)
578 _MM_SET_EXCEPTION_MASK(
580 ~(_MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW));
582 fprintf(stderr,
"FPE trapping not supported on this platform.\n");
587 struct sigaction act;
589 sigemptyset(&act.sa_mask);
590 act.sa_flags = SA_SIGINFO;
591 sigaction(SIGFPE, &act,
nullptr);
592#if defined(__APPLE__) && defined(__aarch64__)
595 sigaction(SIGILL, &act,
nullptr);