Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • SWIFTsim SWIFTsim
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 57
    • Issues 57
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 21
    • Merge requests 21
  • Deployments
    • Deployments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • SWIFT
  • SWIFTsimSWIFTsim
  • Issues
  • #606
Closed
Open
Issue created Aug 06, 2019 by Matthieu Schaller@matthieuOwner

New RNG triggers the undefined behaviour sanitizer

When running the code with GCC's undefined behaviour sanitizer (-fsanitize=undefined), we get a series of error messages coming from the RNG. Specifically, we get:

../src/random.h:110:50: runtime error: left shift of 2053 by 20 places cannot be represented in type 'int'

where the 2053 will be different for every call.

This corresponds to this line in the code:

INLINE static double inl_erand48(uint16_t xsubi[3]) {
  union ieee754_double temp;

  /* Compute next state.  */
  inl_drand48_iterate(xsubi);

  /* Construct a positive double with the 48 random bits distributed over                                                                                                                                                                                                                                                     
     its fractional part so the resulting FP number is [0.0,1.0).  */
  temp.ieee.negative = 0;
  temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
  temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
  temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);    // < ----- HERE ----

  /* Please note the lower 4 bits of mantissa1 are always 0.  */
  return temp.d - 1.0;
}

We indeed bitshift by 20 something that is only 16 bits wide. However, I think we overflow on purpose here.

Silencing the warning somehow would be good but that shouldn't be at the cost of not overflowing since this is part of the RNG strategy. Telling the compiler that we are adults and know what we are doing would be the best choice.

Edited Aug 06, 2019 by Matthieu Schaller
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking