12 November 2008

Handcrafted memory layout: a case study in magic number mayhem

Observe, in the code snippet below, that no fewer than 27 subtly different aliases have been created for the same chunk of memory...

Semantics/Syntax: The @ sign instructs the compiler to have the variables named varsc and vars share storage much like C's union construct. The symbolic keyword acts like a macro definition.

    dynamic complex(64) varsc[(nx+2)/2,ny,37] 
          @ real(64)    vars[     nx+2,ny,37]   
    
    symbolic mic[i,j,l]        = varsc[i,j,l]
           , rhoc[i,j]         = varsc[i,j,4]
           , engyc[i,j]        = varsc[i,j,5]
           , drho_dxic[i,j,l]  = varsc[i,j,l+5]
           , dengy_dxic[i,j,l] = varsc[i,j,l+8]
           , dmi_dxic[i,j,l,m] = varsc[i,j,11+(l-1)*3+m]
           , rhsc[i,j,l,m]     = varsc[i,j,25+(l-1)*3+m]
           , rhs_rhoc[i,j]     = varsc[i,j,21]
      
    symbolic mi[i,j,l]         = vars[i,j,l]
           , rho[i,j]          = vars[i,j,4]
           , engy[i,j]         = vars[i,j,5]
           , drho_dxi[i,j,l]   = vars[i,j,l+5]
           , dengy_dxi[i,j,l]  = vars[i,j,l+8]
           , dmi_dxi[i,j,l,m]  = vars[i,j,11+(l-1)*3+m]
           , rhs_rho[i,j]      = vars[i,j,21]
           , dui_dxi[i,j,l,m]  = vars[i,j,11+(l-1)*3+m]
           , pres[i,j]         = vars[i,j,22]
           , tmpt[i,j]         = vars[i,j,23]
           , mu[i,j]           = vars[i,j,24]
           , tau[i,j,l,m]      = vars[i,j,25+(l-1)*3+m]
           , q1[i,j]           = vars[i,j,9]
           , q3[i,j]           = vars[i,j,11]
           , rhs[i,j,l,m]      = vars[i,j,25+(l-1)*3+m]
           , dmu_dx2[i,j]      = vars[i,j,6]
           , dtmpt_dx2[i,j]    = vars[i,j,7]
           , d2tmpt_dx22[i,j]  = vars[i,j,8]
           , d2ui_dx22[i,j,l]  = vars[i,j,8+l]

No comments:

Subscribe Subscribe to The Return of Agent Zlerich