OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [bench/] [shootout/] [binary-tree.c] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
/*
2
Redistribution and use in source and binary forms, with or without
3
modification, are permitted provided that the following conditions are met:
4
 
5
    * Redistributions of source code must retain the above copyright
6
    notice, this list of conditions and the following disclaimer.
7
 
8
    * Redistributions in binary form must reproduce the above copyright
9
    notice, this list of conditions and the following disclaimer in the
10
    documentation and/or other materials provided with the distribution.
11
 
12
    * Neither the name of "The Computer Language Benchmarks Game" nor the
13
    name of "The Computer Language Shootout Benchmarks" nor the names of
14
    its contributors may be used to endorse or promote products derived
15
    from this software without specific prior written permission.
16
 
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE.
28
*/
29
 
30
/* The Computer Language Shootout Benchmarks
31
   http://shootout.alioth.debian.org/
32
 
33
   contributed by Kevin Carson
34
   compilation:
35
       gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm
36
       icc -O3 -ip -unroll -static binary-trees.c -lm
37
*/
38
 
39
#include <malloc.h>
40
#include <math.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
 
44
 
45
typedef struct tn {
46
    struct tn*    left;
47
    struct tn*    right;
48
    long          item;
49
} treeNode;
50
 
51
 
52
treeNode* NewTreeNode(treeNode* left, treeNode* right, long item)
53
{
54
    treeNode*    new;
55
 
56
    new = (treeNode*)malloc(sizeof(treeNode));
57
 
58
    new->left = left;
59
    new->right = right;
60
    new->item = item;
61
 
62
    return new;
63
} /* NewTreeNode() */
64
 
65
 
66
long ItemCheck(treeNode* tree)
67
{
68
    if (tree->left == NULL)
69
        return tree->item;
70
    else
71
        return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right);
72
} /* ItemCheck() */
73
 
74
 
75
treeNode* BottomUpTree(long item, unsigned depth)
76
{
77
    if (depth > 0)
78
        return NewTreeNode
79
        (
80
            BottomUpTree(2 * item - 1, depth - 1),
81
            BottomUpTree(2 * item, depth - 1),
82
            item
83
        );
84
    else
85
        return NewTreeNode(NULL, NULL, item);
86
} /* BottomUpTree() */
87
 
88
 
89
void DeleteTree(treeNode* tree)
90
{
91
    if (tree->left != NULL)
92
    {
93
        DeleteTree(tree->left);
94
        DeleteTree(tree->right);
95
    }
96
 
97
    free(tree);
98
} /* DeleteTree() */
99
 
100
 
101
int main(int argc, char* argv[])
102
{
103
    unsigned   N, depth, minDepth, maxDepth, stretchDepth;
104
    treeNode   *stretchTree, *longLivedTree, *tempTree;
105
 
106
    N = atol(argv[1]);
107
 
108
    minDepth = 4;
109
 
110
    if ((minDepth + 2) > N)
111
        maxDepth = minDepth + 2;
112
    else
113
        maxDepth = N;
114
 
115
    stretchDepth = maxDepth + 1;
116
 
117
    stretchTree = BottomUpTree(0, stretchDepth);
118
    printf
119
    (
120
        "stretch tree of depth %u\t check: %li\n",
121
        stretchDepth,
122
        ItemCheck(stretchTree)
123
    );
124
 
125
    DeleteTree(stretchTree);
126
 
127
    longLivedTree = BottomUpTree(0, maxDepth);
128
 
129
    for (depth = minDepth; depth <= maxDepth; depth += 2)
130
    {
131
        long    i, iterations, check;
132
 
133
        iterations = pow(2, maxDepth - depth + minDepth);
134
 
135
        check = 0;
136
 
137
        for (i = 1; i <= iterations; i++)
138
        {
139
            tempTree = BottomUpTree(i, depth);
140
            check += ItemCheck(tempTree);
141
            DeleteTree(tempTree);
142
 
143
            tempTree = BottomUpTree(-i, depth);
144
            check += ItemCheck(tempTree);
145
            DeleteTree(tempTree);
146
        } /* for(i = 1...) */
147
 
148
        printf
149
        (
150
            "%li\t trees of depth %u\t check: %li\n",
151
            iterations * 2,
152
            depth,
153
            check
154
        );
155
    } /* for(depth = minDepth...) */
156
 
157
    printf
158
    (
159
        "long lived tree of depth %u\t check: %li\n",
160
        maxDepth,
161
        ItemCheck(longLivedTree)
162
    );
163
 
164
    return 0;
165
} /* main() */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.