| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: cw.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: define global array with predefined control word entries */ |
| 10 |
/* */ |
| 11 |
/* Author: Peter Bastian */ |
| 12 |
/* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */ |
| 13 |
/* Universitaet Heidelberg */ |
| 14 |
/* Im Neuenheimer Feld 368 */ |
| 15 |
/* 6900 Heidelberg */ |
| 16 |
/* Internet: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 11.01.95 begin, ug version 3.0 */ |
| 19 |
/* */ |
| 20 |
/* Remarks: */ |
| 21 |
/* */ |
| 22 |
/****************************************************************************/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
#include <cstring> |
| 26 |
#include <cstdio> |
| 27 |
|
| 28 |
/* define this to exclude extern definition of global arrays */ |
| 29 |
#define __COMPILE_CW__ |
| 30 |
|
| 31 |
|
| 32 |
#include <dune/uggrid/ugdevices.h> |
| 33 |
#include <dune/uggrid/low/architecture.h> |
| 34 |
#include <dune/uggrid/low/debug.h> |
| 35 |
#include <dune/uggrid/low/misc.h> |
| 36 |
|
| 37 |
#include "algebra.h" |
| 38 |
#include "refine.h" |
| 39 |
#include "cw.h" |
| 40 |
|
| 41 |
USING_UG_NAMESPACE |
| 42 |
USING_UGDIM_NAMESPACE |
| 43 |
using namespace PPIF; |
| 44 |
|
| 45 |
/****************************************************************************/ |
| 46 |
/* */ |
| 47 |
/* defines in the following order */ |
| 48 |
/* */ |
| 49 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 50 |
/* other constants */ |
| 51 |
/* macros */ |
| 52 |
/* */ |
| 53 |
/****************************************************************************/ |
| 54 |
|
| 55 |
#define CW_EDOBJ (BITWISE_TYPE(EDOBJ) | BITWISE_TYPE(LIOBJ)) |
| 56 |
/* take both edges and links in one */ |
| 57 |
#define CW_GROBJ BITWISE_TYPE(GROBJ) |
| 58 |
#define CW_MGOBJ BITWISE_TYPE(MGOBJ) |
| 59 |
#define CW_NDOBJ BITWISE_TYPE(NDOBJ) |
| 60 |
#define CW_VEOBJ BITWISE_TYPE(VEOBJ) |
| 61 |
|
| 62 |
#define CW_VXOBJS (BITWISE_TYPE(IVOBJ) | BITWISE_TYPE(BVOBJ)) |
| 63 |
#define CW_ELOBJS (BITWISE_TYPE(IEOBJ) | BITWISE_TYPE(BEOBJ)) |
| 64 |
#define CW_GEOMOBJS (CW_VXOBJS | CW_ELOBJS | CW_NDOBJ | CW_EDOBJ | CW_GROBJ) |
| 65 |
/* NOTE: CW_GEOMOBJS and GEOM_OBJECTS differ*/ |
| 66 |
|
| 67 |
/** @name Status of control entry */ |
| 68 |
/*@{*/ |
| 69 |
#define CE_USED 1 |
| 70 |
#define CE_LOCKED 1 |
| 71 |
/*@}*/ |
| 72 |
|
| 73 |
/** @name Initializer macros for control entry and word predefines */ |
| 74 |
/*@{*/ |
| 75 |
#define CE_INIT(mode,cw,ce,objs) {mode, STR(ce), cw ## CW, ce ## CE, ce ## SHIFT, ce ## LEN, objs} |
| 76 |
/*@}*/ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
/****************************************************************************/ |
| 81 |
/* */ |
| 82 |
/* data structures used in this source file (exported data structures are */ |
| 83 |
/* in the corresponding include file!) */ |
| 84 |
/* */ |
| 85 |
/****************************************************************************/ |
| 86 |
|
| 87 |
/** \brief Description of a control word */ |
| 88 |
typedef struct { |
| 89 |
|
| 90 |
/** \brief where in object is it ? */ |
| 91 |
UINT offset_in_object; |
| 92 |
|
| 93 |
/** \brief bitwise object ID */ |
| 94 |
INT objt_used; |
| 95 |
|
| 96 |
/** \brief used bits */ |
| 97 |
UINT used_mask; |
| 98 |
|
| 99 |
} CONTROL_WORD; |
| 100 |
|
| 101 |
/** \brief Description of a control word predefines */ |
| 102 |
typedef struct { |
| 103 |
INT control_word_id; /**< Index in control_words */ |
| 104 |
UINT offset_in_object ; /**< Where in object is it ? */ |
| 105 |
INT objt_used; /**< Bitwise object ID */ |
| 106 |
} CONTROL_WORD_PREDEF; |
| 107 |
|
| 108 |
/** \brief Description of a control entry predefines */ |
| 109 |
typedef struct { |
| 110 |
INT used; /**< Used this entry */ |
| 111 |
const char *name; /**< Name string */ |
| 112 |
INT control_word; /**< Index of corresp. controlword */ |
| 113 |
INT control_entry_id; /**< Index in control_entries */ |
| 114 |
INT offset_in_word; /**< Shift in control word */ |
| 115 |
INT length; /**< Number of bits used */ |
| 116 |
INT objt_used; /**< Bitwise object ID */ |
| 117 |
} CONTROL_ENTRY_PREDEF; |
| 118 |
|
| 119 |
/****************************************************************************/ |
| 120 |
/* */ |
| 121 |
/* definition of exported global variables */ |
| 122 |
/* */ |
| 123 |
/****************************************************************************/ |
| 124 |
|
| 125 |
CONTROL_ENTRY NS_DIM_PREFIX control_entries[MAX_CONTROL_ENTRIES]; |
| 126 |
|
| 127 |
/****************************************************************************/ |
| 128 |
/* */ |
| 129 |
/* definition of variables global to this source file only (static!) */ |
| 130 |
/* */ |
| 131 |
/****************************************************************************/ |
| 132 |
|
| 133 |
// The order of the entries here has to match the GM_CW enumeration in gm.h |
| 134 |
|
| 135 |
constexpr INT MAX_CONTROL_WORDS = GM_N_CW; |
| 136 |
static CONTROL_WORD control_words[MAX_CONTROL_WORDS] = { |
| 137 |
{VECTOR_OFFSET, CW_VEOBJ, 0b0}, |
| 138 |
{VERTEX_OFFSET, CW_VXOBJS, 0b0}, |
| 139 |
{NODE_OFFSET, CW_NDOBJ, 0b0}, |
| 140 |
{LINK_OFFSET, CW_EDOBJ, 0b0}, |
| 141 |
{EDGE_OFFSET, CW_EDOBJ, 0b0}, |
| 142 |
{ELEMENT_OFFSET, CW_ELOBJS, 0b0}, |
| 143 |
{FLAG_OFFSET, CW_ELOBJS, 0b0}, |
| 144 |
{PROPERTY_OFFSET, CW_ELOBJS, 0b0}, |
| 145 |
{GRID_OFFSET, CW_GROBJ, 0b0}, |
| 146 |
{GRID_STATUS_OFFSET, CW_GROBJ, 0b0}, |
| 147 |
{MULTIGRID_STATUS_OFFSET, CW_MGOBJ, 0b0} |
| 148 |
}; |
| 149 |
|
| 150 |
static CONTROL_ENTRY_PREDEF ce_predefines[MAX_CONTROL_ENTRIES] = { |
| 151 |
CE_INIT(CE_LOCKED, VECTOR_, VOTYPE_, CW_VEOBJ), |
| 152 |
CE_INIT(CE_LOCKED, VECTOR_, VCOUNT_, CW_VEOBJ), |
| 153 |
CE_INIT(CE_LOCKED, VECTOR_, VECTORSIDE_, CW_VEOBJ), |
| 154 |
CE_INIT(CE_LOCKED, VECTOR_, VCLASS_, CW_VEOBJ), |
| 155 |
CE_INIT(CE_LOCKED, VECTOR_, VDATATYPE_, CW_VEOBJ), |
| 156 |
CE_INIT(CE_LOCKED, VECTOR_, VNCLASS_, CW_VEOBJ), |
| 157 |
CE_INIT(CE_LOCKED, VECTOR_, VNEW_, CW_VEOBJ), |
| 158 |
CE_INIT(CE_LOCKED, VECTOR_, VCCUT_, CW_VEOBJ), |
| 159 |
CE_INIT(CE_LOCKED, VECTOR_, FINE_GRID_DOF_, CW_VEOBJ), |
| 160 |
|
| 161 |
CE_INIT(CE_LOCKED, GENERAL_, OBJ_, (CW_GEOMOBJS | CW_VEOBJ)), |
| 162 |
CE_INIT(CE_LOCKED, GENERAL_, USED_, (CW_GEOMOBJS | CW_VEOBJ)), |
| 163 |
CE_INIT(CE_LOCKED, GENERAL_, THEFLAG_, (CW_GEOMOBJS | CW_VEOBJ)), |
| 164 |
CE_INIT(CE_LOCKED, GENERAL_, LEVEL_, CW_GEOMOBJS), |
| 165 |
|
| 166 |
CE_INIT(CE_LOCKED, VERTEX_, MOVE_, CW_VXOBJS), |
| 167 |
CE_INIT(CE_LOCKED, VERTEX_, MOVED_, CW_VXOBJS), |
| 168 |
CE_INIT(CE_LOCKED, VERTEX_, ONEDGE_, CW_VXOBJS), |
| 169 |
CE_INIT(CE_LOCKED, VERTEX_, ONSIDE_, CW_VXOBJS), |
| 170 |
CE_INIT(CE_LOCKED, VERTEX_, ONNBSIDE_, CW_VXOBJS), |
| 171 |
CE_INIT(CE_LOCKED, VERTEX_, NOOFNODE_, CW_VXOBJS), |
| 172 |
|
| 173 |
CE_INIT(CE_LOCKED, NODE_, NSUBDOM_, CW_NDOBJ), |
| 174 |
CE_INIT(CE_LOCKED, NODE_, NPROP_, CW_NDOBJ), |
| 175 |
CE_INIT(CE_LOCKED, NODE_, NCLASS_, CW_NDOBJ), |
| 176 |
CE_INIT(CE_LOCKED, NODE_, NNCLASS_, CW_NDOBJ), |
| 177 |
CE_INIT(CE_LOCKED, NODE_, MODIFIED_, (CW_NDOBJ | CW_GROBJ)), |
| 178 |
CE_INIT(CE_LOCKED, NODE_, NTYPE_, CW_NDOBJ), |
| 179 |
|
| 180 |
CE_INIT(CE_USED, LINK_, LOFFSET_, CW_EDOBJ), |
| 181 |
|
| 182 |
CE_INIT(CE_USED, EDGE_, AUXEDGE_, CW_EDOBJ), |
| 183 |
CE_INIT(CE_USED, EDGE_, PATTERN_, CW_EDOBJ), |
| 184 |
CE_INIT(CE_USED, EDGE_, ADDPATTERN_, CW_EDOBJ), |
| 185 |
CE_INIT(CE_USED, EDGE_, EDGENEW_, CW_EDOBJ), |
| 186 |
CE_INIT(CE_USED, EDGE_, EDSUBDOM_, CW_EDOBJ), |
| 187 |
CE_INIT(CE_USED, EDGE_, NO_OF_ELEM_, CW_EDOBJ), |
| 188 |
|
| 189 |
CE_INIT(CE_USED, ELEMENT_, REFINE_, CW_ELOBJS), |
| 190 |
CE_INIT(CE_USED, ELEMENT_, ECLASS_, CW_ELOBJS), |
| 191 |
CE_INIT(CE_USED, ELEMENT_, NSONS_, CW_ELOBJS), |
| 192 |
CE_INIT(CE_USED, ELEMENT_, REFINECLASS_, CW_ELOBJS), |
| 193 |
CE_INIT(CE_USED, ELEMENT_, NEWEL_, CW_ELOBJS), |
| 194 |
CE_INIT(CE_USED, ELEMENT_, TAG_, CW_ELOBJS), |
| 195 |
|
| 196 |
CE_INIT(CE_USED, FLAG_, MARK_, CW_ELOBJS), |
| 197 |
CE_INIT(CE_USED, FLAG_, COARSEN_, CW_ELOBJS), |
| 198 |
CE_INIT(CE_USED, FLAG_, DECOUPLED_, CW_ELOBJS), |
| 199 |
CE_INIT(CE_USED, FLAG_, UPDATE_GREEN_, CW_ELOBJS), |
| 200 |
CE_INIT(CE_USED, FLAG_, SIDEPATTERN_, CW_ELOBJS), |
| 201 |
CE_INIT(CE_USED, FLAG_, MARKCLASS_, CW_ELOBJS), |
| 202 |
|
| 203 |
CE_INIT(CE_USED, PROPERTY_, SUBDOMAIN_, CW_ELOBJS), |
| 204 |
CE_INIT(CE_USED, PROPERTY_, NODEORD_, CW_ELOBJS), |
| 205 |
CE_INIT(CE_USED, PROPERTY_, PROP_, CW_ELOBJS), |
| 206 |
|
| 207 |
#ifdef ModelP |
| 208 |
CE_INIT(CE_USED, VECTOR_, XFERVECTOR_, CW_VEOBJ), |
| 209 |
#endif /* ModelP */ |
| 210 |
}; |
| 211 |
|
| 212 |
/****************************************************************************/ |
| 213 |
/** \brief Initialize control word entries |
| 214 |
|
| 215 |
This function initializes the predefined control word entries. Predefined |
| 216 |
entries are not checked for overlap. |
| 217 |
|
| 218 |
* @return <ul> |
| 219 |
* <li> GM_OK if ok </li> |
| 220 |
* <li> GM_ERROR if error occurred </li> |
| 221 |
* </ul> |
| 222 |
*/ |
| 223 |
/****************************************************************************/ |
| 224 |
|
| 225 |
static INT InitPredefinedControlEntries (void) |
| 226 |
{ |
| 227 |
CONTROL_ENTRY *ce,*test_ce; |
| 228 |
CONTROL_WORD *cw; |
| 229 |
CONTROL_ENTRY_PREDEF *pce,*test_pce; |
| 230 |
INT i,j,k,mask,error,nused; |
| 231 |
|
| 232 |
/* clear everything */ |
| 233 |
memset(control_entries,0,MAX_CONTROL_ENTRIES*sizeof(CONTROL_ENTRY)); |
| 234 |
|
| 235 |
error = 0; |
| 236 |
nused = 0; |
| 237 |
for (i=0; i<MAX_CONTROL_ENTRIES; i++) |
| 238 |
if (ce_predefines[i].used) |
| 239 |
{ |
| 240 |
pce = ce_predefines+i; |
| 241 |
ASSERT(pce->control_entry_id<MAX_CONTROL_ENTRIES); |
| 242 |
|
| 243 |
nused++; |
| 244 |
ce = control_entries+pce->control_entry_id; |
| 245 |
if (ce->used) |
| 246 |
{ |
| 247 |
printf("redefinition of control entry '%s'\n",pce->name); |
| 248 |
return(__LINE__); |
| 249 |
} |
| 250 |
cw = control_words+pce->control_word; |
| 251 |
ce->used = pce->used; |
| 252 |
ce->name = pce->name; |
| 253 |
ce->control_word = pce->control_word; |
| 254 |
ce->offset_in_word = pce->offset_in_word; |
| 255 |
ce->length = pce->length; |
| 256 |
ce->objt_used = pce->objt_used; |
| 257 |
ce->offset_in_object = cw->offset_in_object; |
| 258 |
ce->mask = (POW2(ce->length)-1)<<ce->offset_in_word; |
| 259 |
ce->xor_mask = ~ce->mask; |
| 260 |
|
| 261 |
PRINTDEBUG(gm,1,("ceID %d used %d name %s cw %d\n", |
| 262 |
i,ce->used,ce->name,ce->control_word)); |
| 263 |
|
| 264 |
ASSERT(ce->objt_used & cw->objt_used); /* ce and cw have! common objects */ |
| 265 |
|
| 266 |
/* set mask in all cws that use some of the ces objects and have the same offset than cw */ |
| 267 |
const UINT offset = ce->offset_in_object; |
| 268 |
mask = ce->mask; |
| 269 |
for (k=0; k<MAX_CONTROL_WORDS; k++) |
| 270 |
{ |
| 271 |
cw = control_words+k; |
| 272 |
|
| 273 |
if (!(ce->objt_used & cw->objt_used)) |
| 274 |
continue; |
| 275 |
if (cw->offset_in_object!=offset) |
| 276 |
continue; |
| 277 |
|
| 278 |
/* do other control entries overlap? */ |
| 279 |
if (cw->used_mask & mask) |
| 280 |
{ |
| 281 |
IFDEBUG(gm,1) |
| 282 |
printf("predef ctrl entry '%s' has overlapping bits with previous ctrl entries:\n",pce->name); |
| 283 |
for (j=0; j<i; j++) |
| 284 |
{ |
| 285 |
test_pce = ce_predefines+j; |
| 286 |
test_ce = control_entries+test_pce->control_entry_id; |
| 287 |
if (test_ce->objt_used & ce->objt_used) |
| 288 |
if (test_ce->offset_in_object==offset) |
| 289 |
if (test_ce->mask & mask) |
| 290 |
printf(" '%s'",test_pce->name); |
| 291 |
|
| 292 |
} |
| 293 |
printf("\n"); |
| 294 |
ENDDEBUG |
| 295 |
error++; |
| 296 |
} |
| 297 |
cw->used_mask |= mask; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/* TODO: enable next lines for error control */ |
| 302 |
IFDEBUG(gm,1) |
| 303 |
if (error) |
| 304 |
return (__LINE__); |
| 305 |
ENDDEBUG |
| 306 |
|
| 307 |
if (nused!=REFINE_N_CE) |
| 308 |
{ |
| 309 |
printf("InitPredefinedControlEntries: nused=%d != REFINE_N_CE=%d\n",nused,REFINE_N_CE); |
| 310 |
assert(false); |
| 311 |
} |
| 312 |
|
| 313 |
return (GM_OK); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
/****************************************************************************/ |
| 318 |
/** \brief Function to replace CW_READ macro and does extended error checks |
| 319 |
|
| 320 |
* @param obj - object pointer |
| 321 |
* @param ceID - control entry ID |
| 322 |
|
| 323 |
This function is to replace the CW_READ and CW_READ_STATIC macros of gm.h and does extended |
| 324 |
error checks:~ |
| 325 |
<ul> |
| 326 |
<li> obj != NULL </li> |
| 327 |
<li> HEAPFAULT </li> |
| 328 |
<li> ceID in valid range </li> |
| 329 |
<li> control entry used </li> |
| 330 |
<li> the object type is checked (with the natural exception of the first SETOBJ access) </li> |
| 331 |
</ul> |
| 332 |
Additionally the read accesses to a control entry are counted. |
| 333 |
|
| 334 |
CAUTION: |
| 335 |
set #define _DEBUG_CW_ to replace CW_READ by ReadCW but be aware of the |
| 336 |
slowing down of the program in this case (no large problems!). |
| 337 |
|
| 338 |
@return |
| 339 |
Number read from the control entry of the object |
| 340 |
*/ |
| 341 |
/****************************************************************************/ |
| 342 |
|
| 343 |
UINT NS_DIM_PREFIX ReadCW (const void *obj, INT ceID) |
| 344 |
{ |
| 345 |
UINT off_in_obj,mask,i,off_in_wrd,cw,cw_objt; |
| 346 |
|
| 347 |
ASSERT(obj!=NULL); |
| 348 |
|
| 349 |
if ((ceID<0) || (ceID>=MAX_CONTROL_ENTRIES)) |
| 350 |
{ |
| 351 |
printf("ReadCW: ceID=%d out of range\n",ceID); |
| 352 |
assert(false); |
| 353 |
} |
| 354 |
|
| 355 |
const CONTROL_ENTRY *ce = control_entries + ceID; |
| 356 |
|
| 357 |
if (!ce->used) |
| 358 |
{ |
| 359 |
printf("ReadCW: ceID=%d unused\n",ceID); |
| 360 |
assert(false); |
| 361 |
} |
| 362 |
|
| 363 |
cw_objt = BITWISE_TYPE(OBJT(obj)); |
| 364 |
if (!(cw_objt & ce->objt_used)) |
| 365 |
{ |
| 366 |
if (ce->name!=NULL) |
| 367 |
printf("ReadCW: invalid objt %d for ce %s\n",OBJT(obj),ce->name); |
| 368 |
else |
| 369 |
printf("ReadCW: invalid objt %d for ce %d\n",OBJT(obj),ceID); |
| 370 |
assert(false); |
| 371 |
} |
| 372 |
|
| 373 |
off_in_wrd = ce->offset_in_word; |
| 374 |
off_in_obj = ce->offset_in_object; |
| 375 |
mask = ce->mask; |
| 376 |
cw = ((UINT *)(obj))[off_in_obj]; |
| 377 |
i = cw & mask; |
| 378 |
i = i>>off_in_wrd; |
| 379 |
|
| 380 |
return (i); |
| 381 |
} |
| 382 |
|
| 383 |
/****************************************************************************/ |
| 384 |
/** \brief Function to replace CW_WRITE macro and does extended error checks |
| 385 |
|
| 386 |
* @param obj - object pointer |
| 387 |
* @param ceID - control entry ID |
| 388 |
* @param n - number to write to the objects control entry |
| 389 |
|
| 390 |
This function is to replace the CW_WRITE and CW_WRITE_STATIC macros of gm.h and does extended |
| 391 |
error checks:~ |
| 392 |
<ul> |
| 393 |
<li> obj != NULL </li> |
| 394 |
<li> HEAPFAULT </li> |
| 395 |
<li> ceID in valid range </li> |
| 396 |
<li> control entry used </li> |
| 397 |
<li> the object type is checked </li> |
| 398 |
<li> n small enough for length of control entry </li> |
| 399 |
</ul> |
| 400 |
Additionally the write accesses to a control entry are counted. |
| 401 |
|
| 402 |
CAUTION: |
| 403 |
set #define _DEBUG_CW_ to replace CW_WRITE by WriteCW but be aware of the |
| 404 |
slowing down of the program in this case (no large problems!). |
| 405 |
|
| 406 |
@return |
| 407 |
Number read from the control entry of the object |
| 408 |
*/ |
| 409 |
/****************************************************************************/ |
| 410 |
|
| 411 |
void NS_DIM_PREFIX WriteCW (void *obj, INT ceID, INT n) |
| 412 |
{ |
| 413 |
UINT off_in_obj,mask,i,j,off_in_wrd,xmsk; |
| 414 |
UINT *pcw; |
| 415 |
|
| 416 |
ASSERT(obj!=NULL); |
| 417 |
|
| 418 |
if ((ceID<0) || (ceID>=MAX_CONTROL_ENTRIES)) |
| 419 |
{ |
| 420 |
printf("WriteCW: ceID=%d out of range\n",ceID); |
| 421 |
assert(false); |
| 422 |
} |
| 423 |
|
| 424 |
const CONTROL_ENTRY *ce = control_entries + ceID; |
| 425 |
|
| 426 |
if (!ce->used) |
| 427 |
{ |
| 428 |
printf("WriteCW: ceID=%d unused\n",ceID); |
| 429 |
assert(false); |
| 430 |
} |
| 431 |
|
| 432 |
const UINT cw_objt = BITWISE_TYPE(OBJT(obj)); |
| 433 |
|
| 434 |
/* special: SETOBJT cannot be checked since at this point the OBJT is unknown of course */ |
| 435 |
if (cw_objt==BITWISE_TYPE(0)) |
| 436 |
{ |
| 437 |
if (ceID!=OBJ_CE) |
| 438 |
if (cw_objt != ce->objt_used) |
| 439 |
{ |
| 440 |
if (ce->name!=NULL) |
| 441 |
printf("WriteCW: objt 0 but %s rather than expected SETOBJT access\n",ce->name); |
| 442 |
else |
| 443 |
printf("WriteCW: objt 0 but %d rather than expected SETOBJT access\n",ceID); |
| 444 |
assert(false); |
| 445 |
} |
| 446 |
} |
| 447 |
else if (!(cw_objt & ce->objt_used)) |
| 448 |
{ |
| 449 |
if (ce->name!=NULL) |
| 450 |
printf("WriteCW: invalid objt %d for ce %s\n",OBJT(obj),ce->name); |
| 451 |
else |
| 452 |
printf("WriteCW: invalid objt %d for ce %d\n",OBJT(obj),ceID); |
| 453 |
assert(false); |
| 454 |
} |
| 455 |
|
| 456 |
off_in_wrd = ce->offset_in_word; |
| 457 |
off_in_obj = ce->offset_in_object; |
| 458 |
mask = ce->mask; |
| 459 |
xmsk = ce->xor_mask; |
| 460 |
pcw = ((UINT *)(obj)) + off_in_obj; |
| 461 |
i = (*pcw) & xmsk; |
| 462 |
j = n<<off_in_wrd; |
| 463 |
|
| 464 |
if (j>mask) |
| 465 |
{ |
| 466 |
if (ce->name!=NULL) |
| 467 |
printf("WriteCW: value=%d exceeds max=%d for %s\n", |
| 468 |
n,POW2(ce->length)-1,ce->name); |
| 469 |
else |
| 470 |
printf("WriteCW: value=%d exceeds max=%d for %d\n", |
| 471 |
n,POW2(ce->length)-1,ceID); |
| 472 |
assert(false); |
| 473 |
} |
| 474 |
|
| 475 |
j = j & mask; |
| 476 |
|
| 477 |
*pcw = i | j; |
| 478 |
} |
| 479 |
|
| 480 |
/****************************************************************************/ |
| 481 |
/** \brief Allocates space in object control words |
| 482 |
|
| 483 |
* @param cw_id - id of a control word |
| 484 |
* @param length - number of bits to allocate |
| 485 |
* @param ce_id - returns identifier of control entry descriptor |
| 486 |
|
| 487 |
This function allocates 'length' consecutive bits in the control word of an |
| 488 |
object identified through the `control word id` 'cw_id'. |
| 489 |
It returns '0' and a valid id in 'ce_id' if space was available. |
| 490 |
The 'ce_id' can be used to read and write the requested bits with the |
| 491 |
'CW_READ' and 'CW_WRITE' macros as in the following example. |
| 492 |
|
| 493 |
The control word ids of all UG objects are defined in 'gm.h' and usually |
| 494 |
have the name `<object>`'_CW', except e.g. the 'ELEMENT' which has three |
| 495 |
words that are used bitwise. |
| 496 |
|
| 497 |
EXAMPLE: |
| 498 |
|
| 499 |
The following code fragment allocates 'NORDER_LEN' bits in the 'flag' word |
| 500 |
of the 'ELEMENT' structure. |
| 501 |
|
| 502 |
\verbatim |
| 503 |
INT ce_NORDER; |
| 504 |
|
| 505 |
if (AllocateControlEntry(FLAG_CW,NORDER_LEN,&ce_NORDER) != GM_OK) return (1); |
| 506 |
\endverbatim |
| 507 |
|
| 508 |
The following macros then read and write the requested bits |
| 509 |
|
| 510 |
\verbatim |
| 511 |
*#define NORDER(p) CW_READ(p,ce_NORDER) |
| 512 |
*#define SETNORDER(p,n) CW_WRITE(p,ce_NORDER,n) |
| 513 |
\endverbatim |
| 514 |
|
| 515 |
|
| 516 |
@return |
| 517 |
</ul> |
| 518 |
<li> GM_OK if ok </li> |
| 519 |
<li> GM_ERROR if error occurred. </li> |
| 520 |
</ul> |
| 521 |
*/ |
| 522 |
/****************************************************************************/ |
| 523 |
|
| 524 |
INT NS_DIM_PREFIX AllocateControlEntry (INT cw_id, INT length, INT *ce_id) |
| 525 |
{ |
| 526 |
INT free, i, offset; |
| 527 |
CONTROL_ENTRY *ce; |
| 528 |
CONTROL_WORD *cw; |
| 529 |
UINT mask; |
| 530 |
|
| 531 |
/* check input */ |
| 532 |
if ((length<0)||(length>=32)) return(GM_ERROR); |
| 533 |
if ((cw_id<0)||(cw_id>=MAX_CONTROL_WORDS)) return(GM_ERROR); |
| 534 |
|
| 535 |
/* it is sufficient to check only the control entries control word |
| 536 |
multiple object types are only allowed for predefines */ |
| 537 |
cw = control_words+cw_id; |
| 538 |
|
| 539 |
/* find unused entry */ |
| 540 |
for (i=0; i<MAX_CONTROL_ENTRIES; i++) |
| 541 |
if (!control_entries[i].used) break; |
| 542 |
if (i==MAX_CONTROL_ENTRIES) return(GM_ERROR); |
| 543 |
free = i; |
| 544 |
ce = control_entries+free; |
| 545 |
|
| 546 |
/* lets see if enough consecutive bits are available */ |
| 547 |
mask = POW2(length)-1; |
| 548 |
for (i=0; i<=32-length; i++) |
| 549 |
{ |
| 550 |
if ((mask&cw->used_mask)==0) break; |
| 551 |
mask <<= 1; |
| 552 |
} |
| 553 |
if (i>32-length) return(GM_ERROR); |
| 554 |
offset = i; |
| 555 |
|
| 556 |
/* fill new entry */ |
| 557 |
*ce_id = free; |
| 558 |
ce->used = 1; |
| 559 |
ce->control_word = cw_id; |
| 560 |
ce->offset_in_object = cw->offset_in_object; |
| 561 |
ce->offset_in_word = offset; |
| 562 |
ce->length = length; |
| 563 |
ce->mask = mask; |
| 564 |
ce->xor_mask = ~mask; |
| 565 |
ce->name = NULL; |
| 566 |
ce->objt_used = cw->objt_used; |
| 567 |
|
| 568 |
/* remember used bits */ |
| 569 |
cw->used_mask |= mask; |
| 570 |
|
| 571 |
/* ok, exit */ |
| 572 |
return(GM_OK); |
| 573 |
} |
| 574 |
|
| 575 |
/****************************************************************************/ |
| 576 |
/** \brief Frees space in object control words |
| 577 |
|
| 578 |
* @param ce_id - control entry descriptor to free |
| 579 |
|
| 580 |
This function frees space in object control words that has been allocated |
| 581 |
with 'AllocateControlEntry'. |
| 582 |
|
| 583 |
@return <ul> |
| 584 |
<li> GM_OK if ok </li> |
| 585 |
<li> GM_ERROR if error occurred. </li> |
| 586 |
</ul> |
| 587 |
*/ |
| 588 |
/****************************************************************************/ |
| 589 |
|
| 590 |
INT NS_DIM_PREFIX FreeControlEntry (INT ce_id) |
| 591 |
{ |
| 592 |
CONTROL_ENTRY *ce; |
| 593 |
CONTROL_WORD *cw; |
| 594 |
|
| 595 |
/* check parameter */ |
| 596 |
if ((ce_id<0)||(ce_id>=MAX_CONTROL_ENTRIES)) return(GM_ERROR); |
| 597 |
ce = control_entries+ce_id; |
| 598 |
cw = control_words+ce->control_word; |
| 599 |
|
| 600 |
/* check if locked */ |
| 601 |
if (ce->used == 2) |
| 602 |
return(GM_ERROR); |
| 603 |
|
| 604 |
/* free used bits */ |
| 605 |
cw->used_mask &= ce->xor_mask; |
| 606 |
|
| 607 |
/* free control entry */ |
| 608 |
ce->used = 0; |
| 609 |
|
| 610 |
/* ok, exit */ |
| 611 |
return(GM_OK); |
| 612 |
} |
| 613 |
|
| 614 |
/****************************************************************************/ |
| 615 |
/** \brief Init cw.c file |
| 616 |
|
| 617 |
This function initializes the control word manager. |
| 618 |
|
| 619 |
@return <ul> |
| 620 |
<li> GM_OK if ok </li> |
| 621 |
<li> > 0 line in which error occurred. </li> |
| 622 |
</ul> |
| 623 |
*/ |
| 624 |
/****************************************************************************/ |
| 625 |
|
| 626 |
INT NS_DIM_PREFIX InitCW (void) |
| 627 |
{ |
| 628 |
if (InitPredefinedControlEntries()) |
| 629 |
return (__LINE__); |
| 630 |
|
| 631 |
return (GM_OK); |
| 632 |
} |