00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2006 by all Contributors. 00005 All Rights reserved. 00006 00007 The contents of this file are subject to the restrictions and limitations 00008 set forth in the SystemC Open Source License Version 2.4 (the "License"); 00009 You may not use this file except in compliance with such restrictions and 00010 limitations. You may obtain instructions on how to receive a copy of the 00011 License at http://www.systemc.org/. Software distributed by Contributors 00012 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 00013 ANY KIND, either express or implied. See the License for the specific 00014 language governing rights and limitations under the License. 00015 00016 *****************************************************************************/ 00017 00018 /***************************************************************************** 00019 00020 scfx_mant.cpp - 00021 00022 Original Author: Robert Graulich, Synopsys, Inc. 00023 Martin Janssen, Synopsys, Inc. 00024 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 00029 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 00030 changes you are making here. 00031 00032 Name, Affiliation, Date: 00033 Description of Modification: 00034 00035 *****************************************************************************/ 00036 00037 00038 // $Log: scfx_mant.cpp,v $ 00039 // Revision 1.1.1.1 2006/12/15 20:31:36 acg 00040 // SystemC 2.2 00041 // 00042 // Revision 1.3 2006/01/13 18:53:58 acg 00043 // Andy Goodrich: added $Log command so that CVS comments are reproduced in 00044 // the source. 00045 // 00046 00047 #include "sysc/datatypes/fx/scfx_mant.h" 00048 00049 00050 namespace sc_dt 00051 { 00052 00053 // ---------------------------------------------------------------------------- 00054 // word memory management 00055 // ---------------------------------------------------------------------------- 00056 00057 class word_list { // Entry in free_words bucket. 00058 public: 00059 word_list* m_next_p; 00060 }; 00061 00062 static inline 00063 int 00064 next_pow2_index( std::size_t size ) 00065 { 00066 int index = scfx_find_msb( size ); 00067 // If this was a power of 2 we are one bucket too low. 00068 if( ~ (1 << index) & size ) index ++; 00069 // If this is a 64-bit machine and we are using 32-bit words go down 00070 // one slot size, as all the slots are 2x in size. 00071 if ( index != 0 && ( sizeof(word_list) != sizeof(word) ) ) 00072 { 00073 index -= 1; 00074 } 00075 return index; 00076 } 00077 00078 static word_list* free_words[32] = { 0 }; 00079 00080 word* 00081 scfx_mant::alloc_word( std::size_t size ) 00082 { 00083 const int ALLOC_SIZE = 128; 00084 00085 int slot_index = next_pow2_index( size ); 00086 00087 int alloc_size = ( 1 << slot_index ); 00088 00089 word_list*& slot = free_words[slot_index]; 00090 00091 if( ! slot ) 00092 { 00093 slot = new word_list[ALLOC_SIZE * alloc_size]; 00094 int i; 00095 for( i = 0; i < alloc_size*(ALLOC_SIZE-1) ; i+=alloc_size ) 00096 { 00097 slot[i].m_next_p = &slot[i+alloc_size]; 00098 } 00099 slot[i].m_next_p = 0; 00100 } 00101 00102 word* result = (word*)slot; 00103 free_words[slot_index] = slot[0].m_next_p; 00104 return result; 00105 } 00106 00107 void 00108 scfx_mant::free_word( word* array, std::size_t size ) 00109 { 00110 if( array && size ) 00111 { 00112 int slot_index = next_pow2_index( size ); 00113 word_list* wl_p = (word_list*)array; 00114 00115 wl_p->m_next_p = free_words[slot_index]; 00116 free_words[slot_index] = wl_p; 00117 } 00118 } 00119 00120 } // namespace sc_dt 00121 00122 00123 // Taf!