fixed-value.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* Fixed-point arithmetic support.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "fixed-value.h"
  29. #include "tree.h"
  30. #include "diagnostic-core.h"
  31. #include "wide-int.h"
  32. /* Compare two fixed objects for bitwise identity. */
  33. bool
  34. fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
  35. {
  36. return (a->mode == b->mode
  37. && a->data.high == b->data.high
  38. && a->data.low == b->data.low);
  39. }
  40. /* Calculate a hash value. */
  41. unsigned int
  42. fixed_hash (const FIXED_VALUE_TYPE *f)
  43. {
  44. return (unsigned int) (f->data.low ^ f->data.high);
  45. }
  46. /* Define the enum code for the range of the fixed-point value. */
  47. enum fixed_value_range_code {
  48. FIXED_OK, /* The value is within the range. */
  49. FIXED_UNDERFLOW, /* The value is less than the minimum. */
  50. FIXED_GT_MAX_EPS, /* The value is greater than the maximum, but not equal
  51. to the maximum plus the epsilon. */
  52. FIXED_MAX_EPS /* The value equals the maximum plus the epsilon. */
  53. };
  54. /* Check REAL_VALUE against the range of the fixed-point mode.
  55. Return FIXED_OK, if it is within the range.
  56. FIXED_UNDERFLOW, if it is less than the minimum.
  57. FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
  58. the maximum plus the epsilon.
  59. FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
  60. static enum fixed_value_range_code
  61. check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, machine_mode mode)
  62. {
  63. REAL_VALUE_TYPE max_value, min_value, epsilon_value;
  64. real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
  65. real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
  66. if (SIGNED_FIXED_POINT_MODE_P (mode))
  67. min_value = real_value_negate (&max_value);
  68. else
  69. real_from_string (&min_value, "0.0");
  70. if (real_compare (LT_EXPR, real_value, &min_value))
  71. return FIXED_UNDERFLOW;
  72. if (real_compare (EQ_EXPR, real_value, &max_value))
  73. return FIXED_MAX_EPS;
  74. real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
  75. if (real_compare (GT_EXPR, real_value, &max_value))
  76. return FIXED_GT_MAX_EPS;
  77. return FIXED_OK;
  78. }
  79. /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
  80. The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
  81. FIXED_VALUE_TYPE
  82. fixed_from_double_int (double_int payload, machine_mode mode)
  83. {
  84. FIXED_VALUE_TYPE value;
  85. gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
  86. if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
  87. value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
  88. else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
  89. value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
  90. else
  91. gcc_unreachable ();
  92. value.mode = mode;
  93. return value;
  94. }
  95. /* Initialize from a decimal or hexadecimal string. */
  96. void
  97. fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
  98. {
  99. REAL_VALUE_TYPE real_value, fixed_value, base_value;
  100. unsigned int fbit;
  101. enum fixed_value_range_code temp;
  102. bool fail;
  103. f->mode = mode;
  104. fbit = GET_MODE_FBIT (mode);
  105. real_from_string (&real_value, str);
  106. temp = check_real_for_fixed_mode (&real_value, f->mode);
  107. /* We don't want to warn the case when the _Fract value is 1.0. */
  108. if (temp == FIXED_UNDERFLOW
  109. || temp == FIXED_GT_MAX_EPS
  110. || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
  111. warning (OPT_Woverflow,
  112. "large fixed-point constant implicitly truncated to fixed-point type");
  113. real_2expN (&base_value, fbit, mode);
  114. real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
  115. wide_int w = real_to_integer (&fixed_value, &fail,
  116. GET_MODE_PRECISION (mode));
  117. f->data.low = w.elt (0);
  118. f->data.high = w.elt (1);
  119. if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
  120. {
  121. /* From the spec, we need to evaluate 1 to the maximal value. */
  122. f->data.low = -1;
  123. f->data.high = -1;
  124. f->data = f->data.zext (GET_MODE_FBIT (f->mode)
  125. + GET_MODE_IBIT (f->mode));
  126. }
  127. else
  128. f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
  129. + GET_MODE_FBIT (f->mode)
  130. + GET_MODE_IBIT (f->mode),
  131. UNSIGNED_FIXED_POINT_MODE_P (f->mode));
  132. }
  133. /* Render F as a decimal floating point constant. */
  134. void
  135. fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
  136. size_t buf_size)
  137. {
  138. REAL_VALUE_TYPE real_value, base_value, fixed_value;
  139. signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
  140. real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
  141. real_from_integer (&real_value, VOIDmode,
  142. wide_int::from (f_orig->data,
  143. GET_MODE_PRECISION (f_orig->mode), sgn),
  144. sgn);
  145. real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
  146. real_to_decimal (str, &fixed_value, buf_size, 0, 1);
  147. }
  148. /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
  149. the machine mode MODE.
  150. Do not modify *F otherwise.
  151. This function assumes the width of double_int is greater than the width
  152. of the fixed-point value (the sum of a possible sign bit, possible ibits,
  153. and fbits).
  154. Return true, if !SAT_P and overflow. */
  155. static bool
  156. fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
  157. bool sat_p)
  158. {
  159. bool overflow_p = false;
  160. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
  161. int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
  162. if (unsigned_p) /* Unsigned type. */
  163. {
  164. double_int max;
  165. max.low = -1;
  166. max.high = -1;
  167. max = max.zext (i_f_bits);
  168. if (a.ugt (max))
  169. {
  170. if (sat_p)
  171. *f = max;
  172. else
  173. overflow_p = true;
  174. }
  175. }
  176. else /* Signed type. */
  177. {
  178. double_int max, min;
  179. max.high = -1;
  180. max.low = -1;
  181. max = max.zext (i_f_bits);
  182. min.high = 0;
  183. min.low = 1;
  184. min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
  185. min = min.sext (1 + i_f_bits);
  186. if (a.sgt (max))
  187. {
  188. if (sat_p)
  189. *f = max;
  190. else
  191. overflow_p = true;
  192. }
  193. else if (a.slt (min))
  194. {
  195. if (sat_p)
  196. *f = min;
  197. else
  198. overflow_p = true;
  199. }
  200. }
  201. return overflow_p;
  202. }
  203. /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
  204. save to *F based on the machine mode MODE.
  205. Do not modify *F otherwise.
  206. This function assumes the width of two double_int is greater than the width
  207. of the fixed-point value (the sum of a possible sign bit, possible ibits,
  208. and fbits).
  209. Return true, if !SAT_P and overflow. */
  210. static bool
  211. fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
  212. double_int *f, bool sat_p)
  213. {
  214. bool overflow_p = false;
  215. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
  216. int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
  217. if (unsigned_p) /* Unsigned type. */
  218. {
  219. double_int max_r, max_s;
  220. max_r.high = 0;
  221. max_r.low = 0;
  222. max_s.high = -1;
  223. max_s.low = -1;
  224. max_s = max_s.zext (i_f_bits);
  225. if (a_high.ugt (max_r)
  226. || (a_high == max_r &&
  227. a_low.ugt (max_s)))
  228. {
  229. if (sat_p)
  230. *f = max_s;
  231. else
  232. overflow_p = true;
  233. }
  234. }
  235. else /* Signed type. */
  236. {
  237. double_int max_r, max_s, min_r, min_s;
  238. max_r.high = 0;
  239. max_r.low = 0;
  240. max_s.high = -1;
  241. max_s.low = -1;
  242. max_s = max_s.zext (i_f_bits);
  243. min_r.high = -1;
  244. min_r.low = -1;
  245. min_s.high = 0;
  246. min_s.low = 1;
  247. min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
  248. min_s = min_s.sext (1 + i_f_bits);
  249. if (a_high.sgt (max_r)
  250. || (a_high == max_r &&
  251. a_low.ugt (max_s)))
  252. {
  253. if (sat_p)
  254. *f = max_s;
  255. else
  256. overflow_p = true;
  257. }
  258. else if (a_high.slt (min_r)
  259. || (a_high == min_r &&
  260. a_low.ult (min_s)))
  261. {
  262. if (sat_p)
  263. *f = min_s;
  264. else
  265. overflow_p = true;
  266. }
  267. }
  268. return overflow_p;
  269. }
  270. /* Return the sign bit based on I_F_BITS. */
  271. static inline int
  272. get_fixed_sign_bit (double_int a, int i_f_bits)
  273. {
  274. if (i_f_bits < HOST_BITS_PER_WIDE_INT)
  275. return (a.low >> i_f_bits) & 1;
  276. else
  277. return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
  278. }
  279. /* Calculate F = A + (SUBTRACT_P ? -B : B).
  280. If SAT_P, saturate the result to the max or the min.
  281. Return true, if !SAT_P and overflow. */
  282. static bool
  283. do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
  284. const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
  285. {
  286. bool overflow_p = false;
  287. bool unsigned_p;
  288. double_int temp;
  289. int i_f_bits;
  290. /* This was a conditional expression but it triggered a bug in
  291. Sun C 5.5. */
  292. if (subtract_p)
  293. temp = -b->data;
  294. else
  295. temp = b->data;
  296. unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
  297. i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
  298. f->mode = a->mode;
  299. f->data = a->data + temp;
  300. if (unsigned_p) /* Unsigned type. */
  301. {
  302. if (subtract_p) /* Unsigned subtraction. */
  303. {
  304. if (a->data.ult (b->data))
  305. {
  306. if (sat_p)
  307. {
  308. f->data.high = 0;
  309. f->data.low = 0;
  310. }
  311. else
  312. overflow_p = true;
  313. }
  314. }
  315. else /* Unsigned addition. */
  316. {
  317. f->data = f->data.zext (i_f_bits);
  318. if (f->data.ult (a->data)
  319. || f->data.ult (b->data))
  320. {
  321. if (sat_p)
  322. {
  323. f->data.high = -1;
  324. f->data.low = -1;
  325. }
  326. else
  327. overflow_p = true;
  328. }
  329. }
  330. }
  331. else /* Signed type. */
  332. {
  333. if ((!subtract_p
  334. && (get_fixed_sign_bit (a->data, i_f_bits)
  335. == get_fixed_sign_bit (b->data, i_f_bits))
  336. && (get_fixed_sign_bit (a->data, i_f_bits)
  337. != get_fixed_sign_bit (f->data, i_f_bits)))
  338. || (subtract_p
  339. && (get_fixed_sign_bit (a->data, i_f_bits)
  340. != get_fixed_sign_bit (b->data, i_f_bits))
  341. && (get_fixed_sign_bit (a->data, i_f_bits)
  342. != get_fixed_sign_bit (f->data, i_f_bits))))
  343. {
  344. if (sat_p)
  345. {
  346. f->data.low = 1;
  347. f->data.high = 0;
  348. f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
  349. if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
  350. {
  351. --f->data;
  352. }
  353. }
  354. else
  355. overflow_p = true;
  356. }
  357. }
  358. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  359. return overflow_p;
  360. }
  361. /* Calculate F = A * B.
  362. If SAT_P, saturate the result to the max or the min.
  363. Return true, if !SAT_P and overflow. */
  364. static bool
  365. do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
  366. const FIXED_VALUE_TYPE *b, bool sat_p)
  367. {
  368. bool overflow_p = false;
  369. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
  370. int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
  371. f->mode = a->mode;
  372. if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
  373. {
  374. f->data = a->data * b->data;
  375. f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
  376. HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
  377. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
  378. }
  379. else
  380. {
  381. /* The result of multiplication expands to two double_int. */
  382. double_int a_high, a_low, b_high, b_low;
  383. double_int high_high, high_low, low_high, low_low;
  384. double_int r, s, temp1, temp2;
  385. int carry = 0;
  386. /* Decompose a and b to four double_int. */
  387. a_high.low = a->data.high;
  388. a_high.high = 0;
  389. a_low.low = a->data.low;
  390. a_low.high = 0;
  391. b_high.low = b->data.high;
  392. b_high.high = 0;
  393. b_low.low = b->data.low;
  394. b_low.high = 0;
  395. /* Perform four multiplications. */
  396. low_low = a_low * b_low;
  397. low_high = a_low * b_high;
  398. high_low = a_high * b_low;
  399. high_high = a_high * b_high;
  400. /* Accumulate four results to {r, s}. */
  401. temp1.high = high_low.low;
  402. temp1.low = 0;
  403. s = low_low + temp1;
  404. if (s.ult (low_low)
  405. || s.ult (temp1))
  406. carry ++; /* Carry */
  407. temp1.high = s.high;
  408. temp1.low = s.low;
  409. temp2.high = low_high.low;
  410. temp2.low = 0;
  411. s = temp1 + temp2;
  412. if (s.ult (temp1)
  413. || s.ult (temp2))
  414. carry ++; /* Carry */
  415. temp1.low = high_low.high;
  416. temp1.high = 0;
  417. r = high_high + temp1;
  418. temp1.low = low_high.high;
  419. temp1.high = 0;
  420. r += temp1;
  421. temp1.low = carry;
  422. temp1.high = 0;
  423. r += temp1;
  424. /* We need to subtract b from r, if a < 0. */
  425. if (!unsigned_p && a->data.high < 0)
  426. r -= b->data;
  427. /* We need to subtract a from r, if b < 0. */
  428. if (!unsigned_p && b->data.high < 0)
  429. r -= a->data;
  430. /* Shift right the result by FBIT. */
  431. if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
  432. {
  433. s.low = r.low;
  434. s.high = r.high;
  435. if (unsigned_p)
  436. {
  437. r.low = 0;
  438. r.high = 0;
  439. }
  440. else
  441. {
  442. r.low = -1;
  443. r.high = -1;
  444. }
  445. f->data.low = s.low;
  446. f->data.high = s.high;
  447. }
  448. else
  449. {
  450. s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
  451. f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
  452. - GET_MODE_FBIT (f->mode)),
  453. HOST_BITS_PER_DOUBLE_INT);
  454. f->data.low = f->data.low | s.low;
  455. f->data.high = f->data.high | s.high;
  456. s.low = f->data.low;
  457. s.high = f->data.high;
  458. r = r.lshift (-GET_MODE_FBIT (f->mode),
  459. HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
  460. }
  461. overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
  462. }
  463. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  464. return overflow_p;
  465. }
  466. /* Calculate F = A / B.
  467. If SAT_P, saturate the result to the max or the min.
  468. Return true, if !SAT_P and overflow. */
  469. static bool
  470. do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
  471. const FIXED_VALUE_TYPE *b, bool sat_p)
  472. {
  473. bool overflow_p = false;
  474. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
  475. int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
  476. f->mode = a->mode;
  477. if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
  478. {
  479. f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
  480. HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
  481. f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
  482. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
  483. }
  484. else
  485. {
  486. double_int pos_a, pos_b, r, s;
  487. double_int quo_r, quo_s, mod, temp;
  488. int num_of_neg = 0;
  489. int i;
  490. /* If a < 0, negate a. */
  491. if (!unsigned_p && a->data.high < 0)
  492. {
  493. pos_a = -a->data;
  494. num_of_neg ++;
  495. }
  496. else
  497. pos_a = a->data;
  498. /* If b < 0, negate b. */
  499. if (!unsigned_p && b->data.high < 0)
  500. {
  501. pos_b = -b->data;
  502. num_of_neg ++;
  503. }
  504. else
  505. pos_b = b->data;
  506. /* Left shift pos_a to {r, s} by FBIT. */
  507. if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
  508. {
  509. r = pos_a;
  510. s.high = 0;
  511. s.low = 0;
  512. }
  513. else
  514. {
  515. s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
  516. r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
  517. - GET_MODE_FBIT (f->mode)),
  518. HOST_BITS_PER_DOUBLE_INT);
  519. }
  520. /* Divide r by pos_b to quo_r. The remainder is in mod. */
  521. quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
  522. quo_s = double_int_zero;
  523. for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
  524. {
  525. /* Record the leftmost bit of mod. */
  526. int leftmost_mod = (mod.high < 0);
  527. /* Shift left mod by 1 bit. */
  528. mod = mod.lshift (1);
  529. /* Test the leftmost bit of s to add to mod. */
  530. if (s.high < 0)
  531. mod.low += 1;
  532. /* Shift left quo_s by 1 bit. */
  533. quo_s = quo_s.lshift (1);
  534. /* Try to calculate (mod - pos_b). */
  535. temp = mod - pos_b;
  536. if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
  537. {
  538. quo_s.low += 1;
  539. mod = temp;
  540. }
  541. /* Shift left s by 1 bit. */
  542. s = s.lshift (1);
  543. }
  544. if (num_of_neg == 1)
  545. {
  546. quo_s = -quo_s;
  547. if (quo_s.high == 0 && quo_s.low == 0)
  548. quo_r = -quo_r;
  549. else
  550. {
  551. quo_r.low = ~quo_r.low;
  552. quo_r.high = ~quo_r.high;
  553. }
  554. }
  555. f->data = quo_s;
  556. overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
  557. }
  558. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  559. return overflow_p;
  560. }
  561. /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
  562. If SAT_P, saturate the result to the max or the min.
  563. Return true, if !SAT_P and overflow. */
  564. static bool
  565. do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
  566. const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
  567. {
  568. bool overflow_p = false;
  569. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
  570. int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
  571. f->mode = a->mode;
  572. if (b->data.low == 0)
  573. {
  574. f->data = a->data;
  575. return overflow_p;
  576. }
  577. if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
  578. {
  579. f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
  580. HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
  581. if (left_p) /* Only left shift saturates. */
  582. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
  583. }
  584. else /* We need two double_int to store the left-shift result. */
  585. {
  586. double_int temp_high, temp_low;
  587. if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
  588. {
  589. temp_high = a->data;
  590. temp_low.high = 0;
  591. temp_low.low = 0;
  592. }
  593. else
  594. {
  595. temp_low = a->data.lshift (b->data.low,
  596. HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
  597. /* Logical shift right to temp_high. */
  598. temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
  599. HOST_BITS_PER_DOUBLE_INT);
  600. }
  601. if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
  602. temp_high = temp_high.ext (b->data.low, unsigned_p);
  603. f->data = temp_low;
  604. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
  605. sat_p);
  606. }
  607. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  608. return overflow_p;
  609. }
  610. /* Calculate F = -A.
  611. If SAT_P, saturate the result to the max or the min.
  612. Return true, if !SAT_P and overflow. */
  613. static bool
  614. do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
  615. {
  616. bool overflow_p = false;
  617. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
  618. int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
  619. f->mode = a->mode;
  620. f->data = -a->data;
  621. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  622. if (unsigned_p) /* Unsigned type. */
  623. {
  624. if (f->data.low != 0 || f->data.high != 0)
  625. {
  626. if (sat_p)
  627. {
  628. f->data.low = 0;
  629. f->data.high = 0;
  630. }
  631. else
  632. overflow_p = true;
  633. }
  634. }
  635. else /* Signed type. */
  636. {
  637. if (!(f->data.high == 0 && f->data.low == 0)
  638. && f->data.high == a->data.high && f->data.low == a->data.low )
  639. {
  640. if (sat_p)
  641. {
  642. /* Saturate to the maximum by subtracting f->data by one. */
  643. f->data.low = -1;
  644. f->data.high = -1;
  645. f->data = f->data.zext (i_f_bits);
  646. }
  647. else
  648. overflow_p = true;
  649. }
  650. }
  651. return overflow_p;
  652. }
  653. /* Perform the binary or unary operation described by CODE.
  654. Note that OP0 and OP1 must have the same mode for binary operators.
  655. For a unary operation, leave OP1 NULL.
  656. Return true, if !SAT_P and overflow. */
  657. bool
  658. fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
  659. const FIXED_VALUE_TYPE *op1, bool sat_p)
  660. {
  661. switch (icode)
  662. {
  663. case NEGATE_EXPR:
  664. return do_fixed_neg (f, op0, sat_p);
  665. break;
  666. case PLUS_EXPR:
  667. gcc_assert (op0->mode == op1->mode);
  668. return do_fixed_add (f, op0, op1, false, sat_p);
  669. break;
  670. case MINUS_EXPR:
  671. gcc_assert (op0->mode == op1->mode);
  672. return do_fixed_add (f, op0, op1, true, sat_p);
  673. break;
  674. case MULT_EXPR:
  675. gcc_assert (op0->mode == op1->mode);
  676. return do_fixed_multiply (f, op0, op1, sat_p);
  677. break;
  678. case TRUNC_DIV_EXPR:
  679. gcc_assert (op0->mode == op1->mode);
  680. return do_fixed_divide (f, op0, op1, sat_p);
  681. break;
  682. case LSHIFT_EXPR:
  683. return do_fixed_shift (f, op0, op1, true, sat_p);
  684. break;
  685. case RSHIFT_EXPR:
  686. return do_fixed_shift (f, op0, op1, false, sat_p);
  687. break;
  688. default:
  689. gcc_unreachable ();
  690. }
  691. return false;
  692. }
  693. /* Compare fixed-point values by tree_code.
  694. Note that OP0 and OP1 must have the same mode. */
  695. bool
  696. fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
  697. const FIXED_VALUE_TYPE *op1)
  698. {
  699. enum tree_code code = (enum tree_code) icode;
  700. gcc_assert (op0->mode == op1->mode);
  701. switch (code)
  702. {
  703. case NE_EXPR:
  704. return op0->data != op1->data;
  705. case EQ_EXPR:
  706. return op0->data == op1->data;
  707. case LT_EXPR:
  708. return op0->data.cmp (op1->data,
  709. UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
  710. case LE_EXPR:
  711. return op0->data.cmp (op1->data,
  712. UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
  713. case GT_EXPR:
  714. return op0->data.cmp (op1->data,
  715. UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
  716. case GE_EXPR:
  717. return op0->data.cmp (op1->data,
  718. UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
  719. default:
  720. gcc_unreachable ();
  721. }
  722. }
  723. /* Extend or truncate to a new mode.
  724. If SAT_P, saturate the result to the max or the min.
  725. Return true, if !SAT_P and overflow. */
  726. bool
  727. fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
  728. const FIXED_VALUE_TYPE *a, bool sat_p)
  729. {
  730. bool overflow_p = false;
  731. if (mode == a->mode)
  732. {
  733. *f = *a;
  734. return overflow_p;
  735. }
  736. if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
  737. {
  738. /* Left shift a to temp_high, temp_low based on a->mode. */
  739. double_int temp_high, temp_low;
  740. int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
  741. temp_low = a->data.lshift (amount,
  742. HOST_BITS_PER_DOUBLE_INT,
  743. SIGNED_FIXED_POINT_MODE_P (a->mode));
  744. /* Logical shift right to temp_high. */
  745. temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
  746. HOST_BITS_PER_DOUBLE_INT);
  747. if (SIGNED_FIXED_POINT_MODE_P (a->mode)
  748. && a->data.high < 0) /* Signed-extend temp_high. */
  749. temp_high = temp_high.sext (amount);
  750. f->mode = mode;
  751. f->data = temp_low;
  752. if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
  753. SIGNED_FIXED_POINT_MODE_P (f->mode))
  754. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
  755. sat_p);
  756. else
  757. {
  758. /* Take care of the cases when converting between signed and
  759. unsigned. */
  760. if (SIGNED_FIXED_POINT_MODE_P (a->mode))
  761. {
  762. /* Signed -> Unsigned. */
  763. if (a->data.high < 0)
  764. {
  765. if (sat_p)
  766. {
  767. f->data.low = 0; /* Set to zero. */
  768. f->data.high = 0; /* Set to zero. */
  769. }
  770. else
  771. overflow_p = true;
  772. }
  773. else
  774. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
  775. &f->data, sat_p);
  776. }
  777. else
  778. {
  779. /* Unsigned -> Signed. */
  780. if (temp_high.high < 0)
  781. {
  782. if (sat_p)
  783. {
  784. /* Set to maximum. */
  785. f->data.low = -1; /* Set to all ones. */
  786. f->data.high = -1; /* Set to all ones. */
  787. f->data = f->data.zext (GET_MODE_FBIT (f->mode)
  788. + GET_MODE_IBIT (f->mode));
  789. /* Clear the sign. */
  790. }
  791. else
  792. overflow_p = true;
  793. }
  794. else
  795. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
  796. &f->data, sat_p);
  797. }
  798. }
  799. }
  800. else
  801. {
  802. /* Right shift a to temp based on a->mode. */
  803. double_int temp;
  804. temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
  805. HOST_BITS_PER_DOUBLE_INT,
  806. SIGNED_FIXED_POINT_MODE_P (a->mode));
  807. f->mode = mode;
  808. f->data = temp;
  809. if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
  810. SIGNED_FIXED_POINT_MODE_P (f->mode))
  811. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
  812. else
  813. {
  814. /* Take care of the cases when converting between signed and
  815. unsigned. */
  816. if (SIGNED_FIXED_POINT_MODE_P (a->mode))
  817. {
  818. /* Signed -> Unsigned. */
  819. if (a->data.high < 0)
  820. {
  821. if (sat_p)
  822. {
  823. f->data.low = 0; /* Set to zero. */
  824. f->data.high = 0; /* Set to zero. */
  825. }
  826. else
  827. overflow_p = true;
  828. }
  829. else
  830. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
  831. sat_p);
  832. }
  833. else
  834. {
  835. /* Unsigned -> Signed. */
  836. if (temp.high < 0)
  837. {
  838. if (sat_p)
  839. {
  840. /* Set to maximum. */
  841. f->data.low = -1; /* Set to all ones. */
  842. f->data.high = -1; /* Set to all ones. */
  843. f->data = f->data.zext (GET_MODE_FBIT (f->mode)
  844. + GET_MODE_IBIT (f->mode));
  845. /* Clear the sign. */
  846. }
  847. else
  848. overflow_p = true;
  849. }
  850. else
  851. overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
  852. sat_p);
  853. }
  854. }
  855. }
  856. f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
  857. + GET_MODE_FBIT (f->mode)
  858. + GET_MODE_IBIT (f->mode),
  859. UNSIGNED_FIXED_POINT_MODE_P (f->mode));
  860. return overflow_p;
  861. }
  862. /* Convert to a new fixed-point mode from an integer.
  863. If UNSIGNED_P, this integer is unsigned.
  864. If SAT_P, saturate the result to the max or the min.
  865. Return true, if !SAT_P and overflow. */
  866. bool
  867. fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
  868. double_int a, bool unsigned_p, bool sat_p)
  869. {
  870. bool overflow_p = false;
  871. /* Left shift a to temp_high, temp_low. */
  872. double_int temp_high, temp_low;
  873. int amount = GET_MODE_FBIT (mode);
  874. if (amount == HOST_BITS_PER_DOUBLE_INT)
  875. {
  876. temp_high = a;
  877. temp_low.low = 0;
  878. temp_low.high = 0;
  879. }
  880. else
  881. {
  882. temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
  883. /* Logical shift right to temp_high. */
  884. temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
  885. HOST_BITS_PER_DOUBLE_INT);
  886. }
  887. if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
  888. temp_high = temp_high.sext (amount);
  889. f->mode = mode;
  890. f->data = temp_low;
  891. if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
  892. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
  893. sat_p);
  894. else
  895. {
  896. /* Take care of the cases when converting between signed and unsigned. */
  897. if (!unsigned_p)
  898. {
  899. /* Signed -> Unsigned. */
  900. if (a.high < 0)
  901. {
  902. if (sat_p)
  903. {
  904. f->data.low = 0; /* Set to zero. */
  905. f->data.high = 0; /* Set to zero. */
  906. }
  907. else
  908. overflow_p = true;
  909. }
  910. else
  911. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
  912. &f->data, sat_p);
  913. }
  914. else
  915. {
  916. /* Unsigned -> Signed. */
  917. if (temp_high.high < 0)
  918. {
  919. if (sat_p)
  920. {
  921. /* Set to maximum. */
  922. f->data.low = -1; /* Set to all ones. */
  923. f->data.high = -1; /* Set to all ones. */
  924. f->data = f->data.zext (GET_MODE_FBIT (f->mode)
  925. + GET_MODE_IBIT (f->mode));
  926. /* Clear the sign. */
  927. }
  928. else
  929. overflow_p = true;
  930. }
  931. else
  932. overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
  933. &f->data, sat_p);
  934. }
  935. }
  936. f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
  937. + GET_MODE_FBIT (f->mode)
  938. + GET_MODE_IBIT (f->mode),
  939. UNSIGNED_FIXED_POINT_MODE_P (f->mode));
  940. return overflow_p;
  941. }
  942. /* Convert to a new fixed-point mode from a real.
  943. If SAT_P, saturate the result to the max or the min.
  944. Return true, if !SAT_P and overflow. */
  945. bool
  946. fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
  947. const REAL_VALUE_TYPE *a, bool sat_p)
  948. {
  949. bool overflow_p = false;
  950. REAL_VALUE_TYPE real_value, fixed_value, base_value;
  951. bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
  952. int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
  953. unsigned int fbit = GET_MODE_FBIT (mode);
  954. enum fixed_value_range_code temp;
  955. bool fail;
  956. real_value = *a;
  957. f->mode = mode;
  958. real_2expN (&base_value, fbit, mode);
  959. real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
  960. wide_int w = real_to_integer (&fixed_value, &fail,
  961. GET_MODE_PRECISION (mode));
  962. f->data.low = w.elt (0);
  963. f->data.high = w.elt (1);
  964. temp = check_real_for_fixed_mode (&real_value, mode);
  965. if (temp == FIXED_UNDERFLOW) /* Minimum. */
  966. {
  967. if (sat_p)
  968. {
  969. if (unsigned_p)
  970. {
  971. f->data.low = 0;
  972. f->data.high = 0;
  973. }
  974. else
  975. {
  976. f->data.low = 1;
  977. f->data.high = 0;
  978. f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
  979. f->data = f->data.sext (1 + i_f_bits);
  980. }
  981. }
  982. else
  983. overflow_p = true;
  984. }
  985. else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
  986. {
  987. if (sat_p)
  988. {
  989. f->data.low = -1;
  990. f->data.high = -1;
  991. f->data = f->data.zext (i_f_bits);
  992. }
  993. else
  994. overflow_p = true;
  995. }
  996. f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
  997. return overflow_p;
  998. }
  999. /* Convert to a new real mode from a fixed-point. */
  1000. void
  1001. real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
  1002. const FIXED_VALUE_TYPE *f)
  1003. {
  1004. REAL_VALUE_TYPE base_value, fixed_value, real_value;
  1005. signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
  1006. real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
  1007. real_from_integer (&fixed_value, VOIDmode,
  1008. wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
  1009. sgn), sgn);
  1010. real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
  1011. real_convert (r, mode, &real_value);
  1012. }
  1013. /* Determine whether a fixed-point value F is negative. */
  1014. bool
  1015. fixed_isneg (const FIXED_VALUE_TYPE *f)
  1016. {
  1017. if (SIGNED_FIXED_POINT_MODE_P (f->mode))
  1018. {
  1019. int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
  1020. int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
  1021. if (sign_bit == 1)
  1022. return true;
  1023. }
  1024. return false;
  1025. }