INDIAN WELLS, Calif. -- Rafael Nadal ended the run of Latvian qualifier Ernests Gulbis with a 4-6, 6-4, 7-5 victory in the fourth round at the BNP Paribas Open on Wednesday night.

Nadal advanced to the quarterfinals against defending champion Roger Federer, who survived a three-set marathon of his own, beating 18th-seeded Stanislas Wawrinka 6-3, 6-7 (4), 7-5.

No. 2 seed Maria Sharapova defeated sixth-seeded Sara Errani 7-6 (6), 6-2 to reach the women's semifinals without dropping a set, stopping a streak of four straight matches that went three sets on the main stadium court.

Top-ranked Novak Djokovic didn't take the court against American Sam Querrey until after midnight PDT.

Gulbis was on a 13-match winning streak, having made his way through the qualifying rounds at Delray Beach, Fla., where he won the title, and at Indian Wells, where he won three main-draw matches, including two over seeded players.

He was playing his 14th match in 19 days, and has yet to beat Nadal in five tries.

"I think that I played better tennis. I was more aggressive," Gulbis said. "I went for my shots much more than he. But he did really incredibly well, as he always does, on important points."

Gulbis held serve to lead 5-4 in the third. He led 30-15 on Nadal's serve, but lost the next five points to let Nadal tie the set.

Nadal earned the only break of the third to go up 6-5, then closed out the win on his third match point. He leapt in the air, tore off his headband and pumped his left arm, yelling, "Yeah!"

"With all the problems, I was able to keep being focused and keep winning," he said. "I really appreciate every moment and every victory."

The fifth-seeded Spaniard opened his bid for a third Indian Wells title with a two-set victory over American Ryan Harrison, Nadal's first hard-court match in nearly a year.

He returned to the ATP Tour a month ago, winning two of three tournaments on clay after missing seven months because of a left knee injury. Nadal, shaken by an earthquake that rattled Indian Wells on Monday, had a walkover in his third-round match when Leonardo Mayer withdrew with a bad back.

"I said before the tournament my knee, some days good, some days not that good. Today so-so," he said. "But I fought. I fought every ball."

Wawrinka's consecutive forehand errors in the gathering dusk of an unusually hot day in the California desert closed out his 10th consecutive loss to Federer on hard courts. The temperature reached 96 degrees -- nearly 20 degrees above normal -- during the day.

Federer, a four-time champion at Indian Wells, is going for his first ATP Tour title since last August. He'll have to get by Nadal in the earliest meeting between the two stars since the first time they played each other in 2004.

"In the past, this match used to be a final. Now it's a quarterfinal, so obviously it's a bit of bad luck of the draw for both of us," Federer said, noting that between his bothersome back and Nadal's left knee "we are both a bit suspect."

Nadal said he doesn't think his level of play is yet up to what he's normally capable of against Federer.

"This match arrives too early for me to go to the match with the feeling that I can play equal conditions," he said. "Two weeks ago, I didn't know if I would be able to be here. Being in quarterfinals is a fantastic result for me, and we'll see."

Federer, ranked second, improved to 13-1 against the 18th-seeded Wawrinka, who was broken twice in the final set played in front of tennis great Rod Laver.

"I don't know what gets me through. Maybe it's the experience or maybe a bit more calm in those moments," Federer said. "Today I think I was a little lucky to come through it."

Federer was broken once in the third set after telling the chair umpire he didn't have enough time to challenge the call on his first serve. He lost that argument and the game, but broke Wawrinka right back to tie it 3-all. From there, both players held until Federer broke Wawrinka at love in the last game.

Federer tweaked his back in his fourth-round match and was glad to have Tuesday off to recover.

"I played three sets over two hours, so I'm happy at what level I can compete," he said. "I'm hopeful that it's going to feel a bit better again tomorrow, another step forward."

Federer has won 29 of 34 sets in their 15 career matches, with Wawrinka's lone win coming four years ago on clay in Monte Carlo.

"I'm not the only guy who beat him only once. A lot of people never beat him. He's Roger. He's amazing player. He always find a way to win the match," Wawrinka said. "I was close, but not enough, and that's why I need to still go to practice and practice again."

Maria Kirilenko got by No. 5 seed Petra Kvitova 4-6, 6-4, 6-3 in the searing heat to reach the women's semifinals for the first time. Kirilenko improved to 6-0 in three-set matches this year.

On the men's side, No. 3 Andy Murray beat Carlos Berlocq 7-6 (4), 6-4 to advance to a quarterfinal against No. 7 Juan Martin del Potro, who routed No. 19 Tommy Haas 6-1, 6-2.

Berlocq complained to the chair umpire that Murray was taking too long between points, prompting Murray to protest the volume of Berlocq's grunting.

"It was extremely, extremely loud, more than what I have experienced from any other player on the tour," Murray said. "If it's going to be suggested that I am using gamesmanship by taking too long, then you can't be making noises like that on the court. It's annoying."

Berlocq said he didn't realize the sound would bother Murray.

"Well, yeah, but that's what all of the real grunters say," the Scotsman retorted.

No. 6 Tomas Berdych beat No. 10 Richard Gasquet 6-1, 7-5; No. 8 seed Jo-Wilfried Tsonga outlasted No. 17 Milos Raonic 4-6, 7-5, 6-4; and Kevin Anderson defeated No. 13 Giles Simon 6-3, 1-6, 6-4.