scfx_mant.cpp revision 12027
12568SN/A/*****************************************************************************
28668Sgeoffrey.blake@arm.com
38668Sgeoffrey.blake@arm.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
48668Sgeoffrey.blake@arm.com  more contributor license agreements.  See the NOTICE file distributed
58668Sgeoffrey.blake@arm.com  with this work for additional information regarding copyright ownership.
68668Sgeoffrey.blake@arm.com  Accellera licenses this file to you under the Apache License, Version 2.0
78668Sgeoffrey.blake@arm.com  (the "License"); you may not use this file except in compliance with the
88668Sgeoffrey.blake@arm.com  License.  You may obtain a copy of the License at
98668Sgeoffrey.blake@arm.com
108668Sgeoffrey.blake@arm.com    http://www.apache.org/licenses/LICENSE-2.0
118668Sgeoffrey.blake@arm.com
128668Sgeoffrey.blake@arm.com  Unless required by applicable law or agreed to in writing, software
138668Sgeoffrey.blake@arm.com  distributed under the License is distributed on an "AS IS" BASIS,
142568SN/A  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
157636Ssteve.reinhardt@amd.com  implied.  See the License for the specific language governing
162568SN/A  permissions and limitations under the License.
172568SN/A
182568SN/A *****************************************************************************/
192568SN/A
202568SN/A/*****************************************************************************
212568SN/A
222568SN/A  scfx_mant.cpp -
232568SN/A
242568SN/A  Original Author: Robert Graulich, Synopsys, Inc.
252568SN/A                   Martin Janssen,  Synopsys, Inc.
262568SN/A
272568SN/A *****************************************************************************/
282568SN/A
292568SN/A/*****************************************************************************
302568SN/A
312568SN/A  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
322568SN/A  changes you are making here.
332568SN/A
342568SN/A      Name, Affiliation, Date:
352568SN/A  Description of Modification:
362568SN/A
372568SN/A *****************************************************************************/
382568SN/A
392568SN/A
402665Ssaidi@eecs.umich.edu// $Log: scfx_mant.cpp,v $
412665Ssaidi@eecs.umich.edu// Revision 1.1.1.1  2006/12/15 20:20:04  acg
422665Ssaidi@eecs.umich.edu// SystemC 2.3
432568SN/A//
442568SN/A// Revision 1.3  2006/01/13 18:53:58  acg
452568SN/A// Andy Goodrich: added $Log command so that CVS comments are reproduced in
462568SN/A// the source.
472568SN/A//
482568SN/A
492568SN/A#include "sysc/datatypes/fx/scfx_mant.h"
503260Ssaidi@eecs.umich.edu
518229Snate@binkert.org
523260Ssaidi@eecs.umich.edunamespace sc_dt
538229Snate@binkert.org{
545314Sstever@gmail.com
552590SN/A// ----------------------------------------------------------------------------
563348Sbinkertn@umich.edu//  word memory management
572568SN/A// ----------------------------------------------------------------------------
582568SN/A
595735Snate@binkert.orgclass word_list { // Entry in free_words bucket.
605735Snate@binkert.org  public:
614022Sstever@eecs.umich.edu    word_list* m_next_p;
624022Sstever@eecs.umich.edu};
634022Sstever@eecs.umich.edu
644022Sstever@eecs.umich.edustatic inline
654022Sstever@eecs.umich.eduint
664022Sstever@eecs.umich.edunext_pow2_index( std::size_t size )
674022Sstever@eecs.umich.edu{
682641Sstever@eecs.umich.edu    int index = scfx_find_msb( size );
694022Sstever@eecs.umich.edu    // If this was a power of 2 we are one bucket too low.
704022Sstever@eecs.umich.edu    if( ~ (1 << index) & size ) index ++;
712641Sstever@eecs.umich.edu    // If this is a 64-bit machine and we are using 32-bit words go down
724022Sstever@eecs.umich.edu	// one slot size, as all the slots are 2x in size.
734022Sstever@eecs.umich.edu    if ( index != 0 && ( sizeof(word_list) != sizeof(word) ) )
744022Sstever@eecs.umich.edu	{
754022Sstever@eecs.umich.edu		index -= 1;
764473Sstever@eecs.umich.edu	}
774473Sstever@eecs.umich.edu    return index;
785319Sstever@gmail.com}
795319Sstever@gmail.com
805319Sstever@gmail.comstatic word_list* free_words[32] = { 0 };
814022Sstever@eecs.umich.edu
824626Sstever@eecs.umich.eduword*
834022Sstever@eecs.umich.eduscfx_mant::alloc_word( std::size_t size )
844022Sstever@eecs.umich.edu{
854626Sstever@eecs.umich.edu    const int ALLOC_SIZE = 128;
864022Sstever@eecs.umich.edu
874628Sstever@eecs.umich.edu    int slot_index = next_pow2_index( size );
884628Sstever@eecs.umich.edu
894022Sstever@eecs.umich.edu    int alloc_size = ( 1 << slot_index );
904022Sstever@eecs.umich.edu
914022Sstever@eecs.umich.edu    word_list*& slot = free_words[slot_index];
924022Sstever@eecs.umich.edu
934022Sstever@eecs.umich.edu    if( ! slot )
944022Sstever@eecs.umich.edu    {
954022Sstever@eecs.umich.edu        slot = new word_list[ALLOC_SIZE * alloc_size];
964022Sstever@eecs.umich.edu	int i;
974022Sstever@eecs.umich.edu	for( i = 0; i < alloc_size*(ALLOC_SIZE-1) ; i+=alloc_size )
984022Sstever@eecs.umich.edu	{
994022Sstever@eecs.umich.edu	    slot[i].m_next_p = &slot[i+alloc_size];
1004022Sstever@eecs.umich.edu	}
1014022Sstever@eecs.umich.edu	slot[i].m_next_p = 0;
1024626Sstever@eecs.umich.edu    }
1034626Sstever@eecs.umich.edu
1044022Sstever@eecs.umich.edu    word* result = (word*)slot;
1054022Sstever@eecs.umich.edu    free_words[slot_index] = slot[0].m_next_p;
1065319Sstever@gmail.com    return result;
1074022Sstever@eecs.umich.edu}
1084022Sstever@eecs.umich.edu
1097465Ssteve.reinhardt@amd.comvoid
1104628Sstever@eecs.umich.eduscfx_mant::free_word( word* array, std::size_t size )
1117465Ssteve.reinhardt@amd.com{
1127465Ssteve.reinhardt@amd.com    if( array && size )
1137465Ssteve.reinhardt@amd.com    {
1147465Ssteve.reinhardt@amd.com        int slot_index = next_pow2_index( size );
1154628Sstever@eecs.umich.edu	word_list* wl_p = (word_list*)array;
1167465Ssteve.reinhardt@amd.com
1177465Ssteve.reinhardt@amd.com	wl_p->m_next_p = free_words[slot_index];
1187465Ssteve.reinhardt@amd.com	free_words[slot_index] = wl_p;
1197465Ssteve.reinhardt@amd.com    }
1207465Ssteve.reinhardt@amd.com}
1217465Ssteve.reinhardt@amd.com
1227465Ssteve.reinhardt@amd.com} // namespace sc_dt
1235319Sstever@gmail.com
1247465Ssteve.reinhardt@amd.com
1254022Sstever@eecs.umich.edu// Taf!
1264626Sstever@eecs.umich.edu