Changeset 84929b0 in mainline


Ignore:
Timestamp:
2018-08-29T20:06:00Z (6 years ago)
Author:
Jiří Zárevúcky <jiri.zarevucky@…>
Children:
ed9043f7
Parents:
184ff675
git-author:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-29 19:44:33)
git-committer:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-08-29 20:06:00)
Message:

Strip libmath to the bare necessities

Location:
uspace
Files:
2 added
1 deleted
6 edited
5 moved

Legend:

Unmodified
Added
Removed
  • uspace/Makefile.common

    r184ff675 r84929b0  
    109109LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
    110110
    111 LIBMATH_PREFIX = $(LIB_PREFIX)/math
    112 LIBMATH_INCLUDES_FLAGS = \
    113         -I$(LIBMATH_PREFIX)/include \
    114         -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include
    115 
    116 LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
    117111LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
    118112
  • uspace/lib/c/include/float.h

    r184ff675 r84929b0  
    11/*
    2  * Copyright (c) 2014 Martin Decky
     2 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup libmath
    30  * @{
    31  */
    32 /** @file
    33  */
     29#ifndef _FLOAT_H
     30#define _FLOAT_H
    3431
    35 #ifndef LIBMATH_FMOD_H_
    36 #define LIBMATH_FMOD_H_
     32// FIXME: <float.h> is freestanding. Just include the compiler-provided file.
    3733
    38 #include <mathtypes.h>
    39 
    40 extern float32_t float32_fmod(float32_t, float32_t);
    41 extern float64_t float64_fmod(float64_t, float64_t);
     34#define FLT_MANT_DIG  __FLT_MANT_DIG__
     35#define DBL_MANT_DIG  __DBL_MANT_DIG__
     36#define FLT_MAX_EXP __FLT_MAX_EXP__
     37#define DBL_MAX_EXP __DBL_MAX_EXP__
    4238
    4339#endif
    4440
    45 /** @}
    46  */
  • uspace/lib/math/Makefile

    r184ff675 r84929b0  
    3434
    3535SOURCES = \
     36        generic/__fcompare.c \
     37        generic/__fpclassify.c \
     38        generic/__signbit.c \
     39        generic/fabs.c \
    3640        generic/fmod.c \
    3741        generic/trig.c \
  • uspace/lib/math/generic/__fcompare.c

    r184ff675 r84929b0  
    11/*
    2  * Copyright (c) 2011 Petr Koupy
     2 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup libposix
    30  * @{
     29#include <math.h>
     30#include <stdarg.h>
     31
     32/**
     33 * Fallback symbol used when code including <math.h> is compiled with something
     34 * other than GCC or Clang. The function itself must be built with GCC or Clang.
    3135 */
    32 /** @file Mathematical operations.
    33  *
    34  * The purpose of this file is only to provide prototypes of mathematical
    35  * functions defined by C standard and by POSIX.
    36  *
    37  * It is up to the application to correctly link with either libmath
    38  * (provided by HelenOS) or by some other math library (such as fdlibm).
    39  */
     36int __fcompare(size_t sz1, size_t sz2, ...)
     37{
     38        va_list ap;
     39        va_start(ap, sz2);
    4040
    41 #ifndef POSIX_MATH_H_
    42 #define POSIX_MATH_H_
     41        long double val1;
     42        long double val2;
    4343
    44 #ifdef __GNUC__
    45 #define HUGE_VAL (__builtin_huge_val())
    46 #endif
     44        switch (sz1) {
     45        case 4:
     46                val1 = (long double) va_arg(ap, double);
     47                break;
     48        case 8:
     49                val1 = (long double) va_arg(ap, double);
     50                break;
     51        default:
     52                val1 = va_arg(ap, long double);
     53                break;
     54        }
    4755
    48 extern double ldexp(double, int);
    49 extern double frexp(double, int *);
     56        switch (sz2) {
     57        case 4:
     58                val2 = (long double) va_arg(ap, double);
     59                break;
     60        case 8:
     61                val2 = (long double) va_arg(ap, double);
     62                break;
     63        default:
     64                val2 = va_arg(ap, long double);
     65                break;
     66        }
    5067
    51 extern double fabs(double);
    52 extern double floor(double);
    53 extern double ceil(double);
    54 extern double modf(double, double *);
    55 extern double fmod(double, double);
    56 extern double pow(double, double);
    57 extern double exp(double);
    58 extern double frexp(double, int *);
    59 extern double expm1(double);
    60 extern double sqrt(double);
    61 extern double log(double);
    62 extern double log10(double);
    63 extern double sin(double);
    64 extern double sinh(double);
    65 extern double asin(double);
    66 extern double asinh(double);
    67 extern double cos(double);
    68 extern double cosh(double);
    69 extern double acos(double);
    70 extern double acosh(double);
    71 extern double tan(double);
    72 extern double tanh(double);
    73 extern double atan(double);
    74 extern double atanh(double);
    75 extern double atan2(double, double);
    76 extern double copysign(double, double);
     68        va_end(ap);
    7769
    78 #endif /* POSIX_MATH_H_ */
     70        if (isgreaterequal(val1, val2)) {
     71                if (isgreater(val1, val2))
     72                        return __FCOMPARE_GREATER;
     73                else
     74                        return __FCOMPARE_EQUAL;
     75        } else {
     76                if (isless(val1, val2))
     77                        return __FCOMPARE_LESS;
     78                else
     79                        return 0;
     80        }
     81}
    7982
    80 /** @}
    81  */
  • uspace/lib/math/generic/__signbit.c

    r184ff675 r84929b0  
    11/*
    2  * Copyright (c) 2014 Martin Decky
     2 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup libmath
    30  * @{
     29#include <math.h>
     30#include <stdarg.h>
     31
     32/**
     33 * Fallback symbol used when code including <math.h> is compiled with something
     34 * other than GCC or Clang. The function itself must be built with GCC or Clang.
    3135 */
    32 /** @file
    33  */
     36int __signbit(size_t sz, ...)
     37{
     38        va_list ap;
     39        va_start(ap, sz);
    3440
    35 #ifndef LIBMATH_TRIG_H_
    36 #define LIBMATH_TRIG_H_
     41        int result;
    3742
    38 #include <mathtypes.h>
     43        switch (sz) {
     44        case 4:
     45                result = signbit(va_arg(ap, double));
     46                break;
     47        case 8:
     48                result = signbit(va_arg(ap, double));
     49                break;
     50        default:
     51                result = signbit(va_arg(ap, long double));
     52                break;
     53        }
    3954
    40 extern float32_t float32_sin(float32_t);
    41 extern float64_t float64_sin(float64_t);
    42 extern float32_t float32_cos(float32_t);
    43 extern float64_t float64_cos(float64_t);
     55        va_end(ap);
     56        return result;
     57}
    4458
    45 #endif
    46 
    47 /** @}
    48  */
  • uspace/lib/math/generic/fabs.c

    r184ff675 r84929b0  
    3333 */
    3434
    35 #ifndef LIBMATH_TRUNC_H_
    36 #define LIBMATH_TRUNC_H_
     35#include <math.h>
    3736
    38 #include <mathtypes.h>
     37float fabsf(float val)
     38{
     39        return copysignf(val, 1.0f);
     40}
    3941
    40 extern float32_t float32_trunc(float32_t);
    41 extern float64_t float64_trunc(float64_t);
    42 
    43 #endif
     42double fabs(double val)
     43{
     44        return copysign(val, 1.0);
     45}
    4446
    4547/** @}
  • uspace/lib/math/generic/fmod.c

    r184ff675 r84929b0  
    3333 */
    3434
    35 #include <fmod.h>
    3635#include <math.h>
    3736
     
    5251 *
    5352 */
    54 float32_t float32_fmod(float32_t dividend, float32_t divisor)
     53float fmodf(float dividend, float divisor)
    5554{
    5655        // FIXME: replace with exact arithmetics
    5756
    58         float32_t quotient = trunc_f32(dividend / divisor);
     57        float quotient = truncf(dividend / divisor);
    5958
    6059        return (dividend - quotient * divisor);
     
    7776 *
    7877 */
    79 float64_t float64_fmod(float64_t dividend, float64_t divisor)
     78double fmod(double dividend, double divisor)
    8079{
    8180        // FIXME: replace with exact arithmetics
    8281
    83         float64_t quotient = trunc_f64(dividend / divisor);
     82        double quotient = trunc(dividend / divisor);
    8483
    8584        return (dividend - quotient * divisor);
  • uspace/lib/math/generic/trig.c

    r184ff675 r84929b0  
    3535
    3636#include <math.h>
    37 #include <trig.h>
    3837
    3938#define TAYLOR_DEGREE_32 13
     
    4140
    4241/** Precomputed values for factorial (starting from 1!) */
    43 static float64_t factorials[TAYLOR_DEGREE_64] = {
     42static double factorials[TAYLOR_DEGREE_64] = {
    4443        1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
    4544        479001600, 6227020800.0L, 87178291200.0L, 1307674368000.0L,
     
    6059 *
    6160 */
    62 static float32_t taylor_sin_32(float32_t arg)
    63 {
    64         float32_t ret = 0;
    65         float32_t nom = 1;
     61static float taylor_sin_32(float arg)
     62{
     63        float ret = 0;
     64        float nom = 1;
    6665
    6766        for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) {
     
    8988 *
    9089 */
    91 static float64_t taylor_sin_64(float64_t arg)
    92 {
    93         float64_t ret = 0;
    94         float64_t nom = 1;
     90static double taylor_sin_64(double arg)
     91{
     92        double ret = 0;
     93        double nom = 1;
    9594
    9695        for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
     
    118117 *
    119118 */
    120 static float32_t taylor_cos_32(float32_t arg)
    121 {
    122         float32_t ret = 1;
    123         float32_t nom = 1;
     119static float taylor_cos_32(float arg)
     120{
     121        float ret = 1;
     122        float nom = 1;
    124123
    125124        for (unsigned int i = 0; i < TAYLOR_DEGREE_32; i++) {
     
    147146 *
    148147 */
    149 static float64_t taylor_cos_64(float64_t arg)
    150 {
    151         float64_t ret = 1;
    152         float64_t nom = 1;
     148static double taylor_cos_64(double arg)
     149{
     150        double ret = 1;
     151        double nom = 1;
    153152
    154153        for (unsigned int i = 0; i < TAYLOR_DEGREE_64; i++) {
     
    176175 *
    177176 */
    178 static float32_t base_sin_32(float32_t arg)
     177static float base_sin_32(float arg)
    179178{
    180179        unsigned int period = arg / (M_PI / 4);
     
    209208 *
    210209 */
    211 static float64_t base_sin_64(float64_t arg)
     210static double base_sin_64(double arg)
    212211{
    213212        unsigned int period = arg / (M_PI / 4);
     
    242241 *
    243242 */
    244 static float32_t base_cos_32(float32_t arg)
     243static float base_cos_32(float arg)
    245244{
    246245        unsigned int period = arg / (M_PI / 4);
     
    275274 *
    276275 */
    277 static float64_t base_cos_64(float64_t arg)
     276static double base_cos_64(double arg)
    278277{
    279278        unsigned int period = arg / (M_PI / 4);
     
    305304 *
    306305 */
    307 float32_t float32_sin(float32_t arg)
    308 {
    309         float32_t base_arg = fmod_f32(arg, 2 * M_PI);
     306float sinf(float arg)
     307{
     308        float base_arg = fmodf(arg, 2 * M_PI);
    310309
    311310        if (base_arg < 0)
     
    324323 *
    325324 */
    326 float64_t float64_sin(float64_t arg)
    327 {
    328         float64_t base_arg = fmod_f64(arg, 2 * M_PI);
     325double sin(double arg)
     326{
     327        double base_arg = fmod(arg, 2 * M_PI);
    329328
    330329        if (base_arg < 0)
     
    343342 *
    344343 */
    345 float32_t float32_cos(float32_t arg)
    346 {
    347         float32_t base_arg = fmod_f32(arg, 2 * M_PI);
     344float cosf(float arg)
     345{
     346        float base_arg = fmodf(arg, 2 * M_PI);
    348347
    349348        if (base_arg < 0)
     
    362361 *
    363362 */
    364 float64_t float64_cos(float64_t arg)
    365 {
    366         float64_t base_arg = fmod_f64(arg, 2 * M_PI);
     363double cos(double arg)
     364{
     365        double base_arg = fmod(arg, 2 * M_PI);
    367366
    368367        if (base_arg < 0)
  • uspace/lib/math/generic/trunc.c

    r184ff675 r84929b0  
    3434 */
    3535
    36 #include <mathtypes.h>
    37 #include <trunc.h>
     36#include <math.h>
     37#include <float.h>
     38#include <stdint.h>
    3839
    3940/** Truncate fractional part (round towards zero)
     
    5253 *
    5354 */
    54 float32_t float32_trunc(float32_t val)
     55float truncf(float val)
    5556{
    56         float32_u v;
    57         int32_t exp;
     57        /* If the input is a nan, return a canonical nan. */
     58        if (isnan(val))
     59                return __builtin_nanf("");
    5860
    59         v.val = val;
    60         exp = v.data.parts.exp - FLOAT32_BIAS;
     61        const int exp_bias = FLT_MAX_EXP - 1;
     62        const int mant_bits = FLT_MANT_DIG - 1;
     63        const uint32_t mant_mask = (UINT32_C(1) << mant_bits) - 1;
    6164
    62         if (exp < 0) {
    63                 /* -1 < val < 1 => result is +0 or -0 */
    64                 v.data.parts.exp = 0;
    65                 v.data.parts.fraction = 0;
    66         } else if (exp >= FLOAT32_FRACTION_SIZE) {
    67                 if (exp == 1024) {
    68                         /* val is +inf, -inf or NaN => trigger an exception */
    69                         // FIXME TODO
    70                 }
     65        union {
     66                float f;
     67                uint32_t i;
     68        } u = { .f = fabsf(val) };
    7169
    72                 /* All bits in val are relevant for the result */
    73         } else {
    74                 /* Truncate irrelevant fraction bits */
    75                 v.data.parts.fraction &= ~(UINT32_C(0x007fffff) >> exp);
    76         }
     70        int exp = (u.i >> mant_bits) - exp_bias;
    7771
    78         return v.val;
     72        /* If value is less than one, return zero with appropriate sign. */
     73        if (exp < 0)
     74                return copysignf(0.0f, val);
     75
     76        if (exp >= mant_bits)
     77                return val;
     78
     79        /* Truncate irrelevant fraction bits */
     80        u.i &= ~(mant_mask >> exp);
     81        return copysignf(u.f, val);
    7982}
    8083
     
    9497 *
    9598 */
    96 float64_t float64_trunc(float64_t val)
     99double trunc(double val)
    97100{
    98         float64_u v;
    99         int32_t exp;
     101        /* If the input is a nan, return a canonical nan. */
     102        if (isnan(val))
     103                return __builtin_nan("");
    100104
    101         v.val = val;
    102         exp = v.data.parts.exp - FLOAT64_BIAS;
     105        const int exp_bias = DBL_MAX_EXP - 1;
     106        const int mant_bits = DBL_MANT_DIG - 1;
     107        const uint64_t mant_mask = (UINT64_C(1) << mant_bits) - 1;
    103108
    104         if (exp < 0) {
    105                 /* -1 < val < 1 => result is +0 or -0 */
    106                 v.data.parts.exp = 0;
    107                 v.data.parts.fraction = 0;
    108         } else if (exp >= FLOAT64_FRACTION_SIZE) {
    109                 if (exp == 1024) {
    110                         /* val is +inf, -inf or NaN => trigger an exception */
    111                         // FIXME TODO
    112                 }
     109        union {
     110                double f;
     111                uint64_t i;
     112        } u = { .f = fabs(val) };
    113113
    114                 /* All bits in val are relevant for the result */
    115         } else {
    116                 /* Truncate irrelevant fraction bits */
    117                 v.data.parts.fraction &= ~(UINT64_C(0x000fffffffffffff) >> exp);
    118         }
     114        int exp = ((int)(u.i >> mant_bits)) - exp_bias;
    119115
    120         return v.val;
     116        /* If value is less than one, return zero with appropriate sign. */
     117        if (exp < 0)
     118                return copysign(0.0, val);
     119
     120        if (exp >= mant_bits)
     121                return val;
     122
     123        /* Truncate irrelevant fraction bits */
     124        u.i &= ~(mant_mask >> exp);
     125        return copysign(u.f, val);
    121126}
    122127
  • uspace/lib/softfloat/Makefile

    r184ff675 r84929b0  
    3030USPACE_PREFIX = ../..
    3131LIBRARY = libsoftfloat
    32 EXTRA_CFLAGS += $(LIBMATH_INCLUDES_FLAGS)
    3332
    3433SOURCES = \
Note: See TracChangeset for help on using the changeset viewer.