fixed_point_errors.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // RUN: %clang_cc1 -verify -ffixed-point %s
  2. /* We do not yet support long long. No recommended bit widths are given for this
  3. * size. */
  4. long long _Accum longlong_accum; // expected-error{{'long long _Accum' is invalid}}
  5. unsigned long long _Accum u_longlong_accum; // expected-error{{'long long _Accum' is invalid}}
  6. long long _Fract longlong_fract; // expected-error{{'long long _Fract' is invalid}}
  7. unsigned long long _Fract u_longlong_fract; // expected-error{{'long long _Fract' is invalid}}
  8. _Sat long long _Accum sat_longlong_accum; // expected-error{{'long long _Accum' is invalid}}
  9. _Sat unsigned long long _Accum sat_u_longlong_accum; // expected-error{{'long long _Accum' is invalid}}
  10. _Sat long long _Fract sat_longlong_fract; // expected-error{{'long long _Fract' is invalid}}
  11. _Sat unsigned long long _Fract sat_u_longlong_fract; // expected-error{{'long long _Fract' is invalid}}
  12. /* Although _Complex types work with floating point numbers, the extension
  13. * provides no info for complex fixed point types. */
  14. _Complex signed short _Accum cmplx_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  15. _Complex signed _Accum cmplx_s_accum; // expected-error{{'_Complex _Accum' is invalid}}
  16. _Complex signed long _Accum cmplx_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  17. _Complex unsigned short _Accum cmplx_u_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  18. _Complex unsigned _Accum cmplx_u_accum; // expected-error{{'_Complex _Accum' is invalid}}
  19. _Complex unsigned long _Accum cmplx_u_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  20. _Complex short _Accum cmplx_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  21. _Complex _Accum cmplx_s_accum; // expected-error{{'_Complex _Accum' is invalid}}
  22. _Complex long _Accum cmplx_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  23. _Complex signed short _Fract cmplx_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  24. _Complex signed _Fract cmplx_s_fract; // expected-error{{'_Complex _Fract' is invalid}}
  25. _Complex signed long _Fract cmplx_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  26. _Complex unsigned short _Fract cmplx_u_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  27. _Complex unsigned _Fract cmplx_u_fract; // expected-error{{'_Complex _Fract' is invalid}}
  28. _Complex unsigned long _Fract cmplx_u_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  29. _Complex short _Fract cmplx_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  30. _Complex _Fract cmplx_s_fract; // expected-error{{'_Complex _Fract' is invalid}}
  31. _Complex long _Fract cmplx_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  32. _Complex _Sat signed short _Accum cmplx_sat_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  33. _Complex _Sat signed _Accum cmplx_sat_s_accum; // expected-error{{'_Complex _Accum' is invalid}}
  34. _Complex _Sat signed long _Accum cmplx_sat_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  35. _Complex _Sat unsigned short _Accum cmplx_sat_u_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  36. _Complex _Sat unsigned _Accum cmplx_sat_u_accum; // expected-error{{'_Complex _Accum' is invalid}}
  37. _Complex _Sat unsigned long _Accum cmplx_sat_u_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  38. _Complex _Sat short _Accum cmplx_sat_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}
  39. _Complex _Sat _Accum cmplx_sat_s_accum; // expected-error{{'_Complex _Accum' is invalid}}
  40. _Complex _Sat long _Accum cmplx_sat_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}
  41. _Complex signed short _Fract cmplx_sat_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  42. _Complex signed _Fract cmplx_sat_s_fract; // expected-error{{'_Complex _Fract' is invalid}}
  43. _Complex signed long _Fract cmplx_sat_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  44. _Complex unsigned short _Fract cmplx_sat_u_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  45. _Complex unsigned _Fract cmplx_sat_u_fract; // expected-error{{'_Complex _Fract' is invalid}}
  46. _Complex unsigned long _Fract cmplx_sat_u_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  47. _Complex short _Fract cmplx_sat_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}
  48. _Complex _Fract cmplx_sat_s_fract; // expected-error{{'_Complex _Fract' is invalid}}
  49. _Complex long _Fract cmplx_sat_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}
  50. /* Bad combinations */
  51. float _Accum f_accum; // expected-error{{cannot combine with previous 'float' declaration specifier}}
  52. double _Accum d_accum; // expected-error{{cannot combine with previous 'double' declaration specifier}}
  53. _Bool _Accum b_accum; // expected-error{{cannot combine with previous '_Bool' declaration specifier}}
  54. char _Accum c_accum; // expected-error{{cannot combine with previous 'char' declaration specifier}}
  55. int _Accum i_accum; // expected-error{{cannot combine with previous 'int' declaration specifier}}
  56. float _Fract f_fract; // expected-error{{cannot combine with previous 'float' declaration specifier}}
  57. double _Fract d_fract; // expected-error{{cannot combine with previous 'double' declaration specifier}}
  58. _Bool _Fract b_fract; // expected-error{{cannot combine with previous '_Bool' declaration specifier}}
  59. char _Fract c_fract; // expected-error{{cannot combine with previous 'char' declaration specifier}}
  60. int _Fract i_fract; // expected-error{{cannot combine with previous 'int' declaration specifier}}
  61. /* Bad saturated combinations */
  62. _Sat float f; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'float'}}
  63. _Sat double d; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'double'}}
  64. _Sat _Bool b; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not '_Bool'}}
  65. _Sat char c; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'char'}}
  66. _Sat int i; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'int'}}
  67. _Sat _Sat _Fract fract; // expected-warning{{duplicate '_Sat' declaration specifier}}
  68. /* Literals that cannot fit into types */
  69. signed short _Accum s_short_accum = 256.0hk; // expected-error{{this value is too large for this fixed point type}}
  70. unsigned short _Accum u_short_accum = 256.0uhk; // expected-error{{this value is too large for this fixed point type}}
  71. signed _Accum s_accum = 65536.0k; // expected-error{{this value is too large for this fixed point type}}
  72. unsigned _Accum u_accum = 65536.0uk; // expected-error{{this value is too large for this fixed point type}}
  73. signed long _Accum s_long_accum = 4294967296.0lk; // expected-error{{this value is too large for this fixed point type}}
  74. unsigned long _Accum u_long_accum = 4294967296.0ulk; // expected-error{{this value is too large for this fixed point type}}
  75. // Large values from decimal exponents
  76. short _Accum short_accum_exp = 2.56e2hk; // expected-error{{this value is too large for this fixed point type}}
  77. _Accum accum_exp = 6.5536e4k; // expected-error{{this value is too large for this fixed point type}}
  78. long _Accum long_accum_exp = 4.294967296e9lk; // expected-error{{this value is too large for this fixed point type}}
  79. unsigned short _Accum u_short_accum_exp = 2.56e2uhk; // expected-error{{this value is too large for this fixed point type}}
  80. unsigned _Accum u_accum_exp = 6.5536e4uk; // expected-error{{this value is too large for this fixed point type}}
  81. unsigned long _Accum u_long_accum_exp = 4.294967296e9ulk; // expected-error{{this value is too large for this fixed point type}}
  82. // Large value from hexidecimal exponents
  83. short _Accum short_accum_hex_exp = 0x1p8hk; // expected-error{{this value is too large for this fixed point type}}
  84. _Accum accum_hex_exp = 0x1p16k; // expected-error{{this value is too large for this fixed point type}}
  85. long _Accum long_accum_hex_exp = 0x1p32lk; // expected-error{{this value is too large for this fixed point type}}
  86. unsigned short _Accum u_short_accum_hex_exp = 0x1p8uhk; // expected-error{{this value is too large for this fixed point type}}
  87. unsigned _Accum u_accum_hex_exp = 0x1p16uk; // expected-error{{this value is too large for this fixed point type}}
  88. unsigned long _Accum u_long_accum_hex_exp = 0x1p32ulk; // expected-error{{this value is too large for this fixed point type}}
  89. // Very large exponent
  90. _Accum x = 1e1000000000000000000000000000000000k; // expected-error{{this value is too large for this fixed point type}}
  91. /* Although _Fract's cannot equal 1, _Fract literals written as 1 are allowed
  92. * and the underlying value represents the max value for that _Fract type. */
  93. short _Fract short_fract_above_1 = 1.1hr; // expected-error{{this value is too large for this fixed point type}}
  94. _Fract fract_above_1 = 1.1r; // expected-error{{this value is too large for this fixed point type}}
  95. long _Fract long_fract_above_1 = 1.1lr; // expected-error{{this value is too large for this fixed point type}}
  96. unsigned short _Fract u_short_fract_above_1 = 1.1uhr; // expected-error{{this value is too large for this fixed point type}}
  97. unsigned _Fract u_fract_above_1 = 1.1ur; // expected-error{{this value is too large for this fixed point type}}
  98. unsigned long _Fract u_long_fract_above_1 = 1.1ulr; // expected-error{{this value is too large for this fixed point type}}
  99. short _Fract short_fract_hex_exp = 0x0.fp1hr; // expected-error{{this value is too large for this fixed point type}}
  100. _Fract fract_hex_exp = 0x0.fp1r; // expected-error{{this value is too large for this fixed point type}}
  101. long _Fract long_fract_hex_exp = 0x0.fp1lr; // expected-error{{this value is too large for this fixed point type}}
  102. unsigned short _Fract u_short_fract_hex_exp = 0x0.fp1uhr; // expected-error{{this value is too large for this fixed point type}}
  103. unsigned _Fract u_fract_hex_exp = 0x0.fp1ur; // expected-error{{this value is too large for this fixed point type}}
  104. unsigned long _Fract u_long_fract_hex_exp = 0x0.fp1ulr; // expected-error{{this value is too large for this fixed point type}}
  105. /* Do not allow typedef to be used with typedef'd types */
  106. typedef short _Fract shortfract_t;
  107. typedef short _Accum shortaccum_t;
  108. typedef _Fract fract_t;
  109. typedef _Accum accum_t;
  110. typedef long _Fract longfract_t;
  111. typedef long _Accum longaccum_t;
  112. _Sat shortfract_t td_sat_short_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  113. _Sat shortaccum_t td_sat_short_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  114. _Sat fract_t td_sat_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  115. _Sat accum_t td_sat_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  116. _Sat longfract_t td_sat_long_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  117. _Sat longaccum_t td_sat_long_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}
  118. /* Bad suffixes */
  119. _Accum fk = 1.0fk; // expected-error{{invalid suffix 'fk' on integer constant}}
  120. _Accum kk = 1.0kk; // expected-error{{invalid suffix 'kk' on integer constant}}
  121. _Accum rk = 1.0rk; // expected-error{{invalid suffix 'rk' on integer constant}}
  122. _Accum rk = 1.0rr; // expected-error{{invalid suffix 'rr' on integer constant}}
  123. _Accum qk = 1.0qr; // expected-error{{invalid suffix 'qr' on integer constant}}
  124. /* Using wrong exponent notation */
  125. _Accum dec_with_hex_exp1 = 0.1p10k; // expected-error{{invalid suffix 'p10k' on integer constant}}
  126. _Accum dec_with_hex_exp2 = 0.1P10k; // expected-error{{invalid suffix 'P10k' on integer constant}}
  127. _Accum hex_with_dex_exp1 = 0x0.1e10k; // expected-error{{hexadecimal floating constant requires an exponent}}
  128. _Accum hex_with_dex_exp2 = 0x0.1E10k; // expected-error{{hexadecimal floating constant requires an exponent}}
  129. void CheckSuffixOnIntegerLiterals() {
  130. _Accum short_acc_int;
  131. _Accum acc_int;
  132. _Accum long_acc_int;
  133. _Accum u_short_acc_int;
  134. _Accum u_acc_int;
  135. _Accum u_long_acc_int;
  136. _Fract short_fract_int;
  137. _Fract fract_int;
  138. _Fract long_fract_int;
  139. _Fract u_short_fract_int;
  140. _Fract u_fract_int;
  141. _Fract u_long_fract_int;
  142. // Decimal integer literals (non-zero)
  143. short_acc_int = 10hk; // expected-error{{invalid suffix 'hk' on integer constant}}
  144. acc_int = 10k; // expected-error{{invalid suffix 'k' on integer constant}}
  145. long_acc_int = 10lk; // expected-error{{invalid suffix 'lk' on integer constant}}
  146. u_short_acc_int = 10uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}
  147. u_acc_int = 10uk; // expected-error{{invalid suffix 'uk' on integer constant}}
  148. u_long_acc_int = 10ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}
  149. short_fract_int = 10hr; // expected-error{{invalid suffix 'hr' on integer constant}}
  150. fract_int = 10r; // expected-error{{invalid suffix 'r' on integer constant}}
  151. long_fract_int = 10lr; // expected-error{{invalid suffix 'lr' on integer constant}}
  152. u_short_fract_int = 10uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}
  153. u_fract_int = 10ur; // expected-error{{invalid suffix 'ur' on integer constant}}
  154. u_long_fract_int = 10ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}
  155. // Decimal integer literals (0)
  156. short_acc_int = 0hk; // expected-error{{invalid suffix 'hk' on integer constant}}
  157. acc_int = 0k; // expected-error{{invalid suffix 'k' on integer constant}}
  158. long_acc_int = 0lk; // expected-error{{invalid suffix 'lk' on integer constant}}
  159. // Decimal integer literals (large number)
  160. acc_int = 999999999999999999k; // expected-error{{invalid suffix 'k' on integer constant}}
  161. fract_int = 999999999999999999r; // expected-error{{invalid suffix 'r' on integer constant}}
  162. // Octal integer literals
  163. short_acc_int = 010hk; // expected-error{{invalid suffix 'hk' on integer constant}}
  164. acc_int = 010k; // expected-error{{invalid suffix 'k' on integer constant}}
  165. long_acc_int = 010lk; // expected-error{{invalid suffix 'lk' on integer constant}}
  166. u_short_acc_int = 010uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}
  167. u_acc_int = 010uk; // expected-error{{invalid suffix 'uk' on integer constant}}
  168. u_long_acc_int = 010ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}
  169. short_fract_int = 010hr; // expected-error{{invalid suffix 'hr' on integer constant}}
  170. fract_int = 010r; // expected-error{{invalid suffix 'r' on integer constant}}
  171. long_fract_int = 010lr; // expected-error{{invalid suffix 'lr' on integer constant}}
  172. u_short_fract_int = 010uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}
  173. u_fract_int = 010ur; // expected-error{{invalid suffix 'ur' on integer constant}}
  174. u_long_fract_int = 010ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}
  175. // Hexadecimal integer literals
  176. short_acc_int = 0x10hk; // expected-error{{invalid suffix 'hk' on integer constant}}
  177. acc_int = 0x10k; // expected-error{{invalid suffix 'k' on integer constant}}
  178. long_acc_int = 0x10lk; // expected-error{{invalid suffix 'lk' on integer constant}}
  179. u_short_acc_int = 0x10uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}
  180. u_acc_int = 0x10uk; // expected-error{{invalid suffix 'uk' on integer constant}}
  181. u_long_acc_int = 0x10ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}
  182. short_fract_int = 0x10hr; // expected-error{{invalid suffix 'hr' on integer constant}}
  183. fract_int = 0x10r; // expected-error{{invalid suffix 'r' on integer constant}}
  184. long_fract_int = 0x10lr; // expected-error{{invalid suffix 'lr' on integer constant}}
  185. u_short_fract_int = 0x10uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}
  186. u_fract_int = 0x10ur; // expected-error{{invalid suffix 'ur' on integer constant}}
  187. u_long_fract_int = 0x10ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}
  188. // Using auto
  189. auto auto_fract = 0r; // expected-error{{invalid suffix 'r' on integer constant}}
  190. // expected-warning@-1{{type specifier missing, defaults to 'int'}}
  191. auto auto_accum = 0k; // expected-error{{invalid suffix 'k' on integer constant}}
  192. // expected-warning@-1{{type specifier missing, defaults to 'int'}}
  193. }
  194. // Ok conversions
  195. int i_const = -2.5hk;
  196. _Sat short _Accum sat_sa_const2 = 256.0k;
  197. _Sat unsigned short _Accum sat_usa_const = -1.0hk;
  198. short _Accum sa_const3 = 2;
  199. short _Accum sa_const4 = -2;
  200. // Overflow
  201. short _Accum sa_const = 256.0k; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'short _Accum'}}
  202. short _Fract sf_const = 1.0hk; // expected-warning{{implicit conversion from 1.0 cannot fit within the range of values for 'short _Fract'}}
  203. unsigned _Accum ua_const = -1.0k; // expected-warning{{implicit conversion from -1.0 cannot fit within the range of values for 'unsigned _Accum'}}
  204. short _Accum sa_const2 = 128.0k + 128.0k; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'short _Accum'}}
  205. short s_const = 65536.0lk; // expected-warning{{implicit conversion from 65536.0 cannot fit within the range of values for 'short'}}
  206. unsigned u_const = -2.5hk; // expected-warning{{implicit conversion from -2.5 cannot fit within the range of values for 'unsigned int'}}
  207. char c_const = 256.0uk; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'char'}}
  208. short _Accum sa_const5 = 256; // expected-warning{{implicit conversion from 256 cannot fit within the range of values for 'short _Accum'}}
  209. unsigned short _Accum usa_const2 = -2; // expected-warning{{implicit conversion from -2 cannot fit within the range of values for 'unsigned short _Accum'}}