First-principles calculation of the instability leading to giant inverse magnetocaloric effects