Changeset 7d7bc09 in mainline


Ignore:
Timestamp:
2018-06-20T18:58:11Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
777832e
Parents:
3bd1d7d4
Message:

abs, labs, llabs.

Location:
uspace/lib
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/io/printf_core.c

    r3bd1d7d4 r7d7bc09  
    3939#include <stdio.h>
    4040#include <stddef.h>
     41#include <stdlib.h>
    4142#include <io/printf_core.h>
    4243#include <ctype.h>
  • uspace/lib/c/generic/stdlib.c

    r3bd1d7d4 r7d7bc09  
    195195}
    196196
     197/** Compute the absolute value of an integer.
     198 *
     199 * If the result cannot be represented, the behavior is undefined.
     200 *
     201 * @param j Integer
     202 * @return The absolute value of @a j
     203 */
     204int abs(int j)
     205{
     206        int aj;
     207
     208        if (j < 0) {
     209                aj = -j;
     210                assert(aj >= 0);
     211        } else {
     212                aj = j;
     213        }
     214
     215        return aj;
     216}
     217
     218/** Compute the absolute value of a long integer.
     219 *
     220 * If the result cannot be represented, the behavior is undefined.
     221 *
     222 * @param j Long integer
     223 * @return The absolute value of @a j
     224 */
     225long labs(long j)
     226{
     227        long aj;
     228
     229        if (j < 0) {
     230                aj = -j;
     231                assert(aj >= 0);
     232        } else {
     233                aj = j;
     234        }
     235
     236        return aj;
     237}
     238
     239/** Compute the absolute value of a long long integer.
     240 *
     241 * If the result cannot be represented, the behavior is undefined.
     242 *
     243 * @param j Long long integer
     244 * @return The absolute value of @a j
     245 */
     246long long llabs(long long j)
     247{
     248        long long aj;
     249
     250        if (j < 0) {
     251                aj = -j;
     252                assert(aj >= 0);
     253        } else {
     254                aj = j;
     255        }
     256
     257        return aj;
     258}
     259
    197260/** Compute quotient and remainder of int division.
    198261 *
  • uspace/lib/c/generic/vfs/vfs.c

    r3bd1d7d4 r7d7bc09  
    285285{
    286286        size_t abs_size;
    287         char *abs = vfs_absolutize(path, &abs_size);
    288         if (!abs)
     287        char *abs_path = vfs_absolutize(path, &abs_size);
     288        if (abs_path == NULL)
    289289                return ENOMEM;
    290290
    291291        int fd;
    292         errno_t rc = vfs_lookup(abs, WALK_DIRECTORY, &fd);
     292        errno_t rc = vfs_lookup(abs_path, WALK_DIRECTORY, &fd);
    293293        if (rc != EOK) {
    294                 free(abs);
     294                free(abs_path);
    295295                return rc;
    296296        }
     
    305305
    306306        cwd_fd = fd;
    307         cwd_path = abs;
     307        cwd_path = abs_path;
    308308        cwd_size = abs_size;
    309309
  • uspace/lib/c/include/macros.h

    r3bd1d7d4 r7d7bc09  
    3838#define min(a, b)  ((a) < (b) ? (a) : (b))
    3939#define max(a, b)  ((a) > (b) ? (a) : (b))
    40 #define abs(a)     ((a) >= 0 ? (a) : -(a))
     40#define mabs(a)    ((a) >= 0 ? (a) : -(a))
    4141
    4242#define ARRAY_SIZE(array)   (sizeof(array) / sizeof(array[0]))
  • uspace/lib/c/include/stdlib.h

    r3bd1d7d4 r7d7bc09  
    8787extern int system(const char *);
    8888
     89extern int abs(int);
     90extern long labs(long);
     91extern long long llabs(long long);
     92
    8993extern int atoi(const char *);
    9094extern long atol(const char *);
  • uspace/lib/c/test/stdlib.c

    r3bd1d7d4 r7d7bc09  
    361361}
    362362
     363/** abs function of positive number */
     364PCUT_TEST(abs_pos)
     365{
     366        int i;
     367
     368        i = abs(1);
     369        PCUT_ASSERT_TRUE(i == 1);
     370}
     371
     372/** abs function of negative number */
     373PCUT_TEST(abs_neg)
     374{
     375        int i;
     376
     377        i = abs(-1);
     378        PCUT_ASSERT_TRUE(i == 1);
     379}
     380
     381/** labs function of positive number */
     382PCUT_TEST(labs_pos)
     383{
     384        long li;
     385
     386        li = labs(1);
     387        PCUT_ASSERT_TRUE(li == 1);
     388}
     389
     390/** labs function of negative number */
     391PCUT_TEST(labs_neg)
     392{
     393        long li;
     394
     395        li = labs(-1);
     396        PCUT_ASSERT_TRUE(li == 1);
     397}
     398
     399/** llabs function of positive number */
     400PCUT_TEST(llabs_pos)
     401{
     402        long long lli;
     403
     404        lli = llabs(1);
     405        PCUT_ASSERT_TRUE(lli == 1);
     406}
     407
     408/** llabs function of negative number */
     409PCUT_TEST(llabs_neg)
     410{
     411        long long lli;
     412
     413        lli = llabs(-1);
     414        PCUT_ASSERT_TRUE(lli == 1);
     415}
     416
    363417/** Integer division */
    364418PCUT_TEST(div_func)
  • uspace/lib/posix/include/posix/stdlib.h

    r3bd1d7d4 r7d7bc09  
    4242#include <_bits/NULL.h>
    4343
    44 /* Absolute Value */
    45 extern int abs(int i);
    46 extern long labs(long i);
    47 extern long long llabs(long long i);
    48 
    4944/* Environment Access */
    5045extern int putenv(char *string);
  • uspace/lib/posix/src/stdlib.c

    r3bd1d7d4 r7d7bc09  
    4949#include "libc/vfs/vfs.h"
    5050#include "libc/stats.h"
    51 
    52 /**
    53  * Integer absolute value.
    54  *
    55  * @param i Input value.
    56  * @return Absolute value of the parameter.
    57  */
    58 int abs(int i)
    59 {
    60         return i < 0 ? -i : i;
    61 }
    62 
    63 /**
    64  * Long integer absolute value.
    65  *
    66  * @param i Input value.
    67  * @return Absolute value of the parameter.
    68  */
    69 long labs(long i)
    70 {
    71         return i < 0 ? -i : i;
    72 }
    73 
    74 /**
    75  * Long long integer absolute value.
    76  *
    77  * @param i Input value.
    78  * @return Absolute value of the parameter.
    79  */
    80 long long llabs(long long i)
    81 {
    82         return i < 0 ? -i : i;
    83 }
    8451
    8552/**
  • uspace/lib/posix/src/stdlib/strtold.c

    r3bd1d7d4 r7d7bc09  
    5252#ifndef HUGE_VALL
    5353#define HUGE_VALL (+1.0l / +0.0l)
    54 #endif
    55 
    56 #ifndef abs
    57 #define abs(x) (((x) < 0) ? -(x) : (x))
    5854#endif
    5955
Note: See TracChangeset for help on using the changeset viewer.