290 lines · c
1// RUN: %clang_cc1 -verify -ffixed-point %s2// RUN: %clang_cc1 -verify -ffixed-point %s -fexperimental-new-constant-interpreter3 4/* We do not yet support long long. No recommended bit widths are given for this5 * size. */6 7long long _Accum longlong_accum; // expected-error{{'long long _Accum' is invalid}}8unsigned long long _Accum u_longlong_accum; // expected-error{{'long long _Accum' is invalid}}9long long _Fract longlong_fract; // expected-error{{'long long _Fract' is invalid}}10unsigned long long _Fract u_longlong_fract; // expected-error{{'long long _Fract' is invalid}}11 12_Sat long long _Accum sat_longlong_accum; // expected-error{{'long long _Accum' is invalid}}13_Sat unsigned long long _Accum sat_u_longlong_accum; // expected-error{{'long long _Accum' is invalid}}14_Sat long long _Fract sat_longlong_fract; // expected-error{{'long long _Fract' is invalid}}15_Sat unsigned long long _Fract sat_u_longlong_fract; // expected-error{{'long long _Fract' is invalid}}16 17 18/* Although _Complex types work with floating point numbers, the extension19 * provides no info for complex fixed point types. */20 21_Complex signed short _Accum cmplx_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}22_Complex signed _Accum cmplx_s_accum; // expected-error{{'_Complex _Accum' is invalid}}23_Complex signed long _Accum cmplx_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}24_Complex unsigned short _Accum cmplx_u_short_accum; // expected-error{{'_Complex _Accum' is invalid}}25_Complex unsigned _Accum cmplx_u_accum; // expected-error{{'_Complex _Accum' is invalid}}26_Complex unsigned long _Accum cmplx_u_long_accum; // expected-error{{'_Complex _Accum' is invalid}}27_Complex short _Accum cmplx_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}28_Complex _Accum cmplx_s_accum; // expected-error{{'_Complex _Accum' is invalid}}29_Complex long _Accum cmplx_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}30 31_Complex signed short _Fract cmplx_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}32_Complex signed _Fract cmplx_s_fract; // expected-error{{'_Complex _Fract' is invalid}}33_Complex signed long _Fract cmplx_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}34_Complex unsigned short _Fract cmplx_u_short_fract; // expected-error{{'_Complex _Fract' is invalid}}35_Complex unsigned _Fract cmplx_u_fract; // expected-error{{'_Complex _Fract' is invalid}}36_Complex unsigned long _Fract cmplx_u_long_fract; // expected-error{{'_Complex _Fract' is invalid}}37_Complex short _Fract cmplx_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}38_Complex _Fract cmplx_s_fract; // expected-error{{'_Complex _Fract' is invalid}}39_Complex long _Fract cmplx_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}40 41_Complex _Sat signed short _Accum cmplx_sat_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}42_Complex _Sat signed _Accum cmplx_sat_s_accum; // expected-error{{'_Complex _Accum' is invalid}}43_Complex _Sat signed long _Accum cmplx_sat_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}44_Complex _Sat unsigned short _Accum cmplx_sat_u_short_accum; // expected-error{{'_Complex _Accum' is invalid}}45_Complex _Sat unsigned _Accum cmplx_sat_u_accum; // expected-error{{'_Complex _Accum' is invalid}}46_Complex _Sat unsigned long _Accum cmplx_sat_u_long_accum; // expected-error{{'_Complex _Accum' is invalid}}47_Complex _Sat short _Accum cmplx_sat_s_short_accum; // expected-error{{'_Complex _Accum' is invalid}}48_Complex _Sat _Accum cmplx_sat_s_accum; // expected-error{{'_Complex _Accum' is invalid}}49_Complex _Sat long _Accum cmplx_sat_s_long_accum; // expected-error{{'_Complex _Accum' is invalid}}50 51_Complex signed short _Fract cmplx_sat_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}52_Complex signed _Fract cmplx_sat_s_fract; // expected-error{{'_Complex _Fract' is invalid}}53_Complex signed long _Fract cmplx_sat_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}54_Complex unsigned short _Fract cmplx_sat_u_short_fract; // expected-error{{'_Complex _Fract' is invalid}}55_Complex unsigned _Fract cmplx_sat_u_fract; // expected-error{{'_Complex _Fract' is invalid}}56_Complex unsigned long _Fract cmplx_sat_u_long_fract; // expected-error{{'_Complex _Fract' is invalid}}57_Complex short _Fract cmplx_sat_s_short_fract; // expected-error{{'_Complex _Fract' is invalid}}58_Complex _Fract cmplx_sat_s_fract; // expected-error{{'_Complex _Fract' is invalid}}59_Complex long _Fract cmplx_sat_s_long_fract; // expected-error{{'_Complex _Fract' is invalid}}60 61/* Bad combinations */62float _Accum f_accum; // expected-error{{cannot combine with previous 'float' declaration specifier}}63double _Accum d_accum; // expected-error{{cannot combine with previous 'double' declaration specifier}}64_Bool _Accum b_accum; // expected-error{{cannot combine with previous '_Bool' declaration specifier}}65char _Accum c_accum; // expected-error{{cannot combine with previous 'char' declaration specifier}}66int _Accum i_accum; // expected-error{{cannot combine with previous 'int' declaration specifier}}67 68float _Fract f_fract; // expected-error{{cannot combine with previous 'float' declaration specifier}}69double _Fract d_fract; // expected-error{{cannot combine with previous 'double' declaration specifier}}70_Bool _Fract b_fract; // expected-error{{cannot combine with previous '_Bool' declaration specifier}}71char _Fract c_fract; // expected-error{{cannot combine with previous 'char' declaration specifier}}72int _Fract i_fract; // expected-error{{cannot combine with previous 'int' declaration specifier}}73 74/* Bad saturated combinations */75_Sat float f; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'float'}}76_Sat double d; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'double'}}77_Sat _Bool b; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not '_Bool'}}78_Sat char c; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'char'}}79_Sat int i; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'int'}}80_Sat _Sat _Fract fract; // expected-warning{{duplicate '_Sat' declaration specifier}}81 82 83/* Literals that cannot fit into types */84signed short _Accum s_short_accum = 256.0hk; // expected-error{{this value is too large for this fixed point type}}85unsigned short _Accum u_short_accum = 256.0uhk; // expected-error{{this value is too large for this fixed point type}}86signed _Accum s_accum = 65536.0k; // expected-error{{this value is too large for this fixed point type}}87unsigned _Accum u_accum = 65536.0uk; // expected-error{{this value is too large for this fixed point type}}88signed long _Accum s_long_accum = 4294967296.0lk; // expected-error{{this value is too large for this fixed point type}}89unsigned long _Accum u_long_accum = 4294967296.0ulk; // expected-error{{this value is too large for this fixed point type}}90 91// Large values from decimal exponents92short _Accum short_accum_exp = 2.56e2hk; // expected-error{{this value is too large for this fixed point type}}93_Accum accum_exp = 6.5536e4k; // expected-error{{this value is too large for this fixed point type}}94long _Accum long_accum_exp = 4.294967296e9lk; // expected-error{{this value is too large for this fixed point type}}95unsigned short _Accum u_short_accum_exp = 2.56e2uhk; // expected-error{{this value is too large for this fixed point type}}96unsigned _Accum u_accum_exp = 6.5536e4uk; // expected-error{{this value is too large for this fixed point type}}97unsigned long _Accum u_long_accum_exp = 4.294967296e9ulk; // expected-error{{this value is too large for this fixed point type}}98 99// Large value from hexidecimal exponents100short _Accum short_accum_hex_exp = 0x1p8hk; // expected-error{{this value is too large for this fixed point type}}101_Accum accum_hex_exp = 0x1p16k; // expected-error{{this value is too large for this fixed point type}}102long _Accum long_accum_hex_exp = 0x1p32lk; // expected-error{{this value is too large for this fixed point type}}103unsigned short _Accum u_short_accum_hex_exp = 0x1p8uhk; // expected-error{{this value is too large for this fixed point type}}104unsigned _Accum u_accum_hex_exp = 0x1p16uk; // expected-error{{this value is too large for this fixed point type}}105unsigned long _Accum u_long_accum_hex_exp = 0x1p32ulk; // expected-error{{this value is too large for this fixed point type}}106 107// Very large exponent108_Accum x = 1e1000000000000000000000000000000000k; // expected-error{{this value is too large for this fixed point type}}109 110/* Although _Fract's cannot equal 1, _Fract literals written as 1 are allowed111 * and the underlying value represents the max value for that _Fract type. */112short _Fract short_fract_above_1 = 1.1hr; // expected-error{{this value is too large for this fixed point type}}113_Fract fract_above_1 = 1.1r; // expected-error{{this value is too large for this fixed point type}}114long _Fract long_fract_above_1 = 1.1lr; // expected-error{{this value is too large for this fixed point type}}115unsigned short _Fract u_short_fract_above_1 = 1.1uhr; // expected-error{{this value is too large for this fixed point type}}116unsigned _Fract u_fract_above_1 = 1.1ur; // expected-error{{this value is too large for this fixed point type}}117unsigned long _Fract u_long_fract_above_1 = 1.1ulr; // expected-error{{this value is too large for this fixed point type}}118 119short _Fract short_fract_hex_exp = 0x0.fp1hr; // expected-error{{this value is too large for this fixed point type}}120_Fract fract_hex_exp = 0x0.fp1r; // expected-error{{this value is too large for this fixed point type}}121long _Fract long_fract_hex_exp = 0x0.fp1lr; // expected-error{{this value is too large for this fixed point type}}122unsigned short _Fract u_short_fract_hex_exp = 0x0.fp1uhr; // expected-error{{this value is too large for this fixed point type}}123unsigned _Fract u_fract_hex_exp = 0x0.fp1ur; // expected-error{{this value is too large for this fixed point type}}124unsigned long _Fract u_long_fract_hex_exp = 0x0.fp1ulr; // expected-error{{this value is too large for this fixed point type}}125 126/* Do not allow typedef to be used with typedef'd types */127typedef short _Fract shortfract_t;128typedef short _Accum shortaccum_t;129typedef _Fract fract_t;130typedef _Accum accum_t;131typedef long _Fract longfract_t;132typedef long _Accum longaccum_t;133_Sat shortfract_t td_sat_short_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}134_Sat shortaccum_t td_sat_short_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}135_Sat fract_t td_sat_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}136_Sat accum_t td_sat_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}137_Sat longfract_t td_sat_long_fract; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}138_Sat longaccum_t td_sat_long_accum; // expected-error{{'_Sat' specifier is only valid on '_Fract' or '_Accum', not 'type-name'}}139 140/* Bad suffixes */141_Accum fk = 1.0fk; // expected-error{{invalid suffix 'fk' on fixed-point constant}}142_Accum kk = 1.0kk; // expected-error{{invalid suffix 'kk' on fixed-point constant}}143_Accum rk = 1.0rk; // expected-error{{invalid suffix 'rk' on fixed-point constant}}144_Accum rk = 1.0rr; // expected-error{{invalid suffix 'rr' on fixed-point constant}}145_Accum qk = 1.0qr; // expected-error{{invalid suffix 'qr' on fixed-point constant}}146 147/* Using wrong exponent notation */148_Accum dec_with_hex_exp1 = 0.1p10k; // expected-error{{invalid suffix 'p10k' on fixed-point constant}}149_Accum dec_with_hex_exp2 = 0.1P10k; // expected-error{{invalid suffix 'P10k' on fixed-point constant}}150_Accum hex_with_dex_exp1 = 0x0.1e10k; // expected-error{{hexadecimal floating constant requires an exponent}}151_Accum hex_with_dex_exp2 = 0x0.1E10k; // expected-error{{hexadecimal floating constant requires an exponent}}152 153void CheckSuffixOnIntegerLiterals(void) {154 _Accum short_acc_int;155 _Accum acc_int;156 _Accum long_acc_int;157 158 _Accum u_short_acc_int;159 _Accum u_acc_int;160 _Accum u_long_acc_int;161 162 _Fract short_fract_int;163 _Fract fract_int;164 _Fract long_fract_int;165 166 _Fract u_short_fract_int;167 _Fract u_fract_int;168 _Fract u_long_fract_int;169 170 // Decimal integer literals (non-zero)171 short_acc_int = 10hk; // expected-error{{invalid suffix 'hk' on integer constant}}172 acc_int = 10k; // expected-error{{invalid suffix 'k' on integer constant}}173 long_acc_int = 10lk; // expected-error{{invalid suffix 'lk' on integer constant}}174 175 u_short_acc_int = 10uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}176 u_acc_int = 10uk; // expected-error{{invalid suffix 'uk' on integer constant}}177 u_long_acc_int = 10ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}178 179 short_fract_int = 10hr; // expected-error{{invalid suffix 'hr' on integer constant}}180 fract_int = 10r; // expected-error{{invalid suffix 'r' on integer constant}}181 long_fract_int = 10lr; // expected-error{{invalid suffix 'lr' on integer constant}}182 183 u_short_fract_int = 10uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}184 u_fract_int = 10ur; // expected-error{{invalid suffix 'ur' on integer constant}}185 u_long_fract_int = 10ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}186 187 // Decimal integer literals (0)188 short_acc_int = 0hk; // expected-error{{invalid suffix 'hk' on integer constant}}189 acc_int = 0k; // expected-error{{invalid suffix 'k' on integer constant}}190 long_acc_int = 0lk; // expected-error{{invalid suffix 'lk' on integer constant}}191 192 // Decimal integer literals (large number)193 acc_int = 999999999999999999k; // expected-error{{invalid suffix 'k' on integer constant}}194 fract_int = 999999999999999999r; // expected-error{{invalid suffix 'r' on integer constant}}195 196 // Octal integer literals197 short_acc_int = 010hk; // expected-error{{invalid suffix 'hk' on integer constant}}198 acc_int = 010k; // expected-error{{invalid suffix 'k' on integer constant}}199 long_acc_int = 010lk; // expected-error{{invalid suffix 'lk' on integer constant}}200 201 u_short_acc_int = 010uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}202 u_acc_int = 010uk; // expected-error{{invalid suffix 'uk' on integer constant}}203 u_long_acc_int = 010ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}204 205 short_fract_int = 010hr; // expected-error{{invalid suffix 'hr' on integer constant}}206 fract_int = 010r; // expected-error{{invalid suffix 'r' on integer constant}}207 long_fract_int = 010lr; // expected-error{{invalid suffix 'lr' on integer constant}}208 209 u_short_fract_int = 010uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}210 u_fract_int = 010ur; // expected-error{{invalid suffix 'ur' on integer constant}}211 u_long_fract_int = 010ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}212 213 // Hexadecimal integer literals214 short_acc_int = 0x10hk; // expected-error{{invalid suffix 'hk' on integer constant}}215 acc_int = 0x10k; // expected-error{{invalid suffix 'k' on integer constant}}216 long_acc_int = 0x10lk; // expected-error{{invalid suffix 'lk' on integer constant}}217 218 u_short_acc_int = 0x10uhk; // expected-error{{invalid suffix 'uhk' on integer constant}}219 u_acc_int = 0x10uk; // expected-error{{invalid suffix 'uk' on integer constant}}220 u_long_acc_int = 0x10ulk; // expected-error{{invalid suffix 'ulk' on integer constant}}221 222 short_fract_int = 0x10hr; // expected-error{{invalid suffix 'hr' on integer constant}}223 fract_int = 0x10r; // expected-error{{invalid suffix 'r' on integer constant}}224 long_fract_int = 0x10lr; // expected-error{{invalid suffix 'lr' on integer constant}}225 226 u_short_fract_int = 0x10uhr; // expected-error{{invalid suffix 'uhr' on integer constant}}227 u_fract_int = 0x10ur; // expected-error{{invalid suffix 'ur' on integer constant}}228 u_long_fract_int = 0x10ulr; // expected-error{{invalid suffix 'ulr' on integer constant}}229 230 // Using auto231 auto auto_fract = 0r; // expected-error{{invalid suffix 'r' on integer constant}}232 // expected-error@-1{{type specifier missing, defaults to 'int'}}233 auto auto_accum = 0k; // expected-error{{invalid suffix 'k' on integer constant}}234 // expected-error@-1{{type specifier missing, defaults to 'int'}}235}236 237// Ok conversions238int i_const = -2.5hk;239_Sat short _Accum sat_sa_const2 = 256.0k;240_Sat unsigned short _Accum sat_usa_const = -1.0hk;241short _Accum sa_const3 = 2;242short _Accum sa_const4 = -2;243 244// Overflow245short _Accum sa_const = 256.0k; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'short _Accum'}}246short _Fract sf_const = 1.0hk; // expected-warning{{implicit conversion from 1.0 cannot fit within the range of values for 'short _Fract'}}247unsigned _Accum ua_const = -1.0k; // expected-warning{{implicit conversion from -1.0 cannot fit within the range of values for 'unsigned _Accum'}}248short _Accum sa_const2 = 128.0k + 128.0k; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'short _Accum'}}249short s_const = 65536.0lk; // expected-warning{{implicit conversion from 65536.0 cannot fit within the range of values for 'short'}}250unsigned u_const = -2.5hk; // expected-warning{{implicit conversion from -2.5 cannot fit within the range of values for 'unsigned int'}}251char c_const = 256.0uk; // expected-warning{{implicit conversion from 256.0 cannot fit within the range of values for 'char'}}252short _Accum sa_const5 = 256; // expected-warning{{implicit conversion from 256 cannot fit within the range of values for 'short _Accum'}}253unsigned short _Accum usa_const2 = -2; // expected-warning{{implicit conversion from -2 cannot fit within the range of values for 'unsigned short _Accum'}}254 255short _Accum add_ovf1 = 255.0hk + 20.0hk; // expected-warning {{overflow in expression; result is -237.0 with type 'short _Accum'}}256short _Accum add_ovf2 = 10 + 0.5hr; // expected-warning {{overflow in expression; result is 0.5 with type 'short _Fract'}}257short _Accum sub_ovf1 = 16.0uhk - 32.0uhk; // expected-warning {{overflow in expression; result is 240.0 with type 'unsigned short _Accum'}}258short _Accum sub_ovf2 = -255.0hk - 20; // expected-warning {{overflow in expression; result is 237.0 with type 'short _Accum'}}259short _Accum mul_ovf1 = 200.0uhk * 10.0uhk; // expected-warning {{overflow in expression; result is 208.0 with type 'unsigned short _Accum'}}260short _Accum mul_ovf2 = (-0.5hr - 0.5hr) * (-0.5hr - 0.5hr); // expected-warning {{overflow in expression; result is -1.0 with type 'short _Fract'}}261short _Accum div_ovf1 = 255.0hk / 0.5hk; // expected-warning {{overflow in expression; result is -2.0 with type 'short _Accum'}}262 263short _Accum shl_ovf1 = 255.0hk << 8; // expected-warning {{overflow in expression; result is -256.0 with type 'short _Accum'}}264short _Fract shl_ovf2 = -0.25hr << 3; // expected-warning {{overflow in expression; result is 0.0 with type 'short _Fract'}}265unsigned short _Accum shl_ovf3 = 100.5uhk << 3; // expected-warning {{overflow in expression; result is 36.0 with type 'unsigned short _Accum'}}266short _Fract shl_ovf4 = 0.25hr << 2; // expected-warning {{overflow in expression; result is -1.0 with type 'short _Fract'}}267 268_Accum shl_bw1 = 0.000091552734375k << 32; // expected-warning {{shift count >= width of type}} \269 expected-warning {{overflow in expression; result is -65536.0 with type '_Accum'}}270unsigned _Fract shl_bw2 = 0.65ur << 16; // expected-warning {{shift count >= width of type}} \271 expected-warning {{overflow in expression; result is 0.0 with type 'unsigned _Fract'}}272_Sat short _Accum shl_bw3 = (_Sat short _Accum)80.0hk << 17; // expected-warning {{shift count >= width of type}}273short _Accum shr_bw1 = 1.0hk >> 17; // expected-warning {{shift count >= width of type}}274 275_Accum shl_neg1 = 25.5k << -5; // expected-warning {{shift count is negative}} \276 // expected-warning {{overflow in expression; result is 0.0 with type '_Accum'}}277_Accum shr_neg1 = 8.75k >> -9; // expected-warning {{shift count is negative}}278_Fract shl_neg2 = 0.25r << -17; // expected-warning {{shift count is negative}} \279 // expected-warning {{overflow in expression; result is 0.0 with type '_Fract'}}280 281// No warnings for saturation282short _Fract add_sat = (_Sat short _Fract)0.5hr + 0.5hr;283short _Accum sub_sat = (_Sat short _Accum)-200.0hk - 80.0hk;284short _Accum mul_sat = (_Sat short _Accum)80.0hk * 10.0hk;285short _Fract div_sat = (_Sat short _Fract)0.9hr / 0.1hr;286short _Accum shl_sat = (_Sat short _Accum)200.0hk << 5;287 288// Division by zero289short _Accum div_zero = 4.5k / 0.0lr; // expected-error {{initializer element is not a compile-time constant}}290