 
 
 /*
- * Convert a given mass into a mass/charge ratio, and locate the appropriate
- * spectrum bin for the peak.
+ * Convert a given mass into a mass/charge ratio
+ * Locate the appropriate spectrum bin for a peak.
  */
-__device__ int
-binIonMZ(float mass, int charge)
-{
-    int bin = rintf((mass + massH * charge) / (charge * binWidthMono));
-#ifdef __DEVICE_EMULATION__
-    assert(bin >= 0 && bin < 2048);
-#endif
-    return bin;
-}
+__device__ float ionMZ(float m, float c) { return (m + massH * c) / c; }
+__device__ int   bin(float x) { return rintf(x / binWidthMono); }
 
 
 /*


        do
        {
            addIonsAB(b_mass, charge, spec);
            addIonsY (y_mass, charge, spec);
        }
        while (++charge < max_charge);
    }

 * corresponding to the neutral losses of H2O and NH3.
 */
__device__ void
addIonsAB(float mass, float charge, int *spec)
{
    // A
    atomicMax(&spec[bin(ionMZ(mass - massCO, charge))], 10);

    // B
    float m = ionMZ(mass, charge);
    int   b = bin(m);

    atomicMax(&spec[b],   50);
    atomicMax(&spec[b+1], 25);
    atomicMax(&spec[b-1], 25);
    atomicMax(&spec[bin(m - massH2O/charge)], 10);
    atomicMax(&spec[bin(m - massNH3/charge)], 10);
}


__device__ void
addIonsY(float mass, float charge, int *spec)
{
    float m = ionMZ(mass + massH2O, charge);
    int   b = bin(m);

    atomicMax(&spec[b],   50);
    atomicMax(&spec[b+1], 25);
    atomicMax(&spec[b-1], 25);
    atomicMax(&spec[bin(m - massNH3/charge)], 10);
}


