MADRID, DEC. 23 -- Prime Minister Felipe Gonzalez has broken off negotiations with the United States over the stationing of American F16 fighter-bombers in Spain and has told Washington it must withdraw the warplanes within 3 1/2 years, diplomatic sources disclosed today.

The sudden decision by Gonzalez to declare the departure of all 72 F16s -- three squadrons stationed at Torrejon air base -- a "nonnegotiable" issue and unilaterally to set a withdrawal deadline for them was conveyed to the American Embassy here on Dec. 10 and has been a closely held secret in both countries since then, according to these sources.

Spain's action -- the first important unilateral reduction of American forces ordered by a European ally since France closed American installations in 1966 and withdrew militarily from the North Atlantic Treaty Organization -- creates the potential for a bitter and divisive dispute that officials in both countries say they have been successful in avoiding until now.

U.S. officials in Washington are also concerned about the ripple effect the Spanish move could have on American efforts to arrive at new basing agreements in Greece and the Philippines and to resolve disputes with Turkey and Portugal over existing defense accords.

Loss of the three F16 squadrons, which make up the 401st Tactical Air Wing, is likely to mean the withdrawal of all of the approximately 4,500 American airmen stationed at Torrejon and closure of the base, which is just west of Madrid, American officials have indicated in the past.

The failure after 18 months of negotiations to reach an agreement on Torrejon leaves a question mark over the future of the other 8,000 U.S. servicemen stationed in Spain at a large naval base in Rota, air bases near Zaragoza and Seville and nine small communications facilities.

Gonzalez, who is carrying out a pledge made to his electorate in 1986 by ordering the departure of the 40lst wing, has repeatedly said that he does not want to disturb the functioning of the other American installations and will sign a new long-term agreement covering them after the current accord expires next May.

But some senior Pentagon officials are reported to have argued that the United States should pull out of Spain and launch a diplomatic campaign to punish Gonzalez if he goes through with what they see as an expulsion of the F16s.

In an interview published today by the Madrid magazine El Globo, Vernon Walters, the U.S. ambassador to the United Nations, was quoted as saying that "if there is no agreement on bases in Spain and we are told to leave, we will leave. If we are not wanted, we will go. . . . But we Americans will not forget . . . an unfriendly gesture toward us."

The chief American negotiator, Ambassador to Spain Reginald Bartholomew, declined through a spokesman to comment on the details contained in this article. The Spanish Foreign Ministry spokesman, Inocencio Arias, also said he could not comment.

U.S. officials have put the probable cost of relocating the aircraft elsewhere in Europe in the range of $500 million and up, and have predicted that it would be difficult to find a new home for them along NATO's southern rim. The F16s -- a crucial part of front-line U.S. striking power in Europe -- would deploy into Italy and Turkey in the event of war and stage nuclear strikes with weapons stored in those countries, according to informed sources.

Belgium, Portugal and Morocco are reported by diplomatic sources to have expressed interest in exploring with the United States taking some or all of the F16s, but Washington has refused to consider rebasing the aircraft while negotiations with Spain continued.

The decision by Gonzalez appeared to catch American negotiators off guard. They were still advancing compromise proposals that would have kept some of the F16s in Spain if accepted and were preparing for a new round of negotiations here set for Dec. 18.

But the December round was hastily postponed until an unspecified date in January after Foreign Minister Francisco Fernandez Ordonez personally conveyed to Bartholomew on Dec. 10 the decision made on the F16s by Gonzalez and his Cabinet. The United States would have three years after the agreement expires in May 1988 to withdraw the aircraft, Ordonez said.

The U.S. negotiator reportedly replied that the Spanish decision was unacceptable and that Washington would need time to review its overall position in the negotiations.

Gonzalez, the 45-year-old Socialist leader who came to power in 1982 and won easy reelection four years later, staged a successful campaign in 1986 to have the electorate endorse Spanish membership in NATO.

He pledged in return that he would significantly reduce the American military presence, which was brought to Spain in 1953 by an agreement between president Dwight D. Eisenhower and generalissimo Francisco Franco. The stationing of U.S. troops here is seen by many Spaniards as a symbol of the help the United States provided Franco in breaking out of the diplomatic isolation that he faced at the time.

Throughout the discussions over a new bases agreement, Gonzalez has maintained that the proximity of the Torrejon base to Madrid and the fact that the 401st wing's missions were executed far from Spanish territory made it fairly easy for the United States to withdraw the F16s.

The Spanish also offered to let the United States maintain important regional repair, training and intelligence functions at Torrejon after the fighter-bombers were withdrawn, according to informed sources.

Gonzalez's decision to set a three-year limit for the withdrawal is likely to be interpreted in Washington as having been politically motivated. He is expected to seek a third term in elections that would normally be held in 1990, and by setting the deadline for withdrawal a year from then, he will be able to enter the campaign asserting he has carried out his 1986 pledge.

His government has been hit in the past year by mounting labor strife and strong criticism of its economic austerity program from its most leftist activists, who accuse Gonzalez of abandoning socialism.

What finally triggered the decision is likely to become a point of sharp dispute as details filter out. Some Spanish officials are said to attribute it to irritation within the Cabinet over what were perceived as American subterfuges in the final stage of the negotiation.

The United States proposed in November that 24 of the F16s be withdrawn over three years and that the remainder stay at Torrejon for up to 10 years, according to an authoritative account.

But Spanish officials, acting under the impression that 24 of the aircraft are normally on rotation outside of Spain at any given time anyway and aware that the F16 would be reaching the end of its normal service in 10 years, reacted negatively to what they felt was an attempt to mislead them.

American negotiators are said to have felt that they did not present any offer to the Spanish on a take-it-or-leave-it basis and that they presented a range of choices that could have met Spanish concerns.

Behind the outward efforts to maintain the appearance of friendly cooperation, tensions have been mounting on other subjects between Washington and Madrid. Spain has particularly resented what it perceives to be American encouragement to the seven members of the West European Union to deflect Spanish efforts to join that defense group.

The Netherlands and Britain have been particularly active in emphasizing that defense cooperation within WEU could not become an alternative to maintaining the American military presence at its current levels, according to Spanish and NATO sources.