Wimbledon champion Federer, who celebrated his 36th birthday on Tuesday, prevailed 4-6 6-4 6-2 in just under two hours in Montreal.
Ferrer came out all guns blazing and after racing into a 4-1 lead took the first set 6-4 against world number three Federer, who had not dropped a set since his shock defeat to Tommy Haas at the Mercedes Cup in June.
Montreal Masters
Federer through to Rogers Cup final
12/08/2017 AT 21:23
But Federer broke Ferrer in the first game of the second and although he lost his serve in the fifth game, he broke back immediately and served out to level the match.
Federer, a three-time previous winner of the tournament, pounced on the Ferrer serve in the opening game of the decider and broke the former world number three a second time to take a 5-2 lead, before sealing a last-eight clash against another Spaniard, Roberto Bautista Agut.

Roger Federer lors de son 2e tour à Montréal

Image credit: Getty Images

Bautista Agut fought back from a set down and out-lasted Frenchman Gael Monfils 4-6 7-6 (7/5) 7-6 (7/2) in just short of three hours.
Argentina's Diego Schwartzman recovered after failing to take a game in the opening set to beat USA's Jared Donaldson 0-6 7-5 7-5.
World number 36 Schwartzman will play Holland's Robin Haase who overcame Grigor Dimitrov 7-6 (7/3) 4-6 6-1.

Results from the Montreal Masters Men's Singles Round 3 matches on Thursday

  • 2-Roger Federer (Switzerland) beat David Ferrer (Spain) 4-6 6-4 6-2
  • Robin Haase (Netherlands) beat 7-Grigor Dimitrov (Bulgaria) 7-6(3) 4-6 6-1
  • 12-Roberto Bautista (Spain) beat Gael Monfils (France) 4-6 7-6(5) 7-6(2)
  • Diego Schwartzman (Argentina) beat Jared Donaldson (U.S.) 0-6 7-5 7-5
  • Kevin Anderson (South Africa) beat Sam Querrey (U.S.) 6-4 6-1
Montreal Masters
Federer cruises to semi-finals in Montreal
11/08/2017 AT 21:47
Montreal Masters
Canadian teenager Shapovalov stuns Nadal in Montreal
11/08/2017 AT 15:31