Skip to content

Instantly share code, notes, and snippets.

@nattoheaven
Created September 4, 2013 14:51
Show Gist options
  • Select an option

  • Save nattoheaven/6438054 to your computer and use it in GitHub Desktop.

Select an option

Save nattoheaven/6438054 to your computer and use it in GitHub Desktop.

Revisions

  1. nattoheaven created this gist Sep 4, 2013.
    48 changes: 48 additions & 0 deletions tsxtest.goodtsx.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <unistd.h>

    #include <immintrin.h>

    static volatile double zr = 0.0;
    static volatile double zi = 0.0;
    static const double cr = 0.1;
    static const double ci = 0.1;


    static void
    *run(void *arg)
    {
    for (int i = 0; i < 1000000; ++i) {
    while (true) {
    if (_xbegin() == _XBEGIN_STARTED) {
    double oldzr = zr;
    double oldzi = zi;
    double newzr = oldzr * oldzr - oldzi * oldzi + cr;
    double newzi = 2 * oldzr * oldzi + ci;
    zr = newzr;
    zi = newzi;
    _xend();
    break;
    }
    _mm256_zeroall();
    }
    }
    return 0;
    }

    int
    main(int argc, char **argv)
    {
    int n = atoi(argv[1]);
    pthread_t *threads = new pthread_t[n];
    for (int i = 0; i < n; ++i) {
    pthread_create(&threads[i], 0, run, 0);
    }
    for (int i = 0; i < n; ++i) {
    pthread_join(threads[i], 0);
    }
    printf("%f + %fi\n", zr, zi);
    return 0;
    }