Roger Federer has defeated Ernests Gulbis 3-6, 6-1, 6-4 to reach the semifinals of Madrid, avenging his loss to the Latvian two weeks ago in the second round of Rome.

Federer was down a set and a break at 3-6, 0-1 but then ran off nine of the next ten games to gain the upper hand.

"I think it’s one of the toughest things in tennis if you lose against a player and you have to play against him in the next couple of weeks," said Federer. "That's why I'm particularly happy to come up with a proper gameplan today.

The pair have played three times this season, with all three encounters going three sets.