Skip to content

Commit bdbe81f

Browse files
committed
Fix libm regression.
1 parent f0142e3 commit bdbe81f

File tree

5 files changed

+73
-22
lines changed

5 files changed

+73
-22
lines changed

lib/std/math/math.c3

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import std::math::matrix;
77
import std::math::quaternion;
88

99
attrdef @MathLibc(name) = @extern(name), @link(env::POSIX, "m");
10-
attrdef @LinkMath = @link(env::POSIX, "m");
1110

1211
const E = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466;
1312
const LOG2E = 1.44269504088896340735992468100189214; // log2(e)
@@ -235,14 +234,14 @@ macro asinh(x)
235234
<*
236235
@require values::@is_floatlike(x) : `The input must be a floating point value or float vector`
237236
*>
238-
macro ceil(x) @LinkMath => $$ceil(x);
237+
macro ceil(x) => $$ceil(x);
239238

240239
<*
241240
Ceil for compile time evaluation.
242241
243242
@require @typeis($input, double) || @typeis($input, float) : "Only float and double may be used"
244243
*>
245-
macro @ceil($input) @const @LinkMath => $$ceil($input);
244+
macro @ceil($input) @const => $$ceil($input);
246245

247246
<*
248247
Constrain the value to lie within the given interval.
@@ -256,18 +255,18 @@ macro @ceil($input) @const @LinkMath => $$ceil($input);
256255
@require values::@assign_to(lower, x) : `The lower bound must be convertable to the value type.`
257256
@require values::@assign_to(upper, x) : `The upper bound must be convertable to the value type.`
258257
*>
259-
macro clamp(x, lower, upper) @LinkMath => $$max(($typeof(x))lower, $$min(x, ($typeof(x))upper));
258+
macro clamp(x, lower, upper) => $$max(($typeof(x))lower, $$min(x, ($typeof(x))upper));
260259

261260
<*
262261
@require values::@is_promotable_to_floatlike(mag) : `The input must be a number value or float vector`
263262
@require $defined(($typeof(values::promote_int(mag)))mag) : `It's not possible to cast the sign to the type of the magnitude`
264263
*>
265-
macro copysign(mag, sgn) @LinkMath => $$copysign(values::promote_int_same(mag, sgn), ($typeof(values::promote_int_same(mag, sgn)))sgn);
264+
macro copysign(mag, sgn) => $$copysign(values::promote_int_same(mag, sgn), ($typeof(values::promote_int_same(mag, sgn)))sgn);
266265

267266
<*
268267
@require values::@is_promotable_to_floatlike(x) : `The input must be a number value or float vector`
269268
*>
270-
macro cos(x) @LinkMath => $$cos(values::promote_int(x));
269+
macro cos(x) => $$cos(values::promote_int(x));
271270

272271
<*
273272
@require values::@is_promotable_to_floatlike(x) : `The input must be a number value or float vector`
@@ -297,17 +296,17 @@ macro cotanh(x) => (exp(2.0 * x) + 1.0) / (exp(2.0 * x) - 1.0);
297296
<*
298297
@require values::@is_promotable_to_floatlike(x) : `The input must be a number value or float vector`
299298
*>
300-
macro exp(x) @LinkMath => $$exp(values::promote_int(x));
299+
macro exp(x) => $$exp(values::promote_int(x));
301300

302301
<*
303302
@require values::@is_promotable_to_floatlike(x) : `The input must be a number value or float vector`
304303
*>
305-
macro exp2(x) @LinkMath => $$exp2(values::promote_int(x));
304+
macro exp2(x) => $$exp2(values::promote_int(x));
306305

307306
<*
308307
@require values::@is_promotable_to_floatlike(x) : `The input must be a number value or float vector`
309308
*>
310-
macro floor(x) @LinkMath => $$floor(values::promote_int(x));
309+
macro floor(x) => $$floor(values::promote_int(x));
311310

312311
<*
313312
@require values::@is_promotable_to_floatlike(a) : `The input must be a number or float vector`
@@ -316,7 +315,7 @@ macro floor(x) @LinkMath => $$floor(values::promote_int(x));
316315
@require values::@is_same_vector_type(a, b) : `The input types must be equal`
317316
@require values::@is_same_vector_type(a, c) : `The input types must match`
318317
*>
319-
macro fma(a, b, c) @LinkMath => $$fma(a, b, c);
318+
macro fma(a, b, c) => $$fma(a, b, c);
320319

