How does smem calculate RSS, USS and PSS?

Right now, I am trying to write a program, which should determine the memory usage of single process similar to smem, but not as fancy as smem. But I was wondering how smem determines the values US...