int main() {
    int repeat = 500;
    int size = TESTSIZE;
    tellmeall();
    printf("array container benchmarks\n");
    array_container_t* B = array_container_create();
    BEST_TIME(add_test(B), 0, repeat, size);
    int answer = contains_test(B);
    size = 1 << 16;
    BEST_TIME(contains_test(B), answer, repeat, size);

    size = (1 << 16) / 3;
    BEST_TIME(remove_test(B), 0, repeat, size);
    array_container_free(B);

    for (int howmany = 32; howmany <= (1 << 16); howmany *= 8) {
        array_container_t* Bt = array_container_create();
        for (int j = 0; j < howmany; ++j) {
            array_container_add(Bt, (uint16_t)pcg32_random());
        }
        size_t nbrtestvalues = 1024;
        uint16_t* testvalues = malloc(nbrtestvalues * sizeof(uint16_t));
        printf("\n number of values in container = %d\n", Bt->cardinality);
        int card = array_container_cardinality(Bt);
        uint32_t* out = malloc(sizeof(uint32_t) * (unsigned long)card);
        BEST_TIME(array_container_to_uint32_array(out, Bt, 1234), card, repeat,
                  card);
        free(out);
        BEST_TIME_PRE_ARRAY(Bt, array_container_contains, array_cache_prefetch,
                            testvalues, nbrtestvalues);
        BEST_TIME_PRE_ARRAY(Bt, array_container_contains, array_cache_flush,
                            testvalues, nbrtestvalues);
        free(testvalues);
        array_container_free(Bt);
    }
    printf("\n");

    array_container_t* B1 = array_container_create();
    for (int x = 0; x < 1 << 16; x += 3) {
        array_container_add(B1, (uint16_t)x);
    }
    array_container_t* B2 = array_container_create();
    for (int x = 0; x < 1 << 16; x += 5) {
        array_container_add(B2, (uint16_t)x);
    }
    int32_t inputsize = B1->cardinality + B2->cardinality;
    array_container_t* BO = array_container_create();
    printf("\nUnion and intersections...\n");
    printf("\nNote:\n");
    printf(
        "union times are expressed in cycles per number of input elements "
        "(both arrays)\n");
    printf(
        "intersection times are expressed in cycles per number of output "
        "elements\n\n");
    printf("==intersection and union test 1 \n");
    printf("input 1 cardinality = %d, input 2 cardinality = %d \n",
           B1->cardinality, B2->cardinality);
    answer = union_test(B1, B2, BO);
    printf("union cardinality = %d \n", answer);
    printf("B1 card = %d B2 card = %d \n", B1->cardinality, B2->cardinality);
    BEST_TIME(union_test(B1, B2, BO), answer, repeat, inputsize);
    answer = intersection_test(B1, B2, BO);
    printf("intersection cardinality = %d \n", answer);
    BEST_TIME(intersection_test(B1, B2, BO), answer, repeat, answer);
    printf("==intersection and union test 2 \n");
    array_container_clear(B1);
    array_container_clear(B2);
    for (int x = 0; x < 1 << 16; x += 16) {
        array_container_add(B1, (uint16_t)x);
    }
    for (int x = 1; x < 1 << 16; x += x) {
        array_container_add(B2, (uint16_t)x);
    }
    printf("input 1 cardinality = %d, input 2 cardinality = %d \n",
           B1->cardinality, B2->cardinality);
    answer = union_test(B1, B2, BO);
    printf("union cardinality = %d \n", answer);
    printf("B1 card = %d B2 card = %d \n", B1->cardinality, B2->cardinality);
    BEST_TIME(union_test(B1, B2, BO), answer, repeat, inputsize);
    answer = intersection_test(B1, B2, BO);
    printf("intersection cardinality = %d \n", answer);
    BEST_TIME(intersection_test(B1, B2, BO), answer, repeat, answer);

    array_container_free(B1);
    array_container_free(B2);
    array_container_free(BO);
    return 0;
}
Beispiel #2
0
bool intersection_test(const Bounding *b1, const Bounding *b2, double *intersection_time)
{
    assert(b1 && b2 && "Bad bounding pointers.");
    assert(intersection_time && "Bad intersection time pointer.");

    *intersection_time = nan(NULL);

    if (bounding_composite == b2->bounding_type &&
        bounding_composite != b1->bounding_type)
    {
        const Bounding *t = b1;
        b1 = b2;
        b2 = t;
    }

    if (bounding_composite == b1->bounding_type)
    {
        size_t intersections_count = b1->data.composite_data.children_count;
        double *intersection_times = _alloca(intersections_count * sizeof(double)); // compiler bug?
        bool *intersection_facts = _alloca(intersections_count * sizeof(bool)); // compiler bug?
        bool composite_intersection_result = false;

        for (size_t i = 0; i < intersections_count; i++)
        {
            intersection_times[i] = nan(NULL);
            intersection_facts[i] = intersection_test(b2, &b1->data.composite_data.children[i], &intersection_times[i]);
            composite_intersection_result |= intersection_facts[i];
        }

        *intersection_time = __get_min_intersection_time(intersection_times, intersection_facts, intersections_count);
        return composite_intersection_result;
    }

    assert(bounding_composite != b1->bounding_type &&
           bounding_composite != b2->bounding_type &&
           "Can't test composite boundings.");

    Vector axes[6];
    double intersection_times[15]; // 6 + 3 * 3.
    bool intersection_facts[15];

    __bounding_get_intersection_axes(b1, &axes[0]);
    __bounding_get_intersection_axes(b2, &axes[3]);

    bool intersection_result = true;
    for (size_t i = 0; i < 6; i++)
    {
        intersection_times[i] = nan(NULL);
        intersection_facts[i] = __bounding_axis_test(b1, b2, &axes[i], &intersection_times[i]);
        intersection_result &= intersection_facts[i];
    }

    size_t intersection_times_counter = 6;
    for (size_t i = 0; i < 3; i++)
    {
        for (size_t j = 3; j < 6; j++)
        {
            if (vector_eq(&axes[i], &axes[j]))
            {
                continue;
            }

            Vector t;
            if (!(vector_tolerance_eq(0.0, vector_angle(&axes[i], &axes[j])) ||
                  vector_tolerance_eq(M_PI, vector_angle(&axes[i], &axes[j]))))
            {
                vector_vector_mul(&axes[i], &axes[j], &t);
            }
            else
            {
                vector_get_orthogonal(&axes[i], &t);
            }
            VECTOR_NORMALIZE(&t);

            intersection_facts[intersection_times_counter] = __bounding_axis_test(b1, b2, &t, &intersection_times[intersection_times_counter]);
            intersection_result &= intersection_facts[intersection_times_counter];
            intersection_times_counter++;
        }
    }

    *intersection_time = __get_min_intersection_time(intersection_times, intersection_facts, intersection_times_counter);
    return intersection_result;
}