321320

322321
<*
@@ -329,26 +328,26 @@ macro hypot(x, y) => sqrt(sqr(x) + sqr(y));
329328
<*
330329
@require values::@is_promotable_to_floatlike(x) : `The input must be a number or a float vector`
331330
*>
332-
macro ln(x) @LinkMath => $$log(values::promote_int(x));
331+
macro ln(x) => $$log(values::promote_int(x));
333332

334333
<*
335334
@require values::@is_promotable_to_floatlike(x) : `The input must be a number or a float vector`
336335
@require values::@is_promotable_to_floatlike(base) : `The base must be a number or a float vector`
337336
*>
338-
macro log(x, base) @LinkMath
337+
macro log(x, base)
339338
{
340339
return $$log(values::promote_int_same(x, base)) / $$log(values::promote_int_same(base, x));
341340
}
342341

343342
<*
344343
@require values::@is_promotable_to_floatlike(x) : `The input must be a number or a float vector`
345344
*>
346-
macro log2(x) @LinkMath => $$log2(values::promote_int(x));
345+
macro log2(x) => $$log2(values::promote_int(x));
347346

348347
<*
349348
@require values::@is_promotable_to_floatlike(x) : `The input must be a number or a float vector`
350349
*>
351-
macro log10(x) @LinkMath => $$log10(values::promote_int(x));
350+
macro log10(x) => $$log10(values::promote_int(x));
352351

353352
<*
354353
@require types::is_numerical($typeof(x)) : `The input must be a floating point value or float vector`
@@ -1095,21 +1094,21 @@ fn float _frexpf(float x, int* e)
10951094
}
10961095
}
10971096

1098-
macro overflow_add_helper(x, y) @local @LinkMath
1097+
macro overflow_add_helper(x, y) @local
10991098
{
11001099
$typeof(x) res @noinit;
11011100
if ($$overflow_add(x, y, &res)) return OVERFLOW?;
11021101
return res;
11031102
}
11041103

1105-
macro overflow_sub_helper(x, y) @local @LinkMath
1104+
macro overflow_sub_helper(x, y) @local
11061105
{
11071106
$typeof(x) res @noinit;
11081107
if ($$overflow_sub(x, y, &res)) return OVERFLOW?;
11091108
return res;
11101109
}
11111110

1112-
macro overflow_mul_helper(x, y) @local @LinkMath
1111+
macro overflow_mul_helper(x, y) @local
11131112
{
11141113
$typeof(x) res @noinit;
11151114
if ($$overflow_mul(x, y, &res)) return OVERFLOW?;
@@ -1123,7 +1122,7 @@ macro overflow_mul_helper(x, y) @local @LinkMath
11231122
@require values::@is_flat_intlike(a) &&& values::@is_flat_intlike(b) : "a and b must both be integer or integer vector based"
11241123
@require $defined(*out) &&& values::@is_same_type(*out, a) : "out must be a pointer of the same type as a and b"
11251124
*>
1126-
macro bool overflow_add(a, b, out) @LinkMath => $$overflow_add(a, b, out);
1125+
macro bool overflow_add(a, b, out) => $$overflow_add(a, b, out);
11271126

11281127
<*
11291128
@param [&out] out : "Where the result of the subtraction is stored"
@@ -1132,7 +1131,7 @@ macro bool overflow_add(a, b, out) @LinkMath => $$overflow_add(a, b, out);
11321131
@require values::@is_flat_intlike(a) &&& values::@is_flat_intlike(b) : "a and b must both be integer or integer vector based"
11331132
@require $defined(*out) &&& values::@is_same_type(*out, a) : "out must be a pointer of the same type as a and b"
11341133
*>
1135-
macro bool overflow_sub(a, b, out) @LinkMath => $$overflow_sub(a, b, out);
1134+
macro bool overflow_sub(a, b, out) => $$overflow_sub(a, b, out);
11361135

11371136
<*
11381137
@param [&out] out : "Where the result of the multiplication is stored"
@@ -1141,7 +1140,7 @@ macro bool overflow_sub(a, b, out) @LinkMath => $$overflow_sub(a, b, out);
11411140
@require values::@is_flat_intlike(a) &&& values::@is_flat_intlike(b) : "a and b must both be integer or integer vector based"
11421141
@require $defined(*out) &&& values::@is_same_type(*out, a) : "out must be a pointer of the same type as a and b"
11431142
*>
1144-
macro bool overflow_mul(a, b, out) @LinkMath => $$overflow_mul(a, b, out);
1143+
macro bool overflow_mul(a, b, out) => $$overflow_mul(a, b, out);
11451144

11461145
<*
11471146
@require types::is_vector($Type) || ($Type.kindof == ARRAY &&& types::is_numerical($typefrom($Type.inner)))

src/build/build_options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ static void parse_option(BuildOptions *options)
13621362
break;
13631363

13641364
}
1365-
FAIL_WITH_ERR("Cannot process the unknown option \"%s\".", current_arg);
1365+
FAIL_WITH_ERR_LONG("Cannot process the unknown option \"%s\".", current_arg);
13661366
}
13671367

