Thermodynamic stability and negative thermal expansion of hexagonal and cubic ices