/****************************************************************************** * Fast DXT - a realtime DXT compression tool * * Author : Luc Renambot * * Copyright (C) 2007 Electronic Visualization Laboratory, * University of Illinois at Chicago * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the distribution. * * Neither the name of the University of Illinois at Chicago nor * the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ * *****************************************************************************/ #include "util.h" #if defined(WIN32) #include static LARGE_INTEGER perf_freq; static LARGE_INTEGER perf_start; static HANDLE win_err; // stderr in a console #elif defined(__APPLE__) #include static double perf_conversion = 0.0; static uint64_t perf_start; #else struct timeval tv_start; #endif #if !defined(WIN32) #include #include #endif #include void dxt_aInitialize() { #if defined(WIN32) QueryPerformanceCounter(&perf_start); QueryPerformanceFrequency(&perf_freq); AllocConsole(); win_err = GetStdHandle(STD_ERROR_HANDLE); #elif defined(__APPLE__) if( perf_conversion == 0.0 ) { mach_timebase_info_data_t info; kern_return_t err = mach_timebase_info( &info ); //Convert the timebase into seconds if( err == 0 ) perf_conversion = 1e-9 * (double) info.numer / (double) info.denom; } // Start of time perf_start = mach_absolute_time(); // Initialize the random generator srand(getpid()); #else // Start of time gettimeofday(&tv_start,0); // Initialize the random generator srand(getpid()); #endif } double dxt_aTime() // return time since start of process in seconds { #if defined(WIN32) LARGE_INTEGER perf_counter; #else struct timeval tv; #endif #if defined(WIN32) // Windows: get performance counter and subtract starting mark QueryPerformanceCounter(&perf_counter); return (double)(perf_counter.QuadPart - perf_start.QuadPart) / (double)perf_freq.QuadPart; #elif defined(__APPLE__) uint64_t difference = mach_absolute_time() - perf_start; return perf_conversion * (double) difference; #else // UNIX: gettimeofday gettimeofday(&tv,0); return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0; #endif } #if defined(WIN32) float drand48(void) { return (((float) rand()) / RAND_MAX); } #endif void *aligned_malloc(size_t size, size_t align_size) { char *ptr,*ptr2,*aligned_ptr; int align_mask = (int)align_size - 1; ptr=(char *)malloc(size + align_size + sizeof(int)); if(ptr==NULL) return(NULL); ptr2 = ptr + sizeof(int); aligned_ptr = ptr2 + (align_size - ((size_t)ptr2 & align_mask)); ptr2 = aligned_ptr - sizeof(int); *((int *)ptr2)=(int)(aligned_ptr - ptr); return(aligned_ptr); } void aligned_free(void *ptr) { int *ptr2=(int *)ptr - 1; ptr = (char*)ptr - *ptr2; free(ptr); }