13681368
BuildOptions parse_arguments(int argc, const char *argv[])

src/compiler/compiler_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,7 @@ typedef struct CopyStruct_
18771877
typedef struct
18781878
{
18791879
const char **links;
1880+
bool link_math;
18801881
} Linking;
18811882

18821883
typedef struct

src/compiler/linker.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ static void linker_setup_macos(const char ***args_ref, Linker linker_type)
268268
error_exit("Cannot crosslink MacOS without providing --macossdk.");
269269
}
270270
linking_add_link(&compiler.linking, "System");
271+
if (compiler.linking.link_math) linking_add_link(&compiler.linking, "m");
271272
add_plain_arg("-syslibroot");
272273
add_quote_arg(compiler.build.macos.sysroot);
273274
if (is_no_pie(compiler.platform.reloc_model)) add_plain_arg("-no_pie");
@@ -406,6 +407,7 @@ static void linker_setup_linux(const char ***args_ref, Linker linker_type, bool
406407
add_plain_arg("-nostdlib");
407408
return;
408409
}
410+
if (compiler.linking.link_math) linking_add_link(&compiler.linking, "m");
409411
if (compiler.build.debug_info == DEBUG_INFO_FULL)
410412
{
411413
add_plain_arg("-rdynamic");
@@ -450,6 +452,7 @@ static void linker_setup_linux(const char ***args_ref, Linker linker_type, bool
450452
add_concat_file_arg(crt_dir, "crtn.o");
451453
add_concat_quote_arg("-L", crt_dir);
452454
add_plain_arg("--dynamic-linker=/lib64/ld-linux-x86-64.so.2");
455+
if (compiler.linking.link_math) linking_add_link(&compiler.linking, "m");
453456
linking_add_link(&compiler.linking, "pthread");
454457
linking_add_link(&compiler.linking, "c");
455458
add_plain_arg("-L/usr/lib/");
@@ -525,12 +528,15 @@ static void linker_setup_android(const char ***args_ref, Linker linker_type, boo
525528
add_plain_arg(scratch_buffer_copy());
526529

527530
add_plain_arg("-ldl");
531+
if (compiler.linking.link_math) add_plain_arg("-lm");
528532
add_plain_arg("-lc");
529533
}
530534

531535
static void linker_setup_freebsd(const char ***args_ref, Linker linker_type, bool is_dylib)
532536
{
533-
if (linker_type == LINKER_CC) {
537+
if (linker_type == LINKER_CC)
538+
{
539+
if (compiler.linking.link_math) linking_add_link(&compiler.linking, "m");
534540
linking_add_link(&compiler.linking, "pthread");
535541
linking_add_link(&compiler.linking, "execinfo"); // for backtrace
536542
if (compiler.build.debug_info == DEBUG_INFO_FULL)
@@ -573,6 +579,7 @@ static void linker_setup_freebsd(const char ***args_ref, Linker linker_type, boo
573579
add_concat_quote_arg("-L", crt_dir);
574580
add_plain_arg("--dynamic-linker=/libexec/ld-elf.so.1");
575581
linking_add_link(&compiler.linking, "c");
582+
if (compiler.linking.link_math) linking_add_link(&compiler.linking, "m");
576583
linking_add_link(&compiler.linking, "gcc");
577584
linking_add_link(&compiler.linking, "gcc_s");
578585

0 commit comments

Comments
 (0)