Skip to content

Instantly share code, notes, and snippets.

@comex
Last active August 13, 2023 22:12
Show Gist options
  • Select an option

  • Save comex/061c2f7316e5bfbded0fed50bf15b424 to your computer and use it in GitHub Desktop.

Select an option

Save comex/061c2f7316e5bfbded0fed50bf15b424 to your computer and use it in GitHub Desktop.

Revisions

  1. comex revised this gist Aug 13, 2023. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,6 @@
    #include <stdlib.h>
    #include <fenv.h>

    #ifdef _WIN32
    #include <intrin.h>
    #endif

    static void check(const char *where) {
    printf("%-40s: fegetround() => %#x\n", where, fegetround());
    }
  2. comex revised this gist Aug 13, 2023. 1 changed file with 7 additions and 13 deletions.
    20 changes: 7 additions & 13 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -10,28 +10,22 @@
    #endif

    static void check(const char *where) {
    printf("%21s: fegetround() => %#x\n", where, fegetround());
    printf("%-40s: fegetround() => %#x\n", where, fegetround());
    }

    static void handler(int _) {
    check("in signal handler");
    check("in signal handler before fesetround");
    assert(!fesetround(FE_DOWNWARD));
    check("in signal handler after fesetround");
    }

    int main() {
    int round = FE_UPWARD;
    printf("fesetround(%#x)\n", round);
    assert(!fesetround(round));
    check("before signal handler");

    assert(SIG_ERR != signal(SIGTRAP, handler));

    #ifdef _WIN32
    __debugbreak();
    #elif defined(__x86_64__)
    asm("int3");
    #else
    asm("bkpt");
    #endif
    assert(SIG_ERR != signal(SIGINT, handler));
    assert(!raise(SIGINT));

    check("after signal handler");
    }
    }
  3. comex revised this gist Aug 13, 2023. 1 changed file with 22 additions and 15 deletions.
    37 changes: 22 additions & 15 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -4,27 +4,34 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <fenv.h>
    #include <xmmintrin.h>

    static void handler(int _) {
    printf("fegetround() in signal handler => %#x\n", fegetround());
    #if defined(_M_X64) || defined(__x86_64__)
    printf("mxcsr: %x\n", _mm_getcsr());
    #ifdef _WIN32
    #include <intrin.h>
    #endif
    _Exit(0);

    static void check(const char *where) {
    printf("%21s: fegetround() => %#x\n", where, fegetround());
    }

    static void handler(int _) {
    check("in signal handler");
    }

    int main() {
    int round = FE_UPWARD;
    printf("fesetround(%#x)\n", round);
    assert(!fesetround(round));
    printf("fegetround() before signal handler => %#x\n", fegetround());
    #if defined(_M_X64) || defined(__x86_64__)
    printf("mxcsr: %x\n", _mm_getcsr());
    #endif
    assert(SIG_ERR != signal(SIGSEGV, handler));
    #ifndef _WIN32
    assert(SIG_ERR != signal(SIGBUS, handler));
    check("before signal handler");

    assert(SIG_ERR != signal(SIGTRAP, handler));

    #ifdef _WIN32
    __debugbreak();
    #elif defined(__x86_64__)
    asm("int3");
    #else
    asm("bkpt");
    #endif
    *(volatile int *)0xdeadbeefdeadbeef = 2;
    }

    check("after signal handler");
    }
  4. comex created this gist Aug 13, 2023.
    30 changes: 30 additions & 0 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #pragma STDC FENV_ACCESS ON
    #include <signal.h>
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fenv.h>
    #include <xmmintrin.h>

    static void handler(int _) {
    printf("fegetround() in signal handler => %#x\n", fegetround());
    #if defined(_M_X64) || defined(__x86_64__)
    printf("mxcsr: %x\n", _mm_getcsr());
    #endif
    _Exit(0);
    }

    int main() {
    int round = FE_UPWARD;
    printf("fesetround(%#x)\n", round);
    assert(!fesetround(round));
    printf("fegetround() before signal handler => %#x\n", fegetround());
    #if defined(_M_X64) || defined(__x86_64__)
    printf("mxcsr: %x\n", _mm_getcsr());
    #endif
    assert(SIG_ERR != signal(SIGSEGV, handler));
    #ifndef _WIN32
    assert(SIG_ERR != signal(SIGBUS, handler));
    #endif
    *(volatile int *)0xdeadbeefdeadbeef = 2;
    }