mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
2fc507c98f
* modelines: tw -> sw shiftwidth, not textwidth. * space-surround modelines * fix irregular modelines * Fix modeline in titlegen.c
97 lines
3.8 KiB
C
97 lines
3.8 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
|
│ │
|
|
│ Musl Libc │
|
|
│ Copyright © 2005-2014 Rich Felker, et al. │
|
|
│ │
|
|
│ Permission is hereby granted, free of charge, to any person obtaining │
|
|
│ a copy of this software and associated documentation files (the │
|
|
│ "Software"), to deal in the Software without restriction, including │
|
|
│ without limitation the rights to use, copy, modify, merge, publish, │
|
|
│ distribute, sublicense, and/or sell copies of the Software, and to │
|
|
│ permit persons to whom the Software is furnished to do so, subject to │
|
|
│ the following conditions: │
|
|
│ │
|
|
│ The above copyright notice and this permission notice shall be │
|
|
│ included in all copies or substantial portions of the Software. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
|
|
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
|
|
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
|
|
│ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │
|
|
│ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │
|
|
│ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │
|
|
│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
|
|
│ │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/math.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
Musl libc (MIT License)\\n\
|
|
Copyright 2005-2014 Rich Felker, et. al.\"");
|
|
asm(".include \"libc/disclaimer.inc\"");
|
|
// clang-format off
|
|
|
|
float fmodf(float x, float y)
|
|
{
|
|
union {float f; uint32_t i;} ux = {x}, uy = {y};
|
|
int ex = ux.i>>23 & 0xff;
|
|
int ey = uy.i>>23 & 0xff;
|
|
uint32_t sx = ux.i & 0x80000000;
|
|
uint32_t i;
|
|
uint32_t uxi = ux.i;
|
|
|
|
if (uy.i<<1 == 0 || isnan(y) || ex == 0xff)
|
|
return (x*y)/(x*y);
|
|
if (uxi<<1 <= uy.i<<1) {
|
|
if (uxi<<1 == uy.i<<1)
|
|
return 0*x;
|
|
return x;
|
|
}
|
|
|
|
/* normalize x and y */
|
|
if (!ex) {
|
|
for (i = uxi<<9; i>>31 == 0; ex--, i <<= 1);
|
|
uxi <<= -ex + 1;
|
|
} else {
|
|
uxi &= -1U >> 9;
|
|
uxi |= 1U << 23;
|
|
}
|
|
if (!ey) {
|
|
for (i = uy.i<<9; i>>31 == 0; ey--, i <<= 1);
|
|
uy.i <<= -ey + 1;
|
|
} else {
|
|
uy.i &= -1U >> 9;
|
|
uy.i |= 1U << 23;
|
|
}
|
|
|
|
/* x mod y */
|
|
for (; ex > ey; ex--) {
|
|
i = uxi - uy.i;
|
|
if (i >> 31 == 0) {
|
|
if (i == 0)
|
|
return 0*x;
|
|
uxi = i;
|
|
}
|
|
uxi <<= 1;
|
|
}
|
|
i = uxi - uy.i;
|
|
if (i >> 31 == 0) {
|
|
if (i == 0)
|
|
return 0*x;
|
|
uxi = i;
|
|
}
|
|
for (; uxi>>23 == 0; uxi <<= 1, ex--);
|
|
|
|
/* scale result up */
|
|
if (ex > 0) {
|
|
uxi -= 1U << 23;
|
|
uxi |= (uint32_t)ex << 23;
|
|
} else {
|
|
uxi >>= -ex + 1;
|
|
}
|
|
uxi |= sx;
|
|
ux.i = uxi;
|
|
return ux.f;
|
|
}
|