Move the cast functions into libbase

fixes #7807