source: trunk/src/testing/app/qshare/FastDXT/util.cpp @ 4

Revision 4, 4.3 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * Fast DXT - a realtime DXT compression tool
3 *
4 * Author : Luc Renambot
5 *
6 * Copyright (C) 2007 Electronic Visualization Laboratory,
7 * University of Illinois at Chicago
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 *  * Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *  * Redistributions in binary form must reproduce the above
17 *    copyright notice, this list of conditions and the following disclaimer
18 *    in the documentation and/or other materials provided with the distribution.
19 *  * Neither the name of the University of Illinois at Chicago nor
20 *    the names of its contributors may be used to endorse or promote
21 *    products derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
36 *
37 *****************************************************************************/
38
39#include "util.h"
40
41
42#if defined(WIN32)
43#include <io.h>
44static LARGE_INTEGER perf_freq;
45static LARGE_INTEGER perf_start;
46static HANDLE win_err; // stderr in a console
47#elif defined(__APPLE__)
48#include <mach/mach_time.h>
49static double perf_conversion = 0.0;
50static uint64_t perf_start;
51#else
52struct timeval tv_start;
53#endif
54
55#if !defined(WIN32)
56#include <sys/stat.h>
57#include <unistd.h>
58#endif
59
60#include <fcntl.h>
61
62
63void dxt_aInitialize()
64{
65#if defined(WIN32)
66        QueryPerformanceCounter(&perf_start);
67        QueryPerformanceFrequency(&perf_freq);
68        AllocConsole();
69        win_err =  GetStdHandle(STD_ERROR_HANDLE);
70#elif defined(__APPLE__)
71        if( perf_conversion == 0.0 )
72        {
73                mach_timebase_info_data_t info;
74                kern_return_t err = mach_timebase_info( &info );
75
76                //Convert the timebase into seconds
77                if( err == 0  )
78                        perf_conversion = 1e-9 * (double) info.numer / (double) info.denom;
79        }
80                // Start of time
81        perf_start = mach_absolute_time();
82
83                // Initialize the random generator
84        srand(getpid());
85#else
86                // Start of time
87        gettimeofday(&tv_start,0);
88                // Initialize the random generator
89        srand(getpid());
90#endif
91}
92
93double dxt_aTime()
94// return time since start of process in seconds
95{
96#if defined(WIN32)
97    LARGE_INTEGER perf_counter;
98#else
99    struct timeval tv;
100#endif
101
102#if defined(WIN32)
103        // Windows: get performance counter and subtract starting mark
104        QueryPerformanceCounter(&perf_counter);
105        return (double)(perf_counter.QuadPart - perf_start.QuadPart) / (double)perf_freq.QuadPart;
106#elif defined(__APPLE__)
107    uint64_t difference = mach_absolute_time() - perf_start;
108    return perf_conversion * (double) difference;
109#else
110        // UNIX: gettimeofday
111        gettimeofday(&tv,0);
112        return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0;
113#endif
114}
115
116
117
118#if defined(WIN32)
119float
120drand48(void)
121{
122        return (((float) rand()) / RAND_MAX);
123}
124#endif
125
126
127void *aligned_malloc(size_t size, size_t align_size) {
128
129  char *ptr,*ptr2,*aligned_ptr;
130  int align_mask = (int)align_size - 1;
131
132  ptr=(char *)malloc(size + align_size + sizeof(int));
133  if(ptr==NULL) return(NULL);
134
135  ptr2 = ptr + sizeof(int);
136  aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask));
137
138
139  ptr2 = aligned_ptr - sizeof(int);
140  *((int *)ptr2)=(int)(aligned_ptr - ptr);
141
142  return(aligned_ptr);
143}
144
145void aligned_free(void *ptr)
146{
147        int *ptr2=(int *)ptr - 1;
148        ptr = (char*)ptr - *ptr2;
149        free(ptr);
150}
151
152
Note: See TracBrowser for help on using the repository browser.