Thank you @FortranFan for the detailed solution and explanation!
Does this work if on the user’s platform real_kind has only two elements rather than three? Or does the standard ensure that size(real_kind) == 3?
A minor side note is that real_kinds and integer_kinds are F2008 but I hope to have an implementation compatible with F2